如何将具有多个相同元素的XML输入转换为没有相同元素的XML输出

时间:2012-03-19 22:55:05

标签: xml xslt

我尝试将包含多个相同元素的xml输入文件转换为新的xml文件,该文件将所有相同的元素合并为一个。输入文件就像是

<?xml version="1.0"?>
<InputShipmentSchedule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<DataArea>
    <ShipmentSchedule>
        <ShipmentScheduleLine>
            <ManufacturingItem>
                <ItemID>
                    <ID>P313503</ID>
                </ItemID>
            </ManufacturingItem>                
        </ShipmentScheduleLine>
        <ShipmentScheduleLine>
            <ManufacturingItem>
                <ItemID>
                    <ID>P313503</ID>
                </ItemID>
            </ManufacturingItem>            
        </ShipmentScheduleLine>
        <ShipmentScheduleLine>
            <ManufacturingItem>
                <ItemID>
                    <ID>P313504</ID>
                </ItemID>
            </ManufacturingItem>            
        </ShipmentScheduleLine>
        <ShipmentScheduleLine>
            <ManufacturingItem>
                <ItemID>
                    <ID>P313504</ID>
                </ItemID>
            </ManufacturingItem>            
        </ShipmentScheduleLine>
    </ShipmentSchedule>
</DataArea>
</InputShipmentSchedule>

我制作了以下xsl转换文件:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="no" indent="yes"/>
<xsl:preserve-space elements="*"/>
<xsl:template match="InputShipmentSchedule">
    <xsl:call-template name="CreateShipmentScheduleXmlns"/>
</xsl:template>

<xsl:template name="CreateShipmentScheduleXmlns">
    <xsl:element name="Output_Data">
        <xsl:element name="ShipmentSchedule">
                <xsl:call-template name="part_detail_template">
                    <xsl:with-param name="currentPartLine" select="DataArea/ShipmentSchedule/ShipmentScheduleLine"/>
                    <xsl:with-param name="nextPartLine" select="DataArea/ShipmentSchedule/ShipmentScheduleLine/following-sibling::ShipmentScheduleLine"/>
                </xsl:call-template>                
        </xsl:element>  <!-- ShipmentSchedule tag end -->
    </xsl:element>  <!-- Output_Data tag end -->
</xsl:template> <!-- CreateShipmentScheduleXmlns template end -->

<xsl:template name="part_detail_template">
    <xsl:param name="currentPartLine"/>
    <xsl:param name="nextPartLine"/>
    <xsl:element name="Part_Detail">  <!-- Part_Detail tag start -->
        <xsl:variable name="part_no" select="$currentPartLine/ManufacturingItem/ItemID/ID"/>
        <xsl:element name="part_no">
            <xsl:attribute name="value">
                <xsl:value-of select="$part_no"/>
            </xsl:attribute>
        </xsl:element>
    </xsl:element>  <!-- Part_Detail tag end -->
    <xsl:variable name="currentItem" select="$currentPartLine/ManufacturingItem/ItemID/ID"/>
    <xsl:variable name="nextItem" select="$nextPartLine/ManufacturingItem/ItemID/ID"/>
    <xsl:choose>            
        <xsl:when test="$nextPartLine and $nextItem != $currentItem">
            <xsl:call-template name="part_detail_template">
                <xsl:with-param name="currentPartLine" select="$nextPartLine"/>
                <xsl:with-param name="nextPartLine" select="$nextPartLine/following-sibling::ShipmentScheduleLine"/>
            </xsl:call-template>
        </xsl:when>
    </xsl:choose>
</xsl:template>  <!-- part_detail_template tag end -->                                        
</xsl:stylesheet>

但输出xml文件仍包含冗余P313503,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Output_Data>
<ShipmentSchedule>
<Part_Detail>
<part_no value="P313503"/>
</Part_Detail>
<Part_Detail>
<part_no value="P313503"/>
</Part_Detail>
<Part_Detail>
<part_no value="P313504"/>
</Part_Detail>
</ShipmentSchedule>
</Output_Data>

我不知道为什么“part_no”元素(“P313503”)会出现两次。它应该在输出xml文件中包含非冗余的“part_no”元素。我在上面的xsl文件中做错了什么?任何评论或建议将不胜感激。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

                <xsl:with-param name="currentPartLine" 
                select="DataArea/ShipmentSchedule/ShipmentScheduleLine"/>
                <xsl:with-param name="nextPartLine" 
                select="DataArea/ShipmentSchedule/ShipmentScheduleLine/
                                   following-sibling::ShipmentScheduleLine"/>

这两个表达式选择相同的节点集。 DataArea/ShipmentSchedule/ShipmentScheduleLine选择所有 ShipmentScheduleLine元素而不仅仅是第一个,因此添加following-sibling::ShipmentScheduleLine不会选择任何不同的节点。

XSLT2中的分组问题比XSLT1容易得多,但假设由于某种原因你被困在1。 google for“muenchian grouping”将引导您找到这个解决方案:

<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

<xsl:key name="n" match="ItemID" use="ID"/>

<xsl:template match="/">
 <Output_Data>
  <ShipmentSchedule>
   <xsl:for-each select="//ManufacturingItem/ItemID[generate-id()=
             generate-id(key('n',.))]">
    <Part_Detail>
     <part_no value="{.}"/>
    </Part_Detail>
   </xsl:for-each>
  </ShipmentSchedule>
 </Output_Data>
</xsl:template>

</xsl:stylesheet>

产生:

$ saxon man.xml man.xsl
<?xml version="1.0" encoding="utf-8"?>
<Output_Data>
   <ShipmentSchedule>
      <Part_Detail>
         <part_no value="P313503"/>
      </Part_Detail>
      <Part_Detail>
         <part_no value="P313504"/>
      </Part_Detail>
   </ShipmentSchedule>
</Output_Data>