I have an XML document with a flat hierarchy which must be transformed in an hierachical XML document:
At first an example of the XML source document:
<group id="xyz">
<name>bla</name>
<part id="abc1"><name>blabla-1</name></part>
<part id="abc2"><name>blabla-2</name></part>
<part id="abc3"><name>blabla-3</name></part>
<part id="abc4"><name>blabla-4</name></part>
<part id="abc5"><name>blabla-5</name></part>
<part id="abc6"><name>blabla-6</name></part>
<part id="abc7"><name>blabla-7</name></part>
<part id="abc8"><name>blabla-8</name></part>
</group>
<group id="abc4">
<name>blabla-4</name>
<part id="abc9"><name>blabla-9</name></part>
<part id="abc10"><name>blabla-10</name></part>
<part id="abc11"><name>blabla-11</name></part>
<part id="abc12"><name>blabla-12</name></part>
<part id="abc13"><name>blabla-13</name></part>
<part id="abc14"><name>blabla-14</name></part>
<part id="abc15"><name>blabla-15</name></part>
<part id="abc16"><name>blabla-16</name></part>
</group>
<group id="abc10">
<name>blabla-10</name>
<part id="abc17"><name>blabla-17</name></part>
<part id="abc18"><name>blabla-18</name></part>
<part id="abc19"><name>blabla-19</name></part>
<part id="abc20"><name>blabla-20</name></part>
<part id="abc21"><name>blabla-21</name></part>
<part id="abc22"><name>blabla-22</name></part>
<part id="abc23"><name>blabla-23</name></part>
<part id="abc24"><name>blabla-24</name></part>
</group>
Here's an example how the targed XML document should look like:
<group id="xyz">
<name>bla</name>
<part id="abc1"><name>blabla-1</name></part>
<part id="abc2"><name>blabla-2</name></part>
<part id="abc3"><name>blabla-3</name></part>
<group id=abc4">
<name>blabla-4</name>
<part id="abc9"><name>blabla-9</name></part>
<group id="abc10">
<name>blabla-10</name>
<part id="abc17"><name>blabla-17</name></part>
<part id="abc18"><name>blabla-18</name></part>
<part id="abc19"><name>blabla-19</name></part>
<part id="abc20"><name>blabla-20</name></part>
<part id="abc21"><name>blabla-21</name></part>
<part id="abc22"><name>blabla-22</name></part>
<part id="abc23"><name>blabla-23</name></part>
<part id="abc24"><name>blabla-24</name></part>
</group>
<part id="abc11"><name>blabla-11</name></part>
<part id="abc12"><name>blabla-12</name></part>
<part id="abc13"><name>blabla-13</name></part>
<part id="abc14"><name>blabla-14</name></part>
<part id="abc15"><name>blabla-15</name></part>
<part id="abc16"><name>blabla-16</name></part>
</group>
</group>
Use case: Imagine a part list. Every group contains some parts. A part in a group can itself be a group which contains further parts. Example shown: The part with "id=abc4" in group with id="xyz" is a group itself. The part with id="abc10" is also a group, it's in the group with id="abc4". The depth of the hierarchy is arbitrary.
Now my XSLT:
<xsl:template match="group[@id='xyz']"
<navigation>
<group id="xyz">
<name>
<xsl:value-of select="name"/>
</name>
<xsl:apply-templates select="part"/>
<xsl:apply-templates/>
</group>
</navigation>
</xsl:template>
<xsl:template match="group">
<group id="{@id}">
<name>
<xsl:value-of select="name">
</name>
<xsl:apply-templates select="part">
</group>
</xsl:template>
<xsl:template match="part">
<xsl:variable name="part-id">
<xsl:value-of select="@id">
<xsl:variable>
<part id="{$part-id}">
<name>
<xsl:value-of select="name"/>
<name>
<xsl:if test="//group[@id = $part-id]">
<xsl:for-each select="//group[@id = $part-id]">
<xsl:apply-templates select="group"/>
<xsl:for-each>
<xsl:if>
</part>
</xsl:template>
My idea: The group with id="xyz" is the biggest group which contains all following groups. Therefore I typed the data directly in the attributes. Then I call template part to get all the parts in the group. At the end of template part I test whether there's a group on the frist level which has the same id as a part. In case it's true I call the template group, get the group-name and again call template part. Actually my plan is to "jump" between group and part until there is no more part which is at the same time a group.
I'm not that much into XSLT so it'll be great if you could give me a hint where something is wrong or whether there's a logical mistake.
No comments:
Post a Comment