本文介绍了XSLT 根据条件拆分 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
首先,我对 xslt 的经验不够好.我需要帮助将 XML 拆分为基于 InoviceNo 的 2 个文件.如果 InvoiceNo = +"将它包含在前面的同级中,其中 InvoiceNo 等于一个数字.
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.
源文件
<?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
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>
我的尝试
<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 根据条件拆分 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!