代码之家  ›  专栏  ›  技术社区  ›  Rookie Programmer Aravind

在xslt 1.0中使用document()函数

  •  2
  • Rookie Programmer Aravind  · 技术社区  · 14 年前

    我正在使用.net代码触发转换,

    除非我加上“ EnableDocumentFunction 属性设置为xsl时,程序抛出错误,说.. Usage of Document() function is prohibited

    实际上这个程序是不可编辑的,而且是只读的。是否可以编辑xsl代码本身以便使用document()函数??

    示例xsl和xmls如下:
    XML示例:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    >
        <xsl:output method="xml" indent="yes"/>
    
        <xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
        </xsl:template>
      <xsl:variable name="State_Code_Trn">
        <State In="California" Out="CA"/>
        <State In="CA" Out="CA"/>
        <State In="Texas" Out="TX"/>
        <State In="TX" Out="TX"/>
      </xsl:variable>
    
      <xsl:template name="testing" match="test_node">
        <xsl:variable name="test_val">
          <xsl:value-of select="."/>
        </xsl:variable>
        <xsl:element name="{name()}">
          <xsl:choose>
          <xsl:when test="document('')/*/xsl:variable[@name='State_Code_Trn']
                  /State[@In=$test_val]">
        <xsl:value-of select="document('')/*/xsl:variable[@name='State_Code_Trn']
                  /State[@In=$test_val]/@Out"/>
          </xsl:when>
            <xsl:otherwise>
              <xsl:text>Other</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
        </xsl:element>
      </xsl:template>
    
    </xsl:stylesheet>
    



    以及示例xml:

    <?xml version="1.0" encoding="utf-8"?>
    <root>
      <test_node>California</test_node>
      <test_node>CA</test_node>
      <test_node> CA</test_node>
      <test_node>Texas</test_node>
      <test_node>TX</test_node>
      <test_node>CAA</test_node>
      <test_node></test_node>
    </root>
    
    2 回复  |  直到 14 年前
        1
  •  2
  •   Tim C    14 年前

    这里的document()函数用于访问xslt文档本身,并提取xsl:变量的内容。在这种情况下,实际上根本不需要使用document()函数。

    因为您在这里使用的是Microsoft.NET,所以您应该能够访问用于xslt的msxml扩展函数。实际上,msxml的相关名称空间已经在xslt文档中定义了

    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    

    这意味着您可以使用node set函数直接轻松访问state_code_tran变量中的节点。为此,请尝试更改现有的 xsl:choose选择 功能如下:

       <xsl:choose>
        <xsl:when test="msxsl:node-set($State_Code_Trn)/State[@In=$test_val]">
         <xsl:value-of select="msxsl:node-set($State_Code_Trn)/State[@In=$test_val]/@Out"/>
        </xsl:when>
        <xsl:otherwise>
         <xsl:text>Other</xsl:text>
        </xsl:otherwise>
       </xsl:choose>
    

    这将产生以下输出

    <root>
    <test_node>CA</test_node>
    <test_node>CA</test_node>
    <test_node>Other</test_node>
    <test_node>TX</test_node>
    <test_node>TX</test_node>
    <test_node>Other</test_node>
    <test_node>Other</test_node>
    </root>
    

    (请注意,在原始XML中的一个“CA”之前有一个空格,这就是为什么它会显示为“其他”。您可能需要添加一些修剪函数来处理此问题。

        2
  •  2
  •   Dimitre Novatchev    14 年前

    除了@tim-c的答案之外,您还可以使用ext:node-set()扩展函数,其中“ext”前缀与exslt命名空间相关联: "http://exslt.org/common" .

    这是由 XslCompiledTransform 并将使您的xslt代码更易于移植,如 EXSLT 是扩展函数的标准库。