Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of XSLT Transformation with for-each-grouping. command to create: saxon -xsl:for-each-group.xsl -s:cities.xml > grouped_cities.xml
<cities>
<state>
<name>Alabama</name>
<city>Abbeville</city>
<number>1</number>
</state>
<state>
<name>Alabama</name>
<city>Adamsville</city>
<number>1</number>
</state>
<state>
<name>Alabama</name>
<city>Addison</city>
<number>1</number>
</state>
<state>
<name>Alabama</name>
<city>Akron</city>
<number>2</number>
</state>
<state>
<name>Missouri</name>
<city>Jefferson City</city>
<number>1</number>
</state>
<state>
<name>Missouri</name>
<city>Columbia</city>
<number>2</number>
</state>
<state>
<name>Kansas</name>
<city>Manhattan</city>
<number>1</number>
</state>
<state>
<name>Kansas</name>
<city>Topeka</city>
<number>2</number>
</state>
</cities>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs xsi xsl">
<xsl:template match="/">
<xsl:for-each-group select="/cities/state"
group-by="name">
<state>
<xsl:attribute name="name"><xsl:value-of select="name" /></xsl:attribute>
<xsl:for-each select="/cities/state/city[../name=current-grouping-key()]">
<city>
<xsl:value-of select="." />
</city>
<number>
<xsl:value-of select="../number" />
</number>
</xsl:for-each>
</state>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<state name="Alabama">
<city>Abbeville</city>
<number>1</number>
<city>Adamsville</city>
<number>1</number>
<city>Addison</city>
<number>1</number>
<city>Akron</city>
<number>2</number>
</state>
<state name="Missouri">
<city>Jefferson City</city>
<number>1</number>
<city>Columbia</city>
<number>2</number>
</state>
<state name="Kansas">
<city>Manhattan</city>
<number>1</number>
<city>Topeka</city>
<number>2</number>
</state>
@Ibrahimhammani

This comment has been minimized.

Copy link

@Ibrahimhammani Ibrahimhammani commented Dec 27, 2017

thanbk your for this gist

@K23Nikhil

This comment has been minimized.

Copy link

@K23Nikhil K23Nikhil commented Feb 12, 2018

Hi I have an issue
Poll concept="outback.tbird" location="000610" date="20180205" search="ERS_LABORSCHED" key="oracle.ers.outback.tbird" created="2018-02-07T12:00:33" cb-s="U">










here is the Raw file and want to below output.

189 LAST THIS NEXT THIS 01/17/2018 9:00 01/18/2018 16:45 9 THIS 01/19/2018 7:00 01/19/2017 13:30 8

1 employee has multiple schedules and wants to check if the employee is same in next row or in the whole xml then it would come in for a particular employee.
Here is the xslt which im using

<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:variable name="SCH1" select="/Poll/Labor/SCH0/SCH1"/>
<xsl:key name="KEYSCHEDULEEMPLOYEE" use="@e" match="/Poll/Labor/SCH0/SCH1"/>
<xsl:template match="/">


<xsl:for-each select="/Poll/Labor/SCH0/SCH1[generate-id()=generate-id(key('KEYSCHEDULEEMPLOYEE',@e))]">
<xsl:element name="EmployeeNumber">
<xsl:value-of select="@e"/>
</xsl:element>
<xsl:variable name="scedule" select="$SCH1/@e"

<xsl:element name="DELETESCHEDULE">LAST</xsl:element>
<xsl:element name="DELETESCHEDULE">THIS</xsl:element>
<xsl:element name="DELETESCHEDULE">NEXT</xsl:element>
<xsl:if test=""

<xsl:element name="Week">THIS</xsl:element>
<xsl:element name="InDay">
<xsl:value-of select="@dt"/>
</xsl:element>
<xsl:element name="InTime">
<xsl:value-of select="@dt"/>
</xsl:element>
<xsl:element name="OutDay">
<xsl:value-of select="@dt"/>
</xsl:element>
<xsl:element name="OutTime">
<xsl:value-of select="@dt"/>
</xsl:element>
<xsl:element name="JobNumber">
<xsl:value-of select="@j"/>
</xsl:element>


</xsl:for-each>


</xsl:template>
</xsl:stylesheet>

but its not working can anyone please help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.