我是XSLT的新手,任何人都可以帮助我进行以下转换吗
这是我的输入XML:
<sent_items>
<item>
<RECEIPT_NUM>1</RECEIPT_NUM>
<SHIPMENT_NUM>1</SHIPMENT_NUM>
<ITEM_NUMBER>10</ITEM_NUMBER>
</item>
<item>
<RECEIPT_NUM>2</RECEIPT_NUM>
<SHIPMENT_NUM>2</SHIPMENT_NUM>
<ITEM_NUMBER>100</ITEM_NUMBER>
</item>
<item>
<RECEIPT_NUM>1</RECEIPT_NUM>
<SHIPMENT_NUM>1</SHIPMENT_NUM>
<ITEM_NUMBER>20</ITEM_NUMBER>
</item>
<item>
<RECEIPT_NUM>2</RECEIPT_NUM>
<SHIPMENT_NUM>2</SHIPMENT_NUM>
<ITEM_NUMBER>30</ITEM_NUMBER>
</item>
<item>
<RECEIPT_NUM>3</RECEIPT_NUM>
<SHIPMENT_NUM>3</SHIPMENT_NUM>
<ITEM_NUMBER>40</ITEM_NUMBER>
</item>
</sent_items>
这是我的输出XML:即,如果我们在RECEIPT_NUM
和SHIPMENT_NUM
中具有相同的值,则在同一个item
节点中添加多个ITEM_NUMBER
节点。
<recived_items>
<item>
<RECEIPT_NUM>1</RECEIPT_NUM>
<SHIPMENT_NUM>1</SHIPMENT_NUM>
<ITEM_NUMBER>10</ITEM_NUMBER>
<ITEM_NUMBER>20</ITEM_NUMBER>
</item>
<item>
<RECEIPT_NUM>2</RECEIPT_NUM>
<SHIPMENT_NUM>2</SHIPMENT_NUM>
<ITEM_NUMBER>100</ITEM_NUMBER>
<ITEM_NUMBER>30</ITEM_NUMBER>
</item>
<item>
<RECEIPT_NUM>3</RECEIPT_NUM>
<SHIPMENT_NUM>3</SHIPMENT_NUM>
<ITEM_NUMBER>40</ITEM_NUMBER>
</item>
</recived_items>
我在下面的两个例子中分别使用了,但这两个例子都没有给出预期的输出。1) XSLT:
<recived_items>
<xsl:for-each select="/sent_items/item">
<item>
<RECEIPT_NUM><xsl:value-of select="RECEIPT_NUM"/></RECEIPT_NUM>
<SHIPMENT_NUM><xsl:value-of select="SHIPMENT_NUM"/></SHIPMENT_NUM>
<ITEM_NUMBER><xsl:value-of select="ITEM_NUMBER"/></ITEM_NUMBER>
</item>
</xsl:for-each>
</recived_items>
2) XSLT:
<recived_items>
<item>
<RECEIPT_NUM><xsl:value-of select="RECEIPT_NUM"/></RECEIPT_NUM>
<SHIPMENT_NUM><xsl:value-of select="SHIPMENT_NUM"/></SHIPMENT_NUM>
<xsl:for-each select="/sent_items/item">
<ITEM_NUMBER><xsl:value-of select="ITEM_NUMBER"/></ITEM_NUMBER>
</xsl:for-each>
</item>
</recived_items>
请帮助我编写XSLT,它将提供预期的输出XML。
这是一个分组问题,在XSLT1.0中使用Muenchian分组解决
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="group" match="item" use="concat(RECEIPT_NUM, '|', SHIPMENT_NUM)"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="item[generate-id() = generate-id(key('group', concat(RECEIPT_NUM, '|', SHIPMENT_NUM))[1])]">
<xsl:copy>
<xsl:copy-of select="* | key('group', concat(RECEIPT_NUM, '|', SHIPMENT_NUM))[position() > 1]/ITEM_NUMBER"/>
</xsl:copy>
</xsl:template>
<xsl:template match="item[not(generate-id() = generate-id(key('group', concat(RECEIPT_NUM, '|', SHIPMENT_NUM))[1]))]"/>
</xsl:stylesheet>