[英]XSLT - Transform XML input into HTML table with two columns
這是我的源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的豐富經驗,所以希望我能為我做錯什么提供一些指導。
恐怕對此我沒有很好的解釋,但是如果您更改:
<xsl:variable name="countryRight" select="following-sibling::region/country"></xsl:variable>
至:
<xsl:variable name="countryRight" select="following-sibling::region[1]/country"></xsl:variable>
它將按預期工作: https : //xsltfiddle.liberty-development.net/eiZQaGp/7
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.