Hello,
I have a problem with grouping respectively merging xml elements. I can only use XPath 1.0 and XSL 1.0.
This is the xml structure:
Code:
<Root>
<Items>
<Item id="A1" name="Part" version="1.0"/>
<Item id="A2" name="Part" version="2.0"/>
<Item id="A3" name="SubPart" version="0.5"/>
<Item id="A4" name="SubPart" version="1.1"/>
<Item id="A5" name="ProductA" version="1.5"/>
<Item id="A6" name="ProductA" version="2.2"/>
<Item id="A7" name="ProductB" version="2.1"/>
</Items>
<Configurations>
<Configuration id="B7" idItem="A7" installed="false">
<Parts>
<Part key="[ProductB]" id="B7" idItem="A7"/>
<Part key="[Part]" id="B1" idItem="A1"/>
<Part key="[SubPart]" id="B9" idItem="A3"/>
</Parts>
</Configuration>
<Configuration id="B3" idItem="A5" installed="false">
<Parts>
<Part key="[Part]" id="B1" idItem="A1"/>
<Part key="[ProductA]" id="B3" idItem="A5"/>
<Part key="[SubPart]" id="B9" idItem="A3"/>
</Parts>
</Configuration>
<Configuration id="B1" idItem="A1" installed="false">
<Parts>
<Part key="[Part]" id="B1" idItem="A1"/>
<Part key="[SubPart]" id="B9" idItem="A3"/>
</Parts>
</Configuration>
<Configuration id="B2" idItem="A2" installed="true">
<Parts>
<Part key="[Part]" id="B2" idItem="A2"/>
<Part key="[SubPart]" id="B10" idItem="A4"/>
</Parts>
</Configuration>
<Configuration id="B4" idItem="A5" installed="true">
<Parts>
<Part key="[ProductA]" id="B4" idItem="A5"/>
<Part key="[Part]" id="B2" idItem="A2"/>
<Part key="[SubPart]" id="B10" idItem="A4"/>
</Parts>
</Configuration>
<Configuration id="B5" idItem="A6" installed="false">
<Parts>
<Part key="[ProductA]" id="B5" idItem="A6"/>
<Part key="[Part]" id="B1" idItem="A1"/>
<Part key="[SubPart]" id="B9" idItem="A3"/>
</Parts>
</Configuration>
<Configuration id="B6" idItem="A6" installed="true">
<Parts>
<Part key="[ProductA]" id="B6" idItem="A6"/>
<Part key="[Part]" id="B2" idItem="A2"/>
<Part key="[SubPart]" id="B10" idItem="A4"/>
</Parts>
</Configuration>
<Configuration id="B8" idItem="A7" installed="true">
<Parts>
<Part key="[ProductB]" id="B8" idItem="A7"/>
<Part key="[Part]" id="B2" idItem="A2"/>
<Part key="[SubPart]" id="B10" idItem="A4"/>
</Parts>
</Configuration>
<Configuration id="B9" idItem="A3" installed="true">
<Parts>
<Part key="[SubPart]" id="B9" idItem="A3"/>
</Parts>
</Configuration>
<Configuration id="B10" idItem="A4" installed="true">
<Parts>
<Part key="[SubPart]" id="B10" idItem="A4"/>
</Parts>
</Configuration>
</Configurations>
</Root>
There are seven items and each item has different configurations.
The
Part elements in the element
Parts are not always in the same order
and the element
Parts doesn't contain always the same amount of
Part
elements for one item name.
It could be possible that there are double versions, e.g.:
ProductA V1.5 consists of:
- ProductA V1.5, Part V1.0 and SubPart V0.5
- ProductA V1.5, Part V2.0 and SubPart V1.1
The desired output should be a table for each item name.
Code:
Part
----------------------
| Part | SubPart |
----------------------
| 1.0 | 0.5 |
----------------------
| 2.0 | 1.1 |
----------------------
ProductA
----------------------------------
| Part | ProductA | SubPart |
----------------------------------
| 1.0 | 1.5 | 0.5 |
----------------------------------
| 2.0 | 1.5 | 1.1 |
----------------------------------
| 1.0 | 2.2 | 0.5 |
----------------------------------
| 2.0 | 2.2 | 1.1 |
----------------------------------
ProductB
----------------------------------
| Part | ProductB | SubPart |
----------------------------------
| 1.0 | 2.1 | 0.5 |
----------------------------------
| 2.0 | 2.1 | 1.1 |
----------------------------------
SubPart
-------------
| SubPart |
-------------
| 0.5 |
-------------
| 1.1 |
-------------
I started with this xsl style sheet to get the superset of the headings (parts) from all configurations of an item name:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="name" match="Item" use="@name"/>
<xsl:key name="headings" match="Parts/*" use="@key"/>
<xsl:variable name="configs" select="//Root/Configurations/Configuration"/>
<xsl:variable name="items" select="//Root/Items/Item"/>
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="./Root/Items/Item[count(.|key('name', @name)[1]) = 1]">
<xsl:sort select="./@name"/>
<xsl:value-of select="./@name"/>
<br/>
<table>
<tr>
<xsl:variable name="name" select="./@name"/>
<xsl:variable name="idsItems" select="../Item[@name=$name]/@id"/>
<xsl:variable name="parts" select="$configs[@idItem=$idsItems]/Parts/*"/>
<xsl:for-each select="$parts[count(.|key('headings', @key)[1])=1]">
<xsl:sort select="./@key"/>
<th>
<xsl:variable name="selectId" select="./@idItem"/>
<xsl:value-of select="$items[@id=$selectId]/@name"/>
</th>
</xsl:for-each>
</tr>
</table>
<br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The output of this xsl style sheet is:
Code:
Part
ProductA
-------------
| ProductA |
-------------
ProductB
----------------------------------
| Part | ProductB | SubPart |
----------------------------------
SubPart
What is wrong with the loop?
Thanks and regards
Jens