I'm having a problem where I have to translate non hierarchical XML into hierarchical XML, and I can't get my head around to how to do it.
My input looks something like this:
Code:
<test>
<header>
<adres>adres 1</adres>
</header>
<line>
<art>art 1a</art>
</line>
<line>
<art>art 1b</art>
</line>
<header>
<adres>adres 2</adres>
</header>
<line>
<art>art 2a</art>
</line>
<line>
<art>art 2b</art>
</line>
<line>
<art>art 2c</art>
</line>
</test>
and I want to transform this into something like this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<interchange>
<message>
<id>1</id>
<adres>adres 1</adres>
<details>
<art>art 1a</art>
</details>
<details>
<art>art 1b</art>
</details>
</message>
<message>
<id>2</id>
<adres>adres 2</adres>
<details>
<art>art 2a</art>
</details>
<details>
<art>art 2b</art>
</details>
<details>
<art>art 2c</art>
</details>
</message>
</interchange>
My XSLT currently looks like this:
Code:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<interchange>
<xsl:for-each select="test/header">
<xsl:variable name="curid" select="position()"/>
<message>
<id><xsl:value-of select="$curid"/></id>
<adres><xsl:value-of select="adres"/></adres>
<xsl:for-each select="following-sibling::line[ preceding-sibling::header[position() = $curid] ]">
<details>
<art><xsl:value-of select="art"/></art>
</details>
</xsl:for-each>
</message>
</xsl:for-each>
</interchange>
</xsl:template>
</xsl:stylesheet>
The for-each which is marked not to work, indeed doesn't work as expected.
The select should basically be something like
"SELECT all following <line> elements who's preceding <header> has the same position as the current <header>" (so basically select all following lines until the next header element).
Could someone please help to write this xpath expression correctly? What am I doing wrong?
Thanks