I have the following input XML
Input XML
<bills>
<bill>
<billNo>1</billNo>
<exportType>1</exportType>
<invoice>
<serialNo>1</serialNo>
</invoice>
</bill>
<bill>
<billNo>1</billNo>
<exportType>1</exportType>
<invoice>
<serialNo>2</serialNo>
</invoice>
</bill>
<bill>
<billNo>2</billNo>
<exportType>1</exportType>
<invoice>
<serialNo>1</serialNo>
</invoice>
</bill>
<bill>
<billNo>2</billNo>
<exportType>1</exportType>
<invoice>
<serialNo>2</serialNo>
</invoice>
</bill>
<bill>
<billNo>2</billNo>
<exportType>1</exportType>
<invoice>
<serialNo>3</serialNo>
</invoice>
</bill>
</bills>
that needs to be converted to an output XML
Output XML
<bills>
<bill>
<billNo>1</billNo>
<exportType>1</exportType>
<invoice>
<serialNo>1</serialNo>
</invoice>
<invoice>
<serialNo>2</serialNo>
</invoice>
</bill>
<bill>
<billNo>2</billNo>
<exportType>1</exportType>
<invoice>
<serialNo>1</serialNo>
</invoice>
<invoice>
<serialNo>2</serialNo>
</invoice>
<invoice>
<serialNo>3</serialNo>
</invoice>
</bill>
</bills>
The following XSLT is being used for transformation however I am missing something as the child nodes under bill also get repeatedly copied instead of only the invoicechild node and its children getting copied. XSL is as below
XSL
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:key name="key" match="bill" use="billNo" />
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<xsl:template match="bill[generate-id() = generate-id(key('key', billNo)[1])]">
<bill>
<xsl:apply-templates select="key('key', billNo)/*" />
</bill>
</xsl:template>
<xsl:template match="bill" />
</xsl:stylesheet>
One way to get the expected result is to change this:
<xsl:apply-templates select="key('key', billNo)/*" />
to:
<xsl:apply-templates select="billNo | exportType" />
<xsl:apply-templates select="key('key', billNo)/invoice" />
Note that this assumes that the exportType is the same for all items grouped under a common billNo.
Related
I am having a really hard time trying to group different elements by a common value using XSLT 1.0.
Using the following XML:
<root>
<segment>
<id>ABCD123</id>
</segment>
<segment>
<contact>
<field1>ABCD123</field1>
<field2>(111)345-7890</field2>
</contact>
</segment>
<segment>
<details>
<field1>ABCD123</field1>
<field5>More Details for ABCD123</field5>
</details>
</segment>
<segment>
<id>XZX098</id>
</segment>
<segment>
<contact>
<field1>XZX098</field1>
<field2>(111)443-9999</field2>
</contact>
</segment>
<segment>
<details>
<field1>XZX098</field1>
<field5>More Details for XZX098</field5>
</details>
</segment>
</root>
Transform into this:
<File>
<Record>
<id>ABCD123</id>
<phone>(111)345-7890</phone>
<details>More Details for ABCD123</details>
</Record>
<Record>
<id>XZX098</id>
<phone>(111)443-9999</phone>
<details>More Details for XZX098</details>
</Record>
</File>
I'm trying to group records by the 'id', and then get the contact, and details information that matches that 'id'.
Any help is greatly appreciated.
I don't see any grouping per se required here - just a simple lookup of cross-referenced data:
XSLT 1.0
<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:key name="contact-details" match="contact|details" use="field1" />
<xsl:template match="root">
<File>
<xsl:for-each select="segment/id">
<Record>
<xsl:copy-of select="."/>
<phone>
<xsl:value-of select="key('contact-details', .)/field2"/>
</phone>
<details>
<xsl:value-of select="key('contact-details', .)/field5"/>
</details>
</Record>
</xsl:for-each>
</File>
</xsl:template>
</xsl:stylesheet>
I have a requirement where i need to transform my xml.
This is the xml given to me
<InvoiceDocument>
<Invoice>
<d>100</d>
<a>120</a>
<Products>
<Product>
<b>11</b>
<c>12</c>
</Product>
<Product>
<b>13</b>
<c>14</c>
</Product>
</Products>
</Invoice>
</InvoiceDocument>
This is the xml that i require
<MessageParts xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/LedgerGeneralJournal">
<LedgerJournalTable class="entity">
<r:z>50</r:z>
<LedgerJournalTrans class="entity">
<r:e>120</r:e>
</LedgerJournalTrans>
<LedgerJournalTrans class="entity">
<r:g>11</r:g>
<r:h>12</r:h>
</LedgerJournalTrans>
<LedgerJournalTrans class="entity">
<r:g>13</r:g>
<r:h>14</r:h>
</LedgerJournalTrans>
</LedgerJournalTable>
</MessageParts>
I tried to use this code for transforming my xml, I was able to transform most of the xml just was getting xmlns="" in each
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:r="http://schemas.microsoft.com/dynamics/2008/01/documents/LedgerGeneralJournal">
<xsl:template match="InvoiceDocument">
<MessageParts
xmlns:r="http://schemas.microsoft.com/dynamics/2008/01/documents/LedgerGeneralJournal">
<LedgerJournalTable class="entity">
<r:z>50</r:z>
<xsl:apply-templates select="Invoice"/>
</LedgerJournalTable>
</MessageParts>
</xsl:template>
<xsl:template match="Invoice">
<LedgerJournalTrans class="entity'>
<r:e><xsl:value-of select="normalize-space(a/text()[1])"/></r:e>
</LedgerJournalTrans>
<xsl:apply-templates select="Products"/>
</xsl:template>
<xsl:template match="Products">
<xsl:apply-templates select="Product"/>
</xsl:template>
<xsl:template match="Product">
<LedgerJournalTrans class="entity'>
<g><xsl:value-of select="normalize-space(b/text()[1])"/></g>
<h><xsl:value-of select="normalize-space(c/text()[1])"/></h>
</LedgerJournalTrans>
</xsl:template>
</xsl:stylesheet>
The output i am getting after applying this xlst is this, there is xmlns="" in the
<MessageParts xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/LedgerGeneralJournal">
<LedgerJournalTable class="entity">
<r:z>50</r:z>
<LedgerJournalTrans class="entity" xmlns="">
<r:e>120</r:e>
</LedgerJournalTrans>
<LedgerJournalTrans class="entity" xmlns="">
<r:g>11</r:g>
<r:h>12</r:h>
</LedgerJournalTrans>
<LedgerJournalTrans class="entity" xmlns="">
<r:g>13</r:g>
<r:h>14</r:h>
</LedgerJournalTrans>
</LedgerJournalTable>
</MessageParts>
So can you help me to remove this xmlns=""
I'm new to XSL and i'm trying to copy a node of XML to another node in the same XML using XSLT. I can able to transform the file as expected but the XMLNS attribute is getting added to the destination which I don't want.
I have tried all the option of copy-namespaces='no' using XSLT2.0 but that doesn't work. Also, I cant have a prefix of namespaces in the XSLT and use exclude namespaces to avoid xmlns because the incoming XML file is dynamic and namespaces keep on changing. I can't have all the namespaces declared as a prefix in XSLT.
Incoming XML:
<?xml version="1.0" encoding="UTF-8"?>
<PropertySet>
<Message>
<NotificationHeader>
<BusinessId></BusinessId>
<CorrelationId>0201201916:21:24CKG3N</CorrelationId>
<SourceName></SourceName>
<SourceId></SourceId>
<EventType></EventType>
<SecurityIdentifierId></SecurityIdentifierId>
<ClientRequestId></ClientRequestId>
<TargetId>ESB</TargetId>
<EventTime></EventTime>
<RequestUser></RequestUser>
</NotificationHeader>
<EventList>
<Event>
<PayLoad>
<ListOfActionIo xmlns="http://www.test.com/IO">
<Action>
<ActivityId>4-309C7WV</ActivityId>
<ActivitySRId></ActivitySRId>
<ActivityTemplateId></ActivityTemplateId>
<ActivityUID>4-309C7WV</ActivityUID>
<Category> Notification</Category>
<Comment></Comment>
<Type>Action</Type>
<ListOfDestination>
<Destination>
<DestinationName>NSW Aboriginal Education Consultative Group Incorporated</DestinationName>
</Destination>
<Destination>
<DestinationName>NSW Aboriginal Education Consultative Group Incorporated</DestinationName>
</Destination>
</ListOfDestination>
</Action>
</ListOfActionIo>
</PayLoad>
</Event>
</EventList>
<DestinationList>
<Destination>
<DestinationName></DestinationName>
<DestinationId></DestinationId>
</Destination>
</DestinationList>
</Message>
</PropertySet>
XSLT Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//*[name()='Message']/*[name()='DestinationList']//*[name()='Destination']"></xsl:template>
<!--Copy the destination from one node to another node-->
<xsl:template match="//*[name()='Message']/*[name()='DestinationList']">
<xsl:copy>
<xsl:copy-of select="//*[name()='Message']/*[name()='EventList']//*[name()='Destination']"/>
</xsl:copy>
</xsl:template>
<!--Remove the original node from where the destination was copied-->
<xsl:template match="//*[name()='Message']/*[name()='EventList']//*[name()='ListOfDestination']"></xsl:template>
</xsl:stylesheet>
Output:
<PropertySet>
<Message>
<NotificationHeader>
<BusinessId/>
<CorrelationId>0201201916:21:24CKG3N</CorrelationId>
<SourceName/>
<SourceId/>
<EventType/>
<SecurityIdentifierId/>
<ClientRequestId/>
<TargetId>ESB</TargetId>
<EventTime/>
<RequestUser/>
</NotificationHeader>
<EventList>
<Event>
<PayLoad>
<ListOfActionIo xmlns="http://www.test.com/IO">
<Action>
<ActivityId>4-309C7WV</ActivityId>
<ActivitySRId/>
<ActivityTemplateId/>
<ActivityUID>4-309C7WV</ActivityUID>
<Category> Notification</Category>
<Comment/>
<Type>Action</Type>
</Action>
</ListOfActionIo>
</PayLoad>
</Event>
</EventList>
<DestinationList>
<Destination xmlns="http://www.test.com/IO">
<DestinationName>NSW Aboriginal Education Consultative Group Incorporated</DestinationName>
</Destination>
<Destination xmlns="http://www.test.com/IO">
<DestinationName>NSW Aboriginal Education Consultative Group Incorporated</DestinationName>
</Destination>
</DestinationList>
</Message>
</PropertySet>
Expected Output:
Without the Namespaces on the copied node.
<PropertySet>
<Message>
<NotificationHeader>
<BusinessId/>
<CorrelationId>0201201916:21:24CKG3N</CorrelationId>
<SourceName/>
<SourceId/>
<EventType/>
<SecurityIdentifierId/>
<ClientRequestId/>
<TargetId>ESB</TargetId>
<EventTime/>
<RequestUser/>
</NotificationHeader>
<EventList>
<Event>
<PayLoad>
<ListOfActionIo xmlns="http://www.test.com/IO">
<Action>
<ActivityId>4-309C7WV</ActivityId>
<ActivitySRId/>
<ActivityTemplateId/>
<ActivityUID>4-309C7WV</ActivityUID>
<Category> Notification</Category>
<Comment/>
<Type>Action</Type>
</Action>
</ListOfActionIo>
</PayLoad>
</Event>
</EventList>
<DestinationList>
<Destination>
<DestinationName>NSW Aboriginal Education Consultative Group Incorporated</DestinationName>
</Destination>
<Destination>
<DestinationName>NSW Aboriginal Education Consultative Group Incorporated</DestinationName>
</Destination>
</DestinationList>
</Message>
</PropertySet>
You are asking the wrong question. You don't want to "remove namespaces" - you want to change the namespace of transformed elements. This is akin to renaming the elements.
Now, since your example is confusing (and too long), consider the following:
XML
<root>
<colors>
<color>red</color>
<color>blue</color>
</colors>
<shapes xmlns="some-unknown-namespace">
<shape>circle</shape>
<shape>square</shape>
</shapes>
<sizes>
<size>small</size>
<size>large</size>
</sizes>
</root>
To change the namespace of shapes (and its descendants, that inherit it!) to no-namespace, without knowing in advance what the original namespace will be, you can do:
XSLT 1.0
<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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[ancestor-or-self::*[name()='shapes']]">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="#*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<root>
<colors>
<color>red</color>
<color>blue</color>
</colors>
<shapes>
<shape>circle</shape>
<shape>square</shape>
</shapes>
<sizes>
<size>small</size>
<size>large</size>
</sizes>
</root>
Added:
lets modify the code to move the shapes into sizes and remove the shapes node.
OK, let's do that:
XSLT 1.0
<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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="sizes">
<xsl:copy>
<xsl:apply-templates/>
<!-- move the shapes into sizes -->
<xsl:apply-templates select="../*[name()='shapes']/*"/>
</xsl:copy>
</xsl:template>
<!-- remove the shapes element -->
<xsl:template match="*[name()='shapes']"/>
<xsl:template match="*[ancestor::*[name()='shapes']]">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="#*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<root>
<colors>
<color>red</color>
<color>blue</color>
</colors>
<sizes>
<size>small</size>
<size>large</size>
<shape>circle</shape>
<shape>square</shape>
</sizes>
</root>
For the below input XML, I am able to group all the child <invoice> elements under the appropriate <shippingBill> element using muenchian grouping method. However there is a requirement to include the count of the number of <shippingBill>elements and the <invoice> elements in the final output XML. I am not sure how to go about it.
Input XML
<bank>
<shippingBills>
<shippingBill>
<shippingBillNo>5786885</shippingBillNo>
<shippingBillDate>10/02/2016</shippingBillDate>
<LEODate>11/02/2016</LEODate>
<invoice>
<invoiceSerialNo>1</invoiceSerialNo>
<invoiceNo>183</invoiceNo>
<invoiceDate>07/02/2016</invoiceDate>
</invoice>
</shippingBill>
<shippingBill>
<shippingBillNo>5786885</shippingBillNo>
<shippingBillDate>10/02/2016</shippingBillDate>
<LEODate>11/02/2016</LEODate>
<invoice>
<invoiceSerialNo>2</invoiceSerialNo>
<invoiceNo>184</invoiceNo>
<invoiceDate>07/02/2016</invoiceDate>
</invoice>
</shippingBill>
<shippingBill>
<shippingBillNo>3318135</shippingBillNo>
<shippingBillDate>01/10/2015</shippingBillDate>
<LEODate>01/10/2015</LEODate>
<invoice>
<invoiceSerialNo>1</invoiceSerialNo>
<invoiceNo>172</invoiceNo>
<invoiceDate>29/09/2015</invoiceDate>
</invoice>
</shippingBill>
<shippingBill>
<shippingBillNo>3318135</shippingBillNo>
<shippingBillDate>01/10/2015</shippingBillDate>
<LEODate>01/10/2015</LEODate>
<invoice>
<invoiceSerialNo>2</invoiceSerialNo>
<invoiceNo>173</invoiceNo>
<invoiceDate>29/09/2015</invoiceDate>
</invoice>
</shippingBill>
</shippingBills>
</bank>
XSL
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="key-bill" match="shippingBill" use="shippingBillNo"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<xsl:template match="shippingBills">
<xsl:copy>
<xsl:apply-templates
select="shippingBill[generate-id() = generate-id(key('key-bill', shippingBillNo)[1])]"
mode="group" />
</xsl:copy>
</xsl:template>
<xsl:template match="shippingBill" mode="group">
<shippingBill>
<xsl:apply-templates select="*[not(self::invoice)]" />
<invoices>
<xsl:apply-templates select="key('key-bill', shippingBillNo)/invoice" />
</invoices>
</shippingBill>
</xsl:template>
</xsl:stylesheet>
Final Output XML Required
The <checkSum> element has been added which has child elements that hold the respective counts.
<bank>
<checkSum>
<noOfInvoices>4</noOfInvoices>
<noOfShippingBills>2</noOfShippingBills>
</checkSum>
<shippingBills>
<shippingBill>
<shippingBillNo>5786885</shippingBillNo>
<shippingBillDate>10/02/2016</shippingBillDate>
<LEODate>11/02/2016</LEODate>
<invoices>
<invoice>
<invoiceSerialNo>1</invoiceSerialNo>
<invoiceNo>183</invoiceNo>
<invoiceDate>07/02/2016</invoiceDate>
</invoice>
<invoice>
<invoiceSerialNo>2</invoiceSerialNo>
<invoiceNo>184</invoiceNo>
<invoiceDate>07/02/2016</invoiceDate>
</invoice>
</invoices>
</shippingBill>
<shippingBill>
<shippingBillNo>3318135</shippingBillNo>
<shippingBillDate>01/10/2015</shippingBillDate>
<LEODate>01/10/2015</LEODate>
<invoices>
<invoice>
<invoiceSerialNo>1</invoiceSerialNo>
<invoiceNo>172</invoiceNo>
<invoiceDate>29/09/2015</invoiceDate>
</invoice>
<invoice>
<invoiceSerialNo>2</invoiceSerialNo>
<invoiceNo>173</invoiceNo>
<invoiceDate>29/09/2015</invoiceDate>
</invoice>
</invoices>
</shippingBill>
</shippingBills>
</bank>
I would do it this way:
XSLT 1.0
<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:strip-space elements="*" />
<xsl:key name="key-bill" match="shippingBill" use="shippingBillNo" />
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="shippingBills">
<xsl:variable name="distinct-bills" select="shippingBill[generate-id() = generate-id(key('key-bill', shippingBillNo)[1])]" />
<checkSum>
<noOfInvoices>
<xsl:value-of select="count(shippingBill/invoice)" />
</noOfInvoices>
<noOfShippingBills>
<xsl:value-of select="count($distinct-bills)" />
</noOfShippingBills>
</checkSum>
<xsl:copy>
<xsl:apply-templates select="$distinct-bills" mode="group" />
</xsl:copy>
</xsl:template>
<xsl:template match="shippingBill" mode="group">
<shippingBill>
<xsl:apply-templates select="*[not(self::invoice)]" />
<invoices>
<xsl:apply-templates select="key('key-bill', shippingBillNo)/invoice" />
</invoices>
</shippingBill>
</xsl:template>
</xsl:stylesheet>
INPUT XML
<root>
<file1>
<commodity>
<units>1</units>
<obj>mango</obj>
</commodity>
<commodity>
<units>5</units>
<obj>guava</obj>
</commodity>
</file1>
<file2>
<category>
<object>guava</object>
<type>CAT1</type>
<colour>green</colour>
</category>
<category>
<object>mango</object>
<type>CAT2</type>
<colour>yellow</colour>
</category>
</file2>
</root>
I need to compare the values of obj in file1 and object in file2 under root, if same I need to take their corresponding units, type and colour and produce the following output using xslt.
OUTPUT XML
<output>
<com>
<name>guava</name>
<num>5</num>
<category>CAT1</category>
<col>green</col>
</com>
<com>
<name>mango</name>
<num>1</num>
<category>CAT2</category>
<col>yellow</col>
</com>
</output>
I tried the below XSLT but the response is not as expected. Its not looping properly. Could you please tell me where I am going wrong.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes" />
<xsl:key name="object-search" match="root/file1/commodity" use="obj" />
<xsl:template match="/">
<output>
<xsl:for-each select="key('object-search', //category/object)">
<com>
<name>
<xsl:value-of select="obj" />
</name>
<num>
<xsl:value-of select="units" />
</num>
<category>
<xsl:value-of
select="//root/file2/category/type" />
</category>
<col>
<xsl:value-of
select="//root/file2/category/colour" />
</col>
</com>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
Try it this way:
XSLT 1.0
<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:key name="cat" match="category" use="object" />
<xsl:template match="/root">
<output>
<xsl:for-each select="file1/commodity">
<com>
<name>
<xsl:value-of select="obj" />
</name>
<num>
<xsl:value-of select="units" />
</num>
<xsl:variable name="cat" select="key('cat', obj)" />
<category>
<xsl:value-of select="$cat/type" />
</category>
<col>
<xsl:value-of select="$cat/colour" />
</col>
</com>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
Note that the result is slightly different from what you posted:
<?xml version="1.0" encoding="UTF-8"?>
<output>
<com>
<name>mango</name>
<num>1</num>
<category>CAT2</category>
<col>yellow</col>
</com>
<com>
<name>guava</name>
<num>5</num>
<category>CAT1</category>
<col>green</col>
</com>
</output>
Alternatively, you could do:
XSLT 1.0
<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:key name="com" match="commodity" use="obj" />
<xsl:template match="/root">
<output>
<xsl:for-each select="file2/category">
<xsl:variable name="com" select="key('com', object)" />
<com>
<name>
<xsl:value-of select="$com/obj" />
</name>
<num>
<xsl:value-of select="$com/units" />
</num>
<category>
<xsl:value-of select="type" />
</category>
<col>
<xsl:value-of select="colour" />
</col>
</com>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
and get:
<?xml version="1.0" encoding="UTF-8"?>
<output>
<com>
<name>guava</name>
<num>5</num>
<category>CAT1</category>
<col>green</col>
</com>
<com>
<name>mango</name>
<num>1</num>
<category>CAT2</category>
<col>yellow</col>
</com>
</output>