【发布时间】:2018-12-03 19:36:50
【问题描述】:
这是我的源 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<root>
<employees>
<region>
<country>AUS</country>
<count>3</count>
</region>
<region>
<country>BEL</country>
<count>1</count>
</region>
<region>
<country>PER</country>
<count>1</count>
</region>
<region>
<country>ALA</country>
<count>5</count>
</region>
</employees>
</root>
这是我的 XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:variable name="map">
<entry key="AGO">Angola</entry>
<entry key="ALA">Alaska</entry>
<entry key="AUS">Australia</entry>
<entry key="PER">Peru</entry>
<entry key="NKO">Not Known</entry>
</xsl:variable>
<xsl:template match="employees">
<html>
<body>
<div>
<table>
<xsl:variable name="test" select="region[count > 0]"></xsl:variable>
<xsl:for-each select="$test[position() mod 2 = 1]">
<tr>
<td>
<xsl:variable name="countryLeft" select="country"></xsl:variable>
<xsl:value-of select="msxsl:node-set($map)/entry[@key=$countryLeft]"/>
</td>
<td>
<xsl:variable name="countryRight" select="following-sibling::region/country"></xsl:variable>
<xsl:value-of select="msxsl:node-set($map)/entry[@key=$countryRight]"/>
</td>
</tr>
</xsl:for-each>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XSLT 应该从 XML 中获取每两个区域,并将它们显示在一个有两列的表格行中,每个区域一个列。它还应该将源国家代码映射到相应的显示名称。在此示例中,我将国家/地区地图存储在名为 map 的 XSLT 变量中,但我也可以从另一个 XML 文件中读取它(使用 document() 函数)并且会出现同样的问题。
我希望输出如下:
Australia | Belgium
--------------------
Peru | Alaska
但它正在返回:
Australia | Alaska
------------------
Peru | Alaska
这是一个演示该问题的 XSLT 小提琴:
https://xsltfiddle.liberty-development.net/eiZQaGp/6
我怀疑问题在于将国家/地区代码映射到显示名称,因为如果我不这样做,那么国家/地区代码将正确显示在输出 HTML 表中。
我在 XSLT 方面没有太多经验,因此希望得到一些关于我哪里出错的指导。
【问题讨论】:
-
为什么这个标签是 XSLT 2.0?您的样式表有
version="1.0",而且您似乎正在使用 Microsoft 处理器。 -
对不起 - 我已经删除了这个标签。