XSLT1.0中从平面XML到嵌套XML的转换



我有下面的平面xml结构

<?xml version="1.0" encoding="UTF-8"?>
<ns0:Main xmlns:ns0="urn:test">
<RECORDSET>
<Item>
<Number>00011111</Number>
<rowNumber>00001</rowNumber>
<Product>Product 1</Product>
</Item>
<Item>
<Number>00011111</Number>
<rowNumber>00002</rowNumber>
<Product>Product 2</Product>
</Item>
<Item>
<Number>00099999</Number>
<rowNumber>00001</rowNumber>
<Product>Product 3</Product>
</Item>
<Tax>
<Number>00011111</Number>
<taxRowNumber>00001</taxRowNumber>
<TaxType>TAX1</TaxType>
<Amount>100</Amount>
</Tax>
<Tax>
<Number>00011111</Number>
<taxRowNumber>00001</taxRowNumber>
<TaxType>TAX2</TaxType>
<Amount>200</Amount>
</Tax>
<Tax>
<Number>00099999</Number>
<taxRowNumber>00001</taxRowNumber>
<TaxType>TAX2</TaxType>
<Amount>110</Amount>
</Tax>
</RECORDSET>
</ns0:Main>

我需要以下结果。每个<Tax>元素必须在正确的<Item>(具有相同NumberrowNumber信息的元素(内移动

<?xml version="1.0" encoding="UTF-8"?>
<ns0:Main xmlns:ns0="urn:test">
<RECORDSET>
<Item>
<Number>00011111</Number>
<rowNumber>00001</rowNumber>
<Product>Product 1</Product>
<Tax>
<Number>00011111</Number>
<taxRowNumber>00001</taxRowNumber>
<TaxType>TAX1</TaxType>
<Amount>100</Amount>
</Tax>
<Tax>
<Number>00011111</Number>
<taxRowNumber>00001</taxRowNumber>
<TaxType>TAX2</TaxType>
<Amount>200</Amount>
</Tax>
</Item>
<Item>
<Number>00011111</Number>
<rowNumber>00002</rowNumber>
<Product>Product 2</Product>
</Item>
<Item>
<Number>00099999</Number>
<rowNumber>00001</rowNumber>
<Product>Product 3</Product>
<Tax>
<Number>00099999</Number>
<taxRowNumber>00001</taxRowNumber>
<TaxType>TAX2</TaxType>
<Amount>110</Amount>
</Tax>
</Item>
</RECORDSET>
</ns0:Main>

我认为使用以下XSLT可以很容易地获得所需内容(XSLT 1.0版是必须的(,但<Tax>元素从未出现。

我尝试了很多改变,但没有一个是正确的。

我还发现了很多"从平面到嵌套的XML"的问题,但运气不佳

<?xml version="1.0"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="urn:test">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<ns0:Main>
<RECORDSET>
<xsl:for-each select="ns0:Main/RECORDSET/Item">
<Item>
<xsl:for-each select="Number">
<Number>
<xsl:value-of select="."/>
</Number>
</xsl:for-each>
<xsl:for-each select="rowNumber">
<rowNumber>
<xsl:value-of select="."/>
</rowNumber>
</xsl:for-each>
<xsl:for-each select="Product">
<Product>
<xsl:value-of select="."/>
</Product>
</xsl:for-each>
<xsl:variable name="KeyNumber" select="Number"/>
<xsl:variable name="KeyrowNumber" select="rowNumber"/>
<xsl:for-each select="ns0:Main/RECORDSET/Tax[ Number    = $KeyNumber and
taxRowNumber = $KeyrowNumber ]">
<Tax>
<xsl:copy-of select="./*"/>
</Tax>
</xsl:for-each>
</Item>
</xsl:for-each>
</RECORDSET>
</ns0:Main>
</xsl:template>
</xsl:transform>

非常感谢你的帮助。

我建议您使用来检索匹配的Tax节点。尝试:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="tax" match="Tax" use="concat(Number, '|', taxRowNumber)" />
<xsl:template match="/*">
<xsl:copy>
<RECORDSET>
<xsl:for-each select="RECORDSET/Item">
<xsl:copy>
<xsl:copy-of select="*"/>
<xsl:copy-of select="key('tax', concat(Number, '|', rowNumber))"/>
</xsl:copy>
</xsl:for-each>
</RECORDSET>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

相关内容

  • 没有找到相关文章

最新更新