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