我有一个下面的xml示例文件,有大量的记录。我想根据头/测试簿id将其分成两个failedItems。生成的文件应该有价格表/价格表/@产品id和价格表/价格表/金额。
我面临的挑战是吐出文件并选择我需要的列。任何帮助都是很大的帮助。
<?xml version="1.0" encoding="UTF-8"?>
<testbooks xmlns="http://www.demandware.com/xml/impex/testbook/2006-10-31">
<testbook>
<header testbook-id="ca-cad-list">
<currency>CAD</currency>
<online-flag>true</online-flag>
</header>
<price-tables>
<price-table product-id="035:M:1:">
<online-from>2022-06-02T00:00:00.000Z</online-from>
<online-to>9999-12-31T23:59:59.000Z</online-to>
<amount quantity="1">90.00 </amount>
</price-table>
</price-tables>
</testbook>
<testbook>
<header testbook-id="ca-cad-sale">
<currency>CAD</currency>
<online-flag>true</online-flag>
</header>
<price-tables>
<price-table product-id="035:M:1:">
<online-from>2022-06-02T00:00:00.000Z</online-from>
<online-to>9999-12-31T23:59:59.000Z</online-to>
<amount quantity="1">85</amount>
</price-table>
</price-tables>
</testbook>
</testbooks>发布于 2022-06-04 10:59:41
在XSLT 3中,您可以使用分组,然后使用xsl:result-document,很明显,加上空的模板,以避免在输出中复制您不想要的数据。
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xpath-default-namespace="http://www.demandware.com/xml/impex/testbook/2006-10-31"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:template match="testbooks">
<xsl:for-each-group select="testbook" group-by="header/@testbook-id">
<xsl:result-document href="{current-grouping-key()}.xml">
<xsl:copy select="..">
<xsl:apply-templates select="current-group()"/>
</xsl:copy>
</xsl:result-document>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="price-table/online-from | price-table/online-to"/>
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
</xsl:stylesheet>这假设您只想根据header/@testbook-id进行拆分,但保留testbook元素。当然,分组还允许您加入/合并属于一个组的元素。
https://stackoverflow.com/questions/72496506
复制相似问题