How to handle JSON input with Saxon XSLT - xslt

Recent Saxon releases contain a command line argument "-json:myfile.json" to input a JSON file.
But how do I implement the XSLT to parse this JSON? I did not find any doc which handles this directly (without making use of "json-to-xml" or similar).
I only found this: how to convert json to xml with saxonjs?
But this does not help me, because my json starts with an array:
[
{
"eid": "2122.5",
"ecat": "show",
"day": "1629410400",
"spcat": "Bühne",
"time": "19:30",
"text": "Welle",
"remarks": "",
"location": ""
},
{
"eid": "2122.6",
"ecat": "show",
"day": "1629496800",
"spcat": "Bühne",
"time": "19:30",
"text": "Welle",
"remarks": "",
"location": ""
}
]
By writing an intermediate XSLT using the function "json-to-xml", I can convert this JSON to xml that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<array xmlns="http://www.w3.org/2005/xpath-functions">
<map>
<string key="eid">2122.5</string>
<string key="ecat">show</string>
<string key="day">1629410400</string>
<string key="spcat">Bühne</string>
<string key="time">19:30</string>
<string key="text">Welle</string>
<string key="remarks"/>
<string key="location"/>
</map>
<map>
<string key="eid">2122.6</string>
<string key="ecat">show</string>
<string key="day">1629496800</string>
<string key="spcat">Bühne</string>
<string key="time">19:30</string>
<string key="text">Welle</string>
<string key="remarks"/>
<string key="location"/>
</map>
</array>
How can I create a template that matches the root item, and how can I call "apply-templates" to trigger another template that handles the items?

The JSON you have shown is an array so it will be mapped to the XPath/XSLT XDM type array(*), or, in your case, array(map(xs:string, xs:string)). To match on that use e.g. <xsl:template match=".[. instance of array(*)]">..</xsl:template> or e.g. <xsl:template match=".[. instance of array(map(xs:string, xs:string))]">..</xsl:template>.
More complete example would be online at the fiddle, doing:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:output method="xml" indent="yes"/>
<xsl:template match=".[. instance of array(map(xs:string, xs:string))]">
<items>
<xsl:apply-templates select="?*"/>
</items>
</xsl:template>
<xsl:template match=".[. instance of map(xs:string, xs:string)]">
<xsl:variable name="map" select="."/>
<item>
<xsl:iterate select="map:keys(.)">
<value key="{.}">{$map(.)}</value>
</xsl:iterate>
</item>
</xsl:template>
</xsl:stylesheet>
As for grouping:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:output method="xml" indent="yes"/>
<xsl:template match=".[. instance of array(map(xs:string, xs:string))]">
<items>
<xsl:for-each-group select="?*" group-by="?text">
<group name="{current-grouping-key()}">
<xsl:apply-templates select="current-group()"/>
</group>
</xsl:for-each-group>
</items>
</xsl:template>
<xsl:template match=".[. instance of map(xs:string, xs:string)]">
<xsl:variable name="map" select="."/>
<item>
<xsl:iterate select="map:keys(.)[not(. = current-grouping-key())]">
<value key="{.}">{$map(.)}</value>
</xsl:iterate>
</item>
</xsl:template>
</xsl:stylesheet>

Related

Change the Name of the element with Find and Replace

Using Find and Replace (XSLT), how to remove element and create new element.
Found Pattern is : <String ID="p1_w5" CONTENT="Human" HPOS="261.948" VPOS="75.8759" STYLEREFS="font1"/>
Replace Pattern Expect this : <b>Human</b>.
Note <bold> is rename with <b> tag and <superscript> is renamed with <sup> as per HTML. How do achieve my requirements using XSLT?
My Current Input XML File is :
<?xml version="1.0" encoding="UTF-8"?>
<ALTO>
<STYLES>
<TextStyle ID="font1" FONTFAMILY="cambria" FONTSIZE="12.000" FONTSTYLE="bold"/>
<TextStyle ID="font2" FONTFAMILY="cambria" FONTSIZE="7.920" FONTSTYLE="sup"/>
<TextStyle ID="font3" FONTFAMILY="cambria" FONTSIZE="12.000" FONTSTYLE="it"/>
</STYLES>
<LAYOUT>
<TextBlock ID="p1_b1" HPOS="83.6703" VPOS="75.8759" HEIGHT="10.6680" WIDTH="445.700">
<TextLine WIDTH="445.700" HEIGHT="10.6680" ID="p1_t1" HPOS="83.6703" VPOS="75.8759">
<String ID="p1_w1" CONTENT="Hie" HPOS="83.6703" VPOS="75.8759" STYLEREFS="font0"/>
<String ID="p1_w2" CONTENT="org" HPOS="154.915" VPOS="75.8759" STYLEREFS="font0"/>
<String ID="p1_w3" CONTENT="of" HPOS="228.005" VPOS="75.8759" STYLEREFS="font0"/>
<String ID="p1_w4" CONTENT="the" HPOS="241.393" VPOS="75.8759" STYLEREFS="font0"/>
<String ID="p1_w5" CONTENT="Human" HPOS="261.948" VPOS="75.8759" STYLEREFS="font1"/>
<String ID="p1_w6" CONTENT="cell" HPOS="303.263" VPOS="75.8759" STYLEREFS="font0"/>
<String ID="p1_w8" CONTENT="a" HPOS="354.900" VPOS="75.8759" STYLEREFS="font0"/>
<String ID="p1_w9" CONTENT="CANCER" HPOS="363.965" VPOS="75.8759" STYLEREFS="font3"/>
</TextLine>
</TextBlock>
</LAYOUT>
</ALTO>
XSLT is:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output method="xhtml" use-character-maps="m1" indent="no"/>
<xsl:character-map name="m1">
<xsl:output-character character="<" string="<"/>
<xsl:output-character character=">" string=">"/>
</xsl:character-map>
<xsl:preserve-space elements="*"/>
<xsl:template match="/|node()|*|#*">
<xsl:copy>
<xsl:apply-templates select="node() | * | #*"/>
</xsl:copy>
</xsl:template>
<xsl:param name="styletag1" select="TextStyle[#FONTSTYLE = 'superscript']"/>
<xsl:param name="styletag2" select="TextStyle[#FONTSTYLE = 'it']"/>
<xsl:param name="styletag3" select="TextStyle[#FONTSTYLE = 'bold']"/>
<xsl:template match="/|node()|*|#*">
<xsl:copy>
<xsl:apply-templates select="node() | * | #*"/>
</xsl:copy>
</xsl:template>
<xsl:key name="text-style" match="TextStyle/#FONTSTYLE" use="../#ID"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="String[key('text-style', #STYLEREFS)]/#CONTENT">
<xsl:attribute name="{name()}" select="'<' || key('text-style', ../#STYLEREFS) || '>' || . || '</' || key('text-style', ../#STYLEREFS) || '>'"/>
</xsl:template>
</xsl:stylesheet>
A slight adaption of the code in the answer to your previous question
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
expand-text="yes"
version="3.0">
<xsl:output indent="yes"/>
<xsl:key name="text-style" match="TextStyle" use="#ID"/>
<xsl:mode on-no-match="shallow-skip"/>
<xsl:template match="String[key('text-style', #STYLEREFS)]">
<xsl:variable name="replacement" as="node()*">
<xsl:element name="{key('text-style', #STYLEREFS)/#FONTSTYLE}">{#CONTENT}</xsl:element>
</xsl:variable>
<xsl:apply-templates select="$replacement" mode="fix-html"/>
</xsl:template>
<xsl:mode name="fix-html" on-no-match="shallow-copy"/>
<xsl:template mode="fix-html" match="bold">
<b>
<xsl:apply-templates mode="#current"/>
</b>
</xsl:template>
</xsl:stylesheet>
transforms
<?xml version="1.0" encoding="UTF-8"?>
<ALTO>
<STYLES>
<TextStyle ID="font1" FONTFAMILY="cambria" FONTSIZE="12.000" FONTSTYLE="bold"/>
<TextStyle ID="font2" FONTFAMILY="cambria" FONTSIZE="7.920" FONTSTYLE="sup"/>
<TextStyle ID="font3" FONTFAMILY="cambria" FONTSIZE="12.000" FONTSTYLE="it"/>
</STYLES>
<LAYOUT>
<TextBlock ID="p1_b1" HPOS="83.6703" VPOS="75.8759" HEIGHT="10.6680" WIDTH="445.700">
<TextLine WIDTH="445.700" HEIGHT="10.6680" ID="p1_t1" HPOS="83.6703" VPOS="75.8759">
<String ID="p1_w1" CONTENT="Hie" HPOS="83.6703" VPOS="75.8759" STYLEREFS="font0"/>
<String ID="p1_w2" CONTENT="org" HPOS="154.915" VPOS="75.8759" STYLEREFS="font0"/>
<String ID="p1_w3" CONTENT="of" HPOS="228.005" VPOS="75.8759" STYLEREFS="font0"/>
<String ID="p1_w4" CONTENT="the" HPOS="241.393" VPOS="75.8759" STYLEREFS="font0"/>
<String ID="p1_w5" CONTENT="Human" HPOS="261.948" VPOS="75.8759" STYLEREFS="font1"/>
<String ID="p1_w6" CONTENT="cell" HPOS="303.263" VPOS="75.8759" STYLEREFS="font0"/>
<String ID="p1_w8" CONTENT="a" HPOS="354.900" VPOS="75.8759" STYLEREFS="font0"/>
<String ID="p1_w9" CONTENT="CANCER" HPOS="363.965" VPOS="75.8759" STYLEREFS="font3"/>
</TextLine>
</TextBlock>
</LAYOUT>
</ALTO>
into
<b>Human</b>
<it>CANCER</it>
so that should show how to replace the elements that reference a style.
I guess you will want to add further templates like
<xsl:template mode="fix-html" match="it">
<i>
<xsl:apply-templates mode="#current"/>
</i>
</xsl:template>
to transform/fix all those styles you have to corresponding, existing HTML elements.

XML to XML using XSLT remove duplicates

Below is an input xml file:
<assets>
<item>
<file_name>file123</file_name>
<description>testing</description>
<created>date</created>
<metadata>
<guest>name</guest>
<webinfo>test</webinfo>
<albumorder>3</albumorder>
<albumorder>3</albumorder>
</metadata>
</item>
</assets>
From the above xml metadata/albumorder is having duplicate. I want to keep only one albumorder element. How to remove the duplicate elements.
Result file should be like:
<assets>
<item>
<file_name>file123</file_name>
<description>testing</description>
<created>date</created>
<metadata>
<guest>name</guest>
<webinfo>test</webinfo>
<albumorder>3</albumorder>
</metadata>
</item>
</assets>
The following copies only elements, which don't have a same named preceeding sibling element:
<?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" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="*">
<xsl:if test="not(preceding-sibling::*[name(.) = name(current())])">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
There is pattern called "XSL identity template" or "XSL identity transformation", that will keep all XML except for rules you define to change certain elements.
kiwiwings already pointed out the solution, I would just integrate it in the identity template, otherwise you would loose XML comments, attributes, and processing instructions (none of them are present in your XML, but someone else may have them).
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="comment()|processing-instruction()|text()">
<xsl:copy/>
</xsl:template>
<xsl:template match="*">
<xsl:if test="not(preceding-sibling::*[name(.) = name(current())])">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:apply-templates select="comment()|processing-instruction()|text()|*"/>
</xsl:copy>
</xsl:if>
</xsl:template>

Build nodes from xpath

I have a source xml and from this source xml I like to select the nodes given by a path e.g. /shiporder/item/title and /shiporder/shipto/name from the sample source:
<?xml version="1.0" encoding="utf-16"?>
<shiporder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" orderid="orderid1">
<orderperson>orderperson1</orderperson>
<shipto>
<name>name1</name>
</shipto>
<item>
<title>foo</title>
</item>
<item>
<title>bar</title>
</item>
</shiporder>
And I like to transform those nodes to certain target tree e.g. each /shiporder/item/title from the source xml should copied to root/Customer/Name/Title in the target xml tree. So my idea was to generate for each level in the source path a template and call this template from the preceding level:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:func="http://www.functx.com">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<root>
<xsl:apply-templates select="/shiporder"/>
</root>
</xsl:template>
<xsl:template match="/shiporder">
<Customer>
<xsl:apply-templates select="item"/>
<xsl:apply-templates select="shipto"/>
</Customer>
</xsl:template>
<xsl:template match="/shiporder/item">
<Name>
<xsl:apply-templates select="title"/>
</Name>
</xsl:template>
<xsl:template match="/shiporder/shipto">
<Address>
<xsl:apply-templates select="name"/>
</Address>
</xsl:template>
<xsl:template match="/shiporder/item/title">
<Title>
<xsl:value-of select="text()" />
</Title>
</xsl:template>
<xsl:template match="/shiporder/shipto/name">
<Street>
<xsl:value-of select="text()" />
</Street>
</xsl:template>
</xsl:stylesheet>
There for I get a huge stylesheet if I have a huge list of source-paths. Has some one a more feasible idea to reach the target?

how to modify one xml input using variable from second xml input in XSLT?

My question is how to change values in <color> in sample1.xml based on the same <id> in sample2.xml
sample1.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<id>1</id>
<color>red</color>
</item>
<item>
<id>2</id>
<color>blue</color>
</item>
<item>
<id>3</id>
<color>green></color>
</item>
</root>
sample 2.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<id>1</id>
<color>yellow</color>
</item>
<item>
<id>3</id>
<color>white</color>
</item>
</root>
expected output
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<id>1</id>
<color>yellow</color>
</item>
<item>
<id>2</id>
<color>blue</color>
</item>
<item>
<id>3</id>
<color>white></color>
</item>
</root>
I only know how to copy entire sample1.xml to output, but I don't know how to remember ids from sample2.xml, and by that values make changes to sample1.
Don't know if is possible, but probably I must use variables on some unknown way.
Here is my code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs" xmlns:saxon="http://saxon.sf.net/" extension-element-prefixes="saxon"
version="2.0">
<xsl:output method="xml" indent="yes" media-type="text/xml" />
<xsl:param name="sample1"/>
<xsl:param name="sample1_xml" select="saxon:parse($sample1)"/>
<xsl:param name="sample2"/>
<xsl:param name="sample2_xml" select="saxon:parse($sample2)"/>
<xsl:template match="/" name="initial">
<xsl:apply-templates select="$sample1_xml/node()"/> <!-- this is only for copying entire sample1 file -->
</xsl:template>
<!-- copy all nodes and values -->
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I really don't have idea what is the right way to do that, because I am new to XSLT 2.0. Any help will be much appreciated.
I was beaten to this by Martin Honnen, but here's my solution:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs" xmlns:saxon="http://saxon.sf.net/" extension-element-prefixes="saxon"
version="2.0">
<xsl:output method="xml" indent="yes" media-type="text/xml"/>
<xsl:param name="sample1"/>
<xsl:param name="sample1_xml" select="saxon:parse($sample1)"/>
<xsl:param name="sample2"/>
<xsl:param name="sample2_xml" select="saxon:parse($sample2)"/>
<xsl:template match="/" name="initial">
<xsl:apply-templates select="$sample1_xml/node()"/>
<!-- this is only for copying entire sample1 file -->
</xsl:template>
<!-- copy all nodes and values -->
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/root/item/color">
<xsl:param name="id" select="parent::item/id/text()"/>
<xsl:copy>
<xsl:choose>
<xsl:when test="$sample2_xml/root/item[id=$id]/color">
<xsl:value-of select="$sample2_xml/root/item[id=$id]/color/text()"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="text()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
Define a key <xsl:key name="id" match="item" use="id"/> and then add a template
<xsl:template match="item[key('id', id, $sample2_xml)]/color">
<xsl:copy-of select="key('id', ../id, $sample2_xml)/color"/>
</xsl:template>
So the complete sample is
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs saxon"
xmlns:saxon="http://saxon.sf.net/"
version="2.0">
<xsl:output method="xml" indent="yes" media-type="text/xml" />
<xsl:param name="sample1" as="xs:string"><![CDATA[<root>
<item>
<id>1</id>
<color>red</color>
</item>
<item>
<id>2</id>
<color>blue</color>
</item>
<item>
<id>3</id>
<color>green></color>
</item>
</root>]]></xsl:param>
<xsl:param name="sample1_xml" select="saxon:parse($sample1)"/>
<xsl:param name="sample2" as="xs:string"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<id>1</id>
<color>yellow</color>
</item>
<item>
<id>3</id>
<color>white</color>
</item>
</root>]]></xsl:param>
<xsl:param name="sample2_xml" select="saxon:parse($sample2)"/>
<xsl:key name="id" match="item" use="id"/>
<xsl:template match="/" name="initial">
<xsl:apply-templates select="$sample1_xml/node()"/> <!-- this is only for copying entire sample1 file -->
</xsl:template>
<!-- copy all nodes and values -->
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="item[key('id', id, $sample2_xml)]/color">
<xsl:copy-of select="key('id', ../id, $sample2_xml)/color"/>
</xsl:template>
</xsl:stylesheet>

Doing a pivot using XSLT

I have an xml file like this:
<root>
<item>
<name>one</name>
<status>good</status>
</item>
<item>
<name>two</name>
<status>good</status>
</item>
<item>
<name>three</name>
<status>bad</status>
</item>
<item>
<name>four</name>
<status>ugly</status>
</item>
<item>
<name>five</name>
<status>bad</status>
</item>
</root>
I want to transform this using XSLT to get something like:
<root>
<items><status>good</status>
<name>one</name>
<name>two</name>
</items>
<items><status>bad</status>
<name>three</name>
<name>five</name>
</items>
<items><status>ugly</status>
<name>four</name>
</items>
</root>
In other words, I get a list of items, each with a status, and I want to turn it into a list of statuses, each with a list of items.
My initial thought was to do apply-templates matching each status type in turn, but that means I have to know the complete list of statuses. Is there a better way to do it?
Thanks for any help.
Muench to the rescue!
<?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" indent="yes" encoding="UTF-8"/>
<xsl:key name="muench" match="/root/item/status" use="."/>
<xsl:template match="/">
<root>
<xsl:for-each select="/root/item/status[generate-id() = generate-id(key('muench',.)[1])]">
<xsl:call-template name="pivot">
<xsl:with-param name="status" select="."/>
</xsl:call-template>
</xsl:for-each>
</root>
</xsl:template>
<xsl:template name="pivot">
<xsl:param name="status"/>
<items>
<status><xsl:value-of select="$status"/></status>
<xsl:for-each select="/root/item[status=$status]">
<name><xsl:value-of select="name"/></name>
</xsl:for-each>
</items>
</xsl:template>
</xsl:stylesheet>
Yes, this can be done in XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<!-- -->
<xsl:key name="kStatByVal"
match="status" use="."/>
<!-- -->
<xsl:key name="kItemByStat"
match="item" use="status"/>
<!-- -->
<xsl:variable name="vDoc" select="/"/>
<xsl:template match="/">
<top>
<xsl:for-each select=
"/*/*/status[generate-id()
=
generate-id(key('kStatByVal',.)[1])
]">
<items>
<status><xsl:value-of select="."/></status>
<xsl:for-each select="key('kItemByStat', .)">
<xsl:copy-of select="name"/>
</xsl:for-each>
</items>
</xsl:for-each>
</top>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the original XML document:
<root>
<item>
<name>one</name>
<status>good</status>
</item>
<item>
<name>two</name>
<status>good</status>
</item>
<item>
<name>three</name>
<status>bad</status>
</item>
<item>
<name>four</name>
<status>ugly</status>
</item>
<item>
<name>five</name>
<status>bad</status>
</item>
</root>
The wanted result is produced:
<top>
<items>
<status>good</status>
<name>one</name>
<name>two</name>
</items>
<items>
<status>bad</status>
<name>three</name>
<name>five</name>
</items>
<items>
<status>ugly</status>
<name>four</name>
</items>
</top>
Do note the use of:
The Muenchian method for grouping
The use of <xsl:key> and the key() function
It depends about your xslt engine. If you're using xslt 1.0 without any extension, then your approach is certainly the best.
On the other side, if you're allowed to use exslt (especially the node-set extension) or xslt 2.0, then you could do it in a more generic way:
Collect all the available statuses
Create a node-set from the obtained result
Iterating on this node set, create your pivot by filtering you status base on the current element in your iteration.
But before doing that, consider that it may be overkill if you only have a few set of statuses and that adding another status is quite rare.
In XSLT 2.0 you can replace the muenchian grouping by its standard grouping mechanism. Applied to the given answer the xslt would look like:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:key name="muench" match="/root/item/status" use="."/>
<xsl:template match="/">
<root>
<xsl:for-each-group select="/root/item/status" group-by="key('muench', .)">
<xsl:call-template name="pivot">
<xsl:with-param name="status" select="."/>
</xsl:call-template>
</xsl:for-each-group>
</root>
</xsl:template>
<xsl:template name="pivot">
<xsl:param name="status"/>
<items>
<status><xsl:value-of select="$status"/></status>
<xsl:for-each select="/root/item[status=$status]">
<name><xsl:value-of select="name"/></name>
</xsl:for-each>
</items>
</xsl:template>
</xsl:stylesheet>