first of all, I spend a lot of time here and got often help in the already given answers here. But now I facing a new topic for me - XSLT - and I'm really need help now.
Ok, the problem I have is to transform a XML file I receive from Excel to reuse it in a XFA form. Excel only support flat data but this doesn't match the date hierarchy of my XFA form.
Here is the data I receive from Excel:
<Accounts>
<Account Category="001" Region="AAA" Name="dolor" Value="123" Type="A" Rating="1,25"/>
<Account Category="001" Region="AAA" Name="sit amet" Value="134" Type="A" Rating="1,25"/>
<Account Category="004" Region="BBB" Name="consetetur" Value="434" Type="A" Rating="1,25"/>
<Account Category="002" Region="AAA" Name="sadipscing" Value="84" Type="A" Rating="1,25"/>
<Account Category="007" Region="ZZZ" Name="elitr" Value="33" Type="A" Rating="1,25"/>
<Account Category="004" Region="CCC" Name="aliquyam" Value="6" Type="A" Rating="1,25"/>
<Account Category="001" Region="BBB" Name="ipsum" Value="34" Type="A" Rating="1,25"/>
<Account Category="003" Region="ZZZ" Name="lorem" Value="75" Type="A" Rating="2.87"/>
</Accounts>
And this is the way I want to transform it:
<Accounts>
<Category name="001">
<Region name="AAA">
<Account Name="dolor" Value="123" Type="A" Rating="1,25"/>
<Account Name="sit amet" Value="134" Type="A" Rating="1,25"/>
</Region>
<Region name="BBB">
<Account Name="ipsum" Value="34" Type="A" Rating="1,25"/>
</Region>
</Category>
<Category name="002">
<Region name="BBB">
<Account Name="sadipscing" Value="84" Type="A" Rating="1,25"/>
</Region>
</Category>
<Category name="003">
<Region name="ZZZ">
<Account Name="lorem" Value="75" Type="A" Rating="2.87"/>
</Region>
</Category>
...
</Accounts>
I already spend a couple of days but the only thing I've reached was to create new tags for the Category without duplicates. Here's my current XSLT file.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:variable name="Root" select="Accounts" />
<xsl:variable name="CategoryList" select="$Root/Account[not(@Category=following::Account/@Category)]" />
<xsl:template match="/">
<Accounts>
<xsl:for-each select="$CategoryList">
<xsl:variable name="varCategory" select="./@Category" />
<Category>
<xsl:value-of select="$varCategory"/>
</Category>
</xsl:for-each>
</Accounts>
</xsl:template>
</xsl:stylesheet>
Any help is welcome.
You don't say if you are using XSLT 1.0 or XSLT 2.0.
This is a multi-level sort that, for XSLT 1.0, I tell my students is more easily written using the variable-based grouping method.
For XSLT 2.0 it is very easily done with available constructs.
Both solutions are here: