【问题标题】:XSLT variables in for each loop每个循环中的 XSLT 变量
【发布时间】:2014-06-19 17:08:18
【问题描述】:

XML 文件:

<generic-cv:generic-cv xmlns:generic-cv="http://www.cihr-irsc.gc.ca/generic-cv/1.0.0" lang="en" dateTimeGenerated="2014-05-30 11:40:50">
<section id="f589cbc028c64fdaa783da01647e5e3c" label="Personal Information">
    <section id="2687e70e5d45487c93a8a02626543f64" label="Identification" recordId="4f7c2ebd789f407b939e05664f6aa7c0">
        <field id="ee8beaea41f049d8bcfadfbfa89ac09e" label="Title">
            <lov id="00000000000000000000000000000318">Mr.</lov>
        </field>

        <field id="98ad36fee26a4d6b8953ea764f4fed04" label="First Name">
            <value type="String">Hara</value>
        </field>

    </section>
    <section id="2687e70e5d45487c93a8a02626543f64" label="Identification" recordId="4f7c2ebd789f407b939e05664f6aa7c0">
        <field id="ee8beaea41f049d8bcfadfbfa89ac09e" label="Title">
            <lov id="00000000000000000000000000000318">Mr.</lov>
        </field>

        <field id="98ad36fee26a4d6b8953ea764f4fed04" label="First Name">
            <value type="String">ali</value>
        </field>

    </section>
</section>

还有一个像这样的 xslt 文件

<xsl:stylesheet version="1.0"


  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes"/>
                                    <xsl:variable name="FirstName" select=".//field[@id='98ad36fee26a4d6b8953ea764f4fed04']/value"/>


<xsl:template match="/">
    <html>
        <head>
        <title>Xml Convertor</title>
    </head>
    <body>
        <h2><b> Personal Information</b></h2>
      <ul>
        <xsl:for-each select=".//section[@id='2687e70e5d45487c93a8a02626543f64']" >

            <li>Name: $FirstName></li>


        </xsl:for-each>                    
        </ul> 
    </body>
</html>

而不是使用特定的代码值来获取 xml 文件中的正确信息。 我想使用(全局)变量,以便我可以轻松更改值。 例如,当我想查找并显示(在 html 中)名字时,我会查找代码“98ad36fee26a4d6b8953ea764f4fed04”。相反,我想设置一个变量,例如将 firstName 设置为此值并在 xml 文件中查找变量值。但是,当我将 first name 设置为变量并将其放在 for each 循环下时,它只会打印出名称 Hara 和不是阿里。有没有一种方法可以解决这个问题,而无需将变量声明放在 for each 循环中

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    xsl:variable 的选择表达式在找到变量声明的任何上下文中进行评估。在您的示例中,FirstName 变量将包含来自 all 整个文档中名字字段内部的一组 value 元素(因为变量声明的上下文是根节点 / ) 和value-of,该节点集被定义为该集合中按文档顺序排列的第一个节点的字符串值,即“Hara”。

    在我看来,您正在尝试声明一种 函数 而不是变量 - 无论您调用什么上下文,它都会为您提供名字字段的值 它来自。在 XSLT 1.0 中,这意味着 命名模板

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html" indent="yes"/>
    
      <xsl:template name="firstName">
        <xsl:value-of select=".//field[@id='98ad36fee26a4d6b8953ea764f4fed04']/value" />
      </xsl:template>
    
    
      <xsl:template match="/">
        <html>
          <head>
            <title>Xml Convertor</title>
          </head>
          <body>
            <h2><b> Personal Information</b></h2>
            <ul>
              <xsl:for-each select=".//section[@id='2687e70e5d45487c93a8a02626543f64']" >
                <li>Name: <xsl:call-template name="firstName"/></li>
              </xsl:for-each>                    
            </ul> 
          </body>
        </html>
      </xsl:template>
    </xsl:stylesheet>
    

    当您调用命名模板时,上下文不会改变,因此firstName 模板中的.// 将相对于for-each 的当前迭代的节点

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      相关资源
      最近更新 更多