XSLT Select 按动态 属性 值
XSLT Select by dynamic property value
我有以下 XML 代表二维数组的文件:
<?xml version="1.0" encoding="UTF-8"?>
<Prop Name='Test' Type='Array' LBound='[0][0]' HBound='[9][9]' ElementType='String' Flags='0x0'>
<Value ID='[0][0]'>1</Value>
<Value ID='[1][0]'>2</Value>
<Value ID='[2][0]'>3</Value>
<Value ID='[0][1]'>10</Value>
<Value ID='[1][1]'>11</Value>
<Value ID='[2][1]'>12</Value>
</Prop>
'ID' 属性 中的第一个括号值是行,第二个值是数组中的列。
'Prop' 中 'Value' 元素的实际数量可能会有所不同,但我将始终使用二维数组。
我需要将其格式化为具有 2 列的 HTML table,如下所示:
为此,我有以下 XSLT,它基本上遍历并将所有以“[0]”结尾的元素打印到第一列,然后尝试找到以“[1]”结尾的匹配元素:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Name Value Pairs</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Name</th>
<th style="text-align:left">Value</th>
</tr>
<xsl:for-each select="Prop[@Name='Test']/Value">
<xsl:if test="contains(self::Value/@ID,'][0]')">
<tr>
<td><xsl:value-of select="self::Value"/></td>
<td><xsl:value-of select="parent::Prop/Value[@ID=concat('[',position()-1,'][1]')]"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
然而,returns 第二列为空,问题似乎出在我尝试使用 @ID 属性 中的 concat 函数动态更改其值时。
我在这里做错了什么?
鉴于您的输入 XML,此 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Name Value Pairs</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Name</th>
<th style="text-align:left">Value</th>
</tr>
<xsl:for-each select="Prop[@Name='Test']/Value">
<xsl:if test="contains(@ID,'][0]')">
<xsl:variable name="pos" select="position()"/>
<tr>
<td><xsl:value-of select="."/></td>
<td>
<xsl:value-of select="../Value[@ID=concat('[',$pos - 1,'][1]')]"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
会产生这个 HTML:
<html>
<body>
<h2>Name Value Pairs</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Name</th>
<th style="text-align:left">Value</th>
</tr>
<tr>
<td>1</td>
<td>10</td>
</tr>
<tr>
<td>2</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>12</td>
</tr>
</table>
</body>
</html>
呈现如下:
我有以下 XML 代表二维数组的文件:
<?xml version="1.0" encoding="UTF-8"?>
<Prop Name='Test' Type='Array' LBound='[0][0]' HBound='[9][9]' ElementType='String' Flags='0x0'>
<Value ID='[0][0]'>1</Value>
<Value ID='[1][0]'>2</Value>
<Value ID='[2][0]'>3</Value>
<Value ID='[0][1]'>10</Value>
<Value ID='[1][1]'>11</Value>
<Value ID='[2][1]'>12</Value>
</Prop>
'ID' 属性 中的第一个括号值是行,第二个值是数组中的列。 'Prop' 中 'Value' 元素的实际数量可能会有所不同,但我将始终使用二维数组。
我需要将其格式化为具有 2 列的 HTML table,如下所示:
为此,我有以下 XSLT,它基本上遍历并将所有以“[0]”结尾的元素打印到第一列,然后尝试找到以“[1]”结尾的匹配元素:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Name Value Pairs</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Name</th>
<th style="text-align:left">Value</th>
</tr>
<xsl:for-each select="Prop[@Name='Test']/Value">
<xsl:if test="contains(self::Value/@ID,'][0]')">
<tr>
<td><xsl:value-of select="self::Value"/></td>
<td><xsl:value-of select="parent::Prop/Value[@ID=concat('[',position()-1,'][1]')]"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
然而,returns 第二列为空,问题似乎出在我尝试使用 @ID 属性 中的 concat 函数动态更改其值时。
我在这里做错了什么?
鉴于您的输入 XML,此 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Name Value Pairs</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Name</th>
<th style="text-align:left">Value</th>
</tr>
<xsl:for-each select="Prop[@Name='Test']/Value">
<xsl:if test="contains(@ID,'][0]')">
<xsl:variable name="pos" select="position()"/>
<tr>
<td><xsl:value-of select="."/></td>
<td>
<xsl:value-of select="../Value[@ID=concat('[',$pos - 1,'][1]')]"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
会产生这个 HTML:
<html>
<body>
<h2>Name Value Pairs</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Name</th>
<th style="text-align:left">Value</th>
</tr>
<tr>
<td>1</td>
<td>10</td>
</tr>
<tr>
<td>2</td>
<td>11</td>
</tr>
<tr>
<td>3</td>
<td>12</td>
</tr>
</table>
</body>
</html>
呈现如下: