XSLT 根据条件拆分 XML 个文件
XSLT Split XML Files Based on Condition
首先,我对xslt的体验还不够好。我需要帮助根据 InoviceNo 将 XML 拆分为 2 个文件。如果 InvoiceNo = "+" 将其包含在前面的同级中,其中 InvoiceNo 等于一个数字。
源文件
<?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>
期望的输出。
文件 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>
文件 2.InvoiceNo = + 将其包含在 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>
我的尝试
<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>
为什么不简单:
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>
首先,我对xslt的体验还不够好。我需要帮助根据 InoviceNo 将 XML 拆分为 2 个文件。如果 InvoiceNo = "+" 将其包含在前面的同级中,其中 InvoiceNo 等于一个数字。
源文件
<?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>
期望的输出。 文件 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>
文件 2.InvoiceNo = + 将其包含在 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>
我的尝试
<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>
为什么不简单:
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>