我有一个复杂的XML,它有一个头部分和多个项部分。XML如下:
<Order>
<OrderNumber>A</OrderNumber>
<SubTotal>20.00</SubTotal>
<Tax>
<TaxCode>AA</TaxCode>
<TaxAmount>1.00</TaxAmount>
</Tax>
<TotalAmount>21.00</TotalAmount>
<ItemSection>
<SectionNumber>1</SectionNumber>
<Item>
<Product>
<LineNumber>10</LineNumber>
<IsConfig>Y</IsConfig>
<Config>
<Product>
<LineNumber>10-1</LineNumber>
<IsConfig>Y</IsConfig>
<Config>
<Product>
<LineNumber>10-1-1</LineNumber>
<IsConfig>N</IsConfig>
<LineTotal>2.00</LineTotal>
</Product>
<Product>
<LineNumber>10-1-2</LineNumber>
<IsConfig>N</IsConfig>
<LineTotal>1.00</LineTotal>
</Product>
</Config>
<LineITotal>1.00</LineITotal>
</Product>
<Product>
<LineNumber>10-2</LineNumber>
<IsConfig>N</IsConfig>
<LineITotal>4.00</LineITotal>
</Product>
</Config>
<LineITotal>1.00</LineITotal>
</Product>
</Item>
<Item>
<Product>
<LineNumber>20</LineNumber>
<IsConfig>N</IsConfig>
<LineITotal>1.00</LineITotal>
</Product>
</Item>
</ItemSection>
<ItemSection>
<SectionNumber>2</SectionNumber>
<Item>
<Product>
<LineNumber>30</LineNumber>
<IsConfig>Y</IsConfig>
<Config>
<Product>
<LineNumber>30-1</LineNumber>
<IsConfig>Y</IsConfig>
<Config>
<Product>
<LineNumber>30-1-1</LineNumber>
<IsConfig>N</IsConfig>
<LineTotal>2.00</LineTotal>
</Product>
<Product>
<LineNumber>30-1-2</LineNumber>
<IsConfig>N</IsConfig>
<LineTotal>1.00</LineTotal>
</Product>
</Config>
<LineITotal>1.00</LineITotal>
</Product>
<Product>
<LineNumber>30-2</LineNumber>
<IsConfig>N</IsConfig>
<LineITotal>4.00</LineITotal>
</Product>
</Config>
<LineITotal>1.00</LineITotal>
</Product>
</Item>
<Item>
<Product>
<LineNumber>40</LineNumber>
<IsConfig>N</IsConfig>
<LineITotal>1.00</LineITotal>
</Product>
</Item>
</ItemSection>
我需要压平结构并移动所有的产品块,无论它们出现在哪个级别,并将它们移动到与项目/产品相同的级别,如下所示。在执行此操作时,我需要将IsConfig的值更改为N,并删除围绕Product的Config元素。同时,我需要复制ItemSection上面的所有元素。请注意,配置/产品块可能有N个级别。结果应该是这样的:
<Order>
<OrderNumber>A</OrderNumber>
<SubTotal>10.00</SubTotal>
<Tax>
<TaxCode>AA</TaxCode>
<TaxAmount>1.00</TaxAmount>
</Tax>
<TotalAmount>21.00</TotalAmount>
<ItemSection>
<SectionNumber>1</SectionNumber>
<Item>
<Product>
<LineNumber>10</LineNumber>
<IsConfig>N</IsConfig>
<LineITotal>1.00</LineITotal>
</Product>
<Product>
<LineNumber>10-1</LineNumber>
<IsConfig>N</IsConfig>
<LineITotal>1.00</LineITotal>
</Product>
<Product>
<LineNumber>10-1-1</LineNumber>
<IsConfig>N</IsConfig>
<LineTotal>2.00</LineTotal>
</Product>
<Product>
<LineNumber>10-1-2</LineNumber>
<IsConfig>N</IsConfig>
<LineTotal>1.00</LineTotal>
</Product>
<Product>
<LineNumber>10-2</LineNumber>
<IsConfig>N</IsConfig>
<LineITotal>4.00</LineITotal>
</Product>
</Item>
<Item>
<Product>
<LineNumber>20</LineNumber>
<IsConfig>N</IsConfig>
<LineITotal>1.00</LineITotal>
</Product>
</Item>
</ItemSection>
<ItemSection>
<SectionNumber>2</SectionNumber>
<Item>
<Product>
<LineNumber>30</LineNumber>
<IsConfig>N</IsConfig>
<LineITotal>1.00</LineITotal>
</Product>
<Product>
<LineNumber>30-1</LineNumber>
<IsConfig>N</IsConfig>
<LineITotal>1.00</LineITotal>
</Product>
<Product>
<LineNumber>30-1-1</LineNumber>
<IsConfig>N</IsConfig>
<LineTotal>2.00</LineTotal>
</Product>
<Product>
<LineNumber>30-1-2</LineNumber>
<IsConfig>N</IsConfig>
<LineTotal>1.00</LineTotal>
</Product>
<Product>
<LineNumber>30-2</LineNumber>
<IsConfig>N</IsConfig>
<LineITotal>4.00</LineITotal>
</Product>
</Item>
<Item>
<Product>
<LineNumber>40</LineNumber>
<IsConfig>N</IsConfig>
<LineITotal>1.00</LineITotal>
</Product>
</Item>
</ItemSection>
这是实际XML的一个简单版本——实际的XML有十亿个元素,与行号处于同一级别,需要移动。我知道如何使用暴力并逐个复制每个字段,并且可以使用行项目部分的递归模板,但我希望在这里获得一些指针,以获得更有效的方法。谢谢
尝试以下样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Product">
<xsl:copy>
<xsl:apply-templates select="*[not(self::Config)]"/>
</xsl:copy>
<xsl:apply-templates select="Config"/>
</xsl:template>
<xsl:template match="Config">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
输出与您的要求一样精确。