尝试解析以下内容时xml
,
<root>
<SelectValue>One</SelectValue> <!-- Tends to Vary -->
<SubRoot> <!-- Iterate elements inside it using [ SelectValue] -->
<One>One</One>
<Two>Two</Two>
<Three>Three</Three>
</SubRoot>
</root>
如下xsl
,
<xsl:template match="/root">
<xsl:variable name="columns" select="SelectValue"/>
<xsl:for-each select="SubRoot"> -->
<tr>
<td>
<xsl:value-of select="SubRoot/@*[local-name()=$columns]"/>
</td>
</tr>
</xsl:for-each>
检索空 html-tags
虽然我期望像下面这样的东西,
<table>
<thead>
<th>One</th>
</thead>
<tbody>
<tr>
<td>one</td>
</tr>
<tbody>
</table>
我正在尝试传递 <SelectValue>
内的值获取 <SubRoot>
内的节点
这里有什么想法吗?
最佳答案
我无法识别您引用的错误消息(它甚至看起来都不是英语),但在尝试运行您的代码时确实遇到了错误。原因是你的变量名无效;你需要改变:
<xsl:variable name="$columns" select="..."/>
至:
<xsl:variable name="columns" select="..."/>
在引用变量时使用 $
前缀,而不是在定义变量时使用。
另请注意,以 /
开头的 XPath 表达式是从根节点开始的绝对路径。因此,除了 /root
之外,您的任何 select
表达式都不会选择任何内容。我猜你正在尝试做类似的事情:
<xsl:template match="/root">
<xsl:variable name="columns" select="SelectValue"/>
<tr>
<td>
<xsl:value-of select="SubRoot/*[local-name()=$columns]"/>
</td>
</tr>
</xsl:template>
给定您的输入示例将返回:
<tr>
<td>One</td>
</tr>
关于java - 使用 XSL 变量获取值(XPath) - xslt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43693433/