Reputation: 161
I'm struggling with a simple xsl transformation and I'm not able to find out why I have two elements twice in my output result. Here is my code:
xsl-stylesheet:
<xsl:template match="/">
<xsl:text>[{</xsl:text>
<xsl:apply-templates/>
<xsl:text>}]</xsl:text>
</xsl:template>
<xsl:template match="Title">
<xsl:text>{</xsl:text>
<xsl:value-of select="@id"/>
<xsl:apply-templates select="descendant::Title"/>
<xsl:text>}</xsl:text>
</xsl:template>
xml:
<?xml version="1.0" encoding="UTF-8"?>
<Start>
<Title id="1">
<Title id="2">Bob</Title>
<Age>39</Age>
<Address>
<Title id="3">10 Idle Lane</Title>
<City>Yucksville</City>
<PostalCode>xxxyyy</PostalCode>
</Address>
</Title>
<Title id="4">
<Title id="5">Bill</Title>
<Age>39</Age>
<Title id ="6">
<Title id="7">10 Idle Lane</Title>
<City>Yucksville</City>
<PostalCode>xxxyyy</PostalCode>
<Title id="8">10 Idle Lane</Title>
</Title>
<Title id="9">10 Idle Lane</Title>
</Title>
</Start>
output:
[{
{1{2}{3}}
{4{5}{6{7}{8}}{7}{8}{9}}
}]
The elements which I've not expected to have in my output are the last numbers: {8}{9}. I guess it has something to do with the descendant axis selector.
Upvotes: 0
Views: 60
Reputation: 197
True, the descendant axis selector selects all children and grandchildren etc.. So you just need to apply templates to children. This should work:
<xsl:template match="/">
<xsl:text>[{</xsl:text>
<xsl:apply-templates/>
<xsl:text>}]</xsl:text>
</xsl:template>
<xsl:template match="Title">
<xsl:text>{</xsl:text>
<xsl:value-of select="@id"/>
<xsl:apply-templates />
<xsl:text>}</xsl:text>
</xsl:template>
<xsl:template match="text()" />
Upvotes: 1