Skip to content

Instantly share code, notes, and snippets.

Created April 8, 2011 12:52
Show Gist options
  • Save anonymous/909771 to your computer and use it in GitHub Desktop.
Save anonymous/909771 to your computer and use it in GitHub Desktop.
XSLT to transform a flat XML to a tree-like XML using grouping
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0"
xmlns:tns="your_default_target_namespace"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:ns0="your_webservice_namespace"
xmlns:db="oracle_db_adapter_namespace"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
>
<xsl:template match="/">
<ns0:getToysCollectionResponse>
<return>
<xsl:for-each-group select="//db:toy"
group-by="db:category">
<toys>
<category>
<xsl:value-of select="db:category"/>
<!--alternatively you can use <xsl:value-of select="current-grouping-key()"/>-->
</category>
<members>
<xsl:for-each select="current-group()">
<name>
<xsl:value-of select="db:name"/>
</name>
</xsl:for-each>
</members>
</toys>
</xsl:for-each-group>
</return>
</ns0:getToysCollectionResponse>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment