I have an XML that needs to be transformed into a better grouping of the items.
- The items need to be counted.
- I do not know how many dogs there will be in the list.
- I may not know how many descriptive items about the dogs there will be. In the example there are three, but it could be any number. If this cannot be flexible, then a fixed number is ok.
Preferrably XSLT 1.0
Is this possible?
I need to go from this:
<table name = "dogs">
<fields>
<field name = "name" value = "dog1"></field>
<field name = "age" value = "2"></field>
<field name = "haircolor" value = "brown"></field>
<field name = "name" value = "dog2"></field>
<field name = "age" value = "10"></field>
<field name = "haircolor" value = "white"></field>
<field name = "name" value = "dog3"></field>
<field name = "age" value = "7"></field>
<field name = "haircolor" value = "black"></field>
<field name = "name" value = "dog4"></field>
<field name = "age" value = "4"></field>
<field name = "haircolor" value = "brown"></field>
</fields>
</table>
To this:
<dogs count = "4">
<dog>
<name>dog1</name>
<age>2</age>
<haircolor>brown</haircolor>
</dog>
<dog>
<name>dog2</name>
<age>10</age>
<haircolor>white</haircolor>
</dog>
<dog>
<name>dog3</name>
<age>7</age>
<haircolor>black</haircolor>
</dog>
<dog>
<name>dog4</name>
<age>4</age>
<haircolor>brown</haircolor>
</dog>
</dogs>
Assuming each group of fields starts with a name: .
XSLT 1.0
Edit:
The following modification starts a new group for every field whose name matches the name of the very first field.
XSLT 1.0
Here is an XSLT 1.0 way of doing it:
If you want to use a parameter then the code could use