Reputation: 1708
Here is my source XML file:
<?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>
Here is my 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>
The XSLT should take every two regions from the XML and display them in a table row with two columns, one for each region. It should also map the source country code to the corresponding display name. In this example I'm storing the country map in an XSLT variable named map
, but I could just as well read it from another XML file (using document()
function) and the same issue occurs.
I'm expecting the output to be as follows:
Australia | Belgium
--------------------
Peru | Alaska
But it's returning:
Australia | Alaska
------------------
Peru | Alaska
Here is an XSLT fiddle demonstrating the issue:
https://xsltfiddle.liberty-development.net/eiZQaGp/6
I suspect the issue is with mapping the country code to the display name, because if I don't do this then the country codes display correctly in the output HTML table.
I don't have much experience with XSLT so would appreciate some guidance as to where I'm going wrong.
Upvotes: 0
Views: 381
Reputation: 117073
I am afraid I don't have a good explanation for this, but if you change:
<xsl:variable name="countryRight" select="following-sibling::region/country"></xsl:variable>
to:
<xsl:variable name="countryRight" select="following-sibling::region[1]/country"></xsl:variable>
it will work as expected: https://xsltfiddle.liberty-development.net/eiZQaGp/7
Upvotes: 1