I have a XML like this:
<?xml version="1.0" encoding="UTF-8"?>
<Section>
<Chapter>
<nametable>
<namerow>
<namecell stuff="1">
<entity>A</entity>
</namecell>
<namecell stuff="2">
<entity>B</entity>
</namecell>
</namerow>
</nametable>
</Chapter>
</Section>
My XSLT is like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="Section/Chapter//nametable"/>
</xsl:template>
<xsl:template match="nametable">
<xsl:for-each select="./namerow">
<xsl:value-of select="./namecell/@stuff"/>
<xsl:value-of select="./namecell" />
</xsl:for-each>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
Odd is I’m getting an output in this order 1 2 A B, I thought I’m going to get 1 A 2 B.
Not sure why is that ?.
TIA,
John
Your problem is here:
Unlike in XSLT 1.0 in XSLT 2.0 the
xsl:value-ofinstruction outputs all items of the sequence specified in itsselectattribute.This means that
outputs all
stuffattributes (1 and 2)then the next instruction:
outputs the string value of the two
namecellchildren — respectively"A"and"B".Solution:
Replace:
with:
The complete code with this modification is:
and when this transformation is applied on the provided XML document:
the wanted result is produced:
Second solution (probably what you intended to do in the first place):
Replace:
with:
Now you again get the wanted result: