Hello,
I am new to this forum and hope to place my question here at the right place. What I try to do is a transformation of data from Eurostat (Claset-format) to a nested syle to use some common editors to view the data an easy way. The minimized problem:
Input data:
Code:
<?xml version="1.0" encoding="utf-8"?>
<items>
<item id="i1" level="1" />
<item id="i2" level="2" />
<item id="i3" level="2" />
<item id="i4" level="3" />
<item id="i5" level="3" />
<item id="i6" level="1" />
<item id="i7" level="2" />
</items>
The @levels up between direct neighbors there is only one step alowed
For @levels down: all steps allowed (i.e. i5->i6)
Data should be transformed to 'nested structure': Required output
Code:
<Items>
<Item id="i1">
<Item id="i2" />
<Item id="i3">
<Item id="i4" />
<Item id="i5" />
</Item>
</Item>
<Item id="i6">
<Item id="i7" />
</Item>
</Items>
The transformation (with saxon9he.jar):
Code:
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:template match="/">
<Items>
<xsl:apply-templates select="//item[@level=1]"/>
</Items>
</xsl:template>
<xsl:template match="*">
<Item id="{@id}" level="{@level}">
<xsl:variable name="level"><xsl:value-of select="@level" /></xsl:variable>
<xsl:apply-templates select="following-sibling::item[@level = $level + 1]"/>
</Item>
</xsl:template>
</xsl:transform>
The result is preety close to the required output. But it is faulty.
Code:
<Items>
<Item id="i1" level="1">
<Item id="i2" level="2">
<Item id="i4" level="3"/>
<Item id="i5" level="3"/>
</Item>
<Item id="i3" level="2">
<Item id="i4" level="3"/>
<Item id="i5" level="3"/>
</Item>
<Item id="i7" level="2"/>
</Item>
<Item id="i6" level="1">
<Item id="i7" level="2"/>
</Item>
</Items>
I presume the error is in the filter of the XPath expression "following-sibling::item[@level = $level + 1]".
How can I adress only those following-siblings down to where the attribute (@level) changes?
Any help very much apreciated.
Regards
Christian
(sorry for my english)