Reputation: 5
First,I don't have good enough experience on xslt. I need help splitting the XML into 2 Files based on InoviceNo. If InvoiceNo = "+" include it in the preceding sibling where InvoiceNo equals a number.
Source File
<?xml version="1.0"?>
<ns:MT_CheckoutReport xmlns:ns="http://test.com/LE/ChannelAdvisor">
<row>
<row>
<InvoiceNo>547194</InvoiceNo>
<CheckoutDate>8/02/2021 19:29</CheckoutDate>
<TotalInvoiceAmt>159.99</TotalInvoiceAmt>
<PymtType>Amazon</PymtType>
<PymtID></PymtID>
<PymtExp>0/0</PymtExp>
<CVV></CVV>
</row>
</row>
<row>
<row>
<InvoiceNo>547195</InvoiceNo>
<CheckoutDate>8/02/2021 19:29</CheckoutDate>
<TotalInvoiceAmt>219.98</TotalInvoiceAmt>
<PymtType>Amazon</PymtType>
<PymtID></PymtID>
<PymtExp>0/0</PymtExp>
<CVV></CVV>
</row>
</row>
<row>
<row>
<InvoiceNo>+</InvoiceNo>
<CheckoutDate></CheckoutDate>
<TotalInvoiceAmt></TotalInvoiceAmt>
<PymtType></PymtType>
<PymtID></PymtID>
<PymtExp></PymtExp>
<CVV></CVV>
</row>
</row>
</ns:MT_CheckoutReport>
Desired Output. File 1
<?xml version="1.0"?>
<ns:MT_CheckoutReport xmlns:ns="http://test.com/LE/ChannelAdvisor">
<row>
<row>
<InvoiceNo>547194</InvoiceNo>
<CheckoutDate>8/02/2021 19:29</CheckoutDate>
<TotalInvoiceAmt>159.99</TotalInvoiceAmt>
<PymtType>Amazon</PymtType>
<PymtID></PymtID>
<PymtExp>0/0</PymtExp>
<CVV></CVV>
</row>
</row>
File 2. InvoiceNo = + Include it in the file with preceeding sibling where the InvoiceNo = 547195
<?xml version="1.0"?>
<ns:MT_CheckoutReport xmlns:ns="http://test.com/LE/ChannelAdvisor">
<row>
<row>
<InvoiceNo>547195</InvoiceNo>
<CheckoutDate>8/02/2021 19:29</CheckoutDate>
<TotalInvoiceAmt>219.98</TotalInvoiceAmt>
<PymtType>Amazon</PymtType>
<PymtID></PymtID>
<PymtExp>0/0</PymtExp>
<CVV></CVV>
</row>
</row>
<row>
<row>
<InvoiceNo>+</InvoiceNo>
<CheckoutDate></CheckoutDate>
<TotalInvoiceAmt></TotalInvoiceAmt>
<PymtType></PymtType>
<PymtID></PymtID>
<PymtExp></PymtExp>
<CVV></CVV>
</row>
</row>
My attempt
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="row">
<xsl:for-each-group select="row/InvoiceNo" group-by="InvoiceNo">
<xsl:result-document href="file_{current-grouping-key()}.xml">
<some>
<xsl:copy-of select="current-group()"/>
</some>
</xsl:result-document>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Views: 162
Reputation: 117073
Why not simply:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http://test.com/LE/ChannelAdvisor">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/ns:MT_CheckoutReport">
<xsl:for-each-group select="row" group-starting-with="row[row/InvoiceNo!='+']">
<xsl:result-document href="file_{row/InvoiceNo}.xml">
<ns:MT_CheckoutReport>
<xsl:copy-of select="current-group()"/>
</ns:MT_CheckoutReport>
</xsl:result-document>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2