I have an XML like this:
<?xml version="1.0" encoding="UTF-8"?>
<Section>
<Chapter>
<Cell colname="1">
<Value>A</Value>
</Cell>
<Cell colname="2">
<MyValue>AAA</MyValue>
<MyValue>BBB</MyValue>
</Cell>
<Cell colname="3">
<MyCar>Honda</MyCar>
</Cell>
</Chapter>
<Chapter>
<Cell colname="1">
<Value>C</Value>
</Cell>
<Cell colname="2">
<MyValue>CCC</MyValue>
</Cell>
<Cell colname="3">
<MyCar>Toyota</MyCar>
</Cell>
</Chapter>
</Section>
I have an XSLT like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="Section/Chapter"/>
</xsl:template>
<xsl:template match="Chapter">
<xsl:apply-templates select="./Cell/MyValue"/>
</xsl:template>
<xsl:template match="MyValue">
<xsl:message>
<xsl:value-of select="../../Cell/@colname"/>
<xsl:value-of select="../../Cell[@colname='1']/Value"/>
<xsl:value-of select="."/>
<xsl:value-of select="../../Cell[@colname='3']/MyCar"/>
</xsl:message>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
Problem is the debug output shows up as 1 2 3 A AAA Honda 1 2 3 A BBB Honda 1 2 3 C CCC Toyota. I’d like to get something like 1 A 2 AAA 3 Honda 1 A 2 BBB 3 Honda 1 C 2 CCC 3 Toyota. Basically getting the value of the attribute colname correctly.
So several questions:
- What did I do wrong ?.
- There is a sequence 1 2 3 generated why ?.
TIA,
John
You didn’t specify exactly the necessary expressions.
Because
../../Cell/@colnameselects thecolnameattribute of everyCellchild of the current node’s grandparentChapter.Solution:
Use:
The debug output is exactly the wanted:
You also can use a slight modification of the answer to your previous question:
When this transformation is applied on the provided XML document:
the wanted, correct result is produced: