【问题标题】:Storing XML attribute's value in a XSL variable将 XML 属性的值存储在 XSL 变量中
【发布时间】:2017-12-05 16:54:30
【问题描述】:

我想使用 XML 属性的值并在 XSL 文件中存储和比较它。我正在通过 JAVA 程序将 XSL 转换为 HTML,该程序运行良好。输出 HTML 为:-

价值 =

XML 文件:

<root>
<Request>
    <Desc>Insert new team into the table</Desc>
    <Param name="teamName" required="false"/>
    <Param name="rankings" required="true"/>
    <Update requires="teamName,rankings" >
        INSERT INTO standing (teamName,rankings) VALUES($rankings,$rankings)
    </Update>
</Request>

XSL 文件:

<xsl:template match="root">
    <xsl:variable name="UpdateRequires"/>
    <html>
        <head>
            <title>DocStyle.xsl</title>
        </head>
        <body> 
            <xsl:for-each select="*/Request">
                <xsl:for-each select="*/Update">
                    <xsl:variable name="UpdateRequires" select="*/Update/@requires"/>
                </xsl:for-each>
            </xsl:for-each>
            <h1>
               Value = <xsl:value-of select="$UpdateRequires"/>
            </h1>
        </body>
    </html>
</xsl:template>

我想显示变量“UpdateRequires”的值,然后将它与Param标签的属性“@name”进行比较,可能像这样“contains(@name,UpdateRequires)”

更新 1.0:

我能够获取变量的值,现在我想比较变量 $UpdateRequires 的值和属性 @name 的值。 它应该为未执行的 contains(@name,$UpdateRequires) 返回 true(带有 test="@name" 的 if 循环只是为了检查值)

对 XSL 所做的更改:

<xsl:for-each select="Request">
                <xsl:variable name="UpdateRequires" select="*/@requires"/>
                <xsl:for-each select="Update">
                    <xsl:variable name="UpdateRequires" select="@requires"/>
                    <h1>
                        We are in Update : <xsl:value-of select="$UpdateRequires"/>
                    </h1>    
                </xsl:for-each>
                <xsl:for-each select="Param">
                    <xsl:if test=" contains(@name,$UpdateRequires) ">
                        <span>
                            We are in Param : <xsl:value-of select="$UpdateRequires"/>
                        </span>
                    </xsl:if>
                    <xsl:if test=" @name ">
                        <span>
                            We are in Param : <br/> Value of variable : <xsl:value-of select="$UpdateRequires"/> Value of name : <xsl:value-of select="@name"/> <br/>
                        </span>
                    </xsl:if>
                </xsl:for-each>

【问题讨论】:

  • 您的 XML 是否会有多个 RequestRequest 可以有多个 Update 吗?
  • 可以有多个,也可以有多个标签。
  • "可以有多个 标签" 那么不清楚你要做什么。 contains() 函数接受字符串作为参数,你不能给它一个节点集。
  • 我只是想比较两个不同标签中两个属性的值。无论如何,感谢您的帮助,我找到了答案。但是由于某种原因,contain() 函数返回 false。

标签: xml variables xslt


【解决方案1】:

您的方法存在几个问题:

首先,您的 XPath 表达式是错误的:Requestroot 的子代,而不是它的孙代 - 所以您的指令 &lt;xsl:for-each select="*/Request"&gt; 什么都不做。

同样,&lt;xsl:for-each select="*/Update"&gt; 不会在 Request 的上下文中执行任何操作。

接下来,变量的作用域仅限于其父元素:如果在xsl:for-each 中定义变量,则不能在该指令之外使用它。


试试类似的东西:

<xsl:template match="/root">
    <html>
        <head>
            <title>DocStyle.xsl</title>
        </head>
        <body> 
            <xsl:for-each select="Request">
                <xsl:variable name="UpdateRequires" select="Update/@requires"/>
                <h1>
                   Value = <xsl:value-of select="$UpdateRequires"/>
                </h1>
             </xsl:for-each>
        </body>
    </html>
</xsl:template>

请注意,这里假设可以有多个 Requests,但每个都只有一个 Update 子代。

【讨论】:

  • 嘿,谢谢您的回答。我之前也试过这个,问题仍然存在,我想在 for-each 标签之外使用 $UpdateRequires 。您对如何实现这一目标有任何想法吗?也许我应该用 JavaScript 来处理这个?
  • 您需要明确您希望如何使用该变量,并显示您的预期结果。你也没有回答我的问题。 -- XSLT 是图灵完备的,因此不需要使用 Javascript 或其他任何东西。
  • 请注意 Params 是 Request 的子代 - 所以您应该能够使用在 &lt;xsl:for-each select="Request"&gt; 中定义的变量来测试它们,没有任何问题。
  • 是的,我使用了这个逻辑并在 中设置了全局变量
【解决方案2】:
<xsl:for-each select="Request">
                <xsl:variable name="UpdateRequires" select="*/@requires"/>
                <xsl:for-each select="Update">
                    <xsl:variable name="UpdateRequires" select="@requires"/>
                    <h1>
                        We are in Update : <xsl:value-of select="$UpdateRequires"/>
                    </h1>    
                </xsl:for-each>
                <xsl:for-each select="Param">
                    <xsl:if test=" matches($UpdateRequires,@name) ">
                        <span>
                            We are in Param : <xsl:value-of select="$UpdateRequires"/><br/>
                        </span>
                    </xsl:if>
                </xsl:for-each>
            </xsl:for-each>

contains() 函数没有返回 true(有人能告诉我为什么吗?)因此我使用了 match()。现在可以了。

【讨论】:

    猜你喜欢
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多