删除XSLT中的重复节点

时间:2016-07-06 08:59:22

标签: xml xslt xslt-1.0

我想写一个XSLT,我可以在翻译时删除重复的节点。 在下面的消息中,因为温度代码对于1和3节点是相同的,所以对我来说它是重复的。

我正在使用的示例输入消息是

    <document>
    <party>
        <gtin>1000909090</gtin>
        <pos>
            <attrGroupMany name="temperatureInformation">
                <row>
                    <attr name="temperatureCode">STORADE</attr>
                    <attrQual name="maximumTemperature" qual="FAH">80</attrQual>
                    <attrQual name="minimumTemperature" qual="ABC">10</attrQual>
                </row>
                <row>
                    <attr name="temperatureCode">HANDLING</attr>
                    <attrQual name="maximumTemperature" qual="XYZ">20</attrQual>
                    <attrQual name="minimumTemperature" qual="PQR">30</attrQual>
                </row>
                <row>
                    <attr name="temperatureCode">STORADE</attr>
                    <attrQual name="maximumTemperature" qual="FAH">80</attrQual>
                    <attrQual name="minimumTemperature" qual="ABC">10</attrQual>
                </row>
            </attrGroupMany>
        </pos>
    </party>
    </document>

我正在使用Below XSLT转换示例消息

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

    <xsl:output indent="yes"/>


    <xsl:template match="document"> 
        <CatalogItem>
            <RelationshipData>
                <Relationship>
                    <RelationType>temperatureInformation_details</RelationType>  
                    <RelatedItems>      
                        <xsl:for-each select="party/pos/attrGroupMany[@name ='temperatureInformation']/row">                        
                            <RelatedItem>
                                <xsl:attribute name="referenceKey">
                                    <xsl:value-of select="concat('temperatureInformation_details','-',attr[@name='temperatureCode'],'-',attrQual/@name,'-',attrQual/@qual,'-',attrQual    )"/>
                                </xsl:attribute>
                            </RelatedItem>
                        </xsl:for-each>

                    </RelatedItems>
                </Relationship>
            </RelationshipData>
        </CatalogItem>

    </xsl:template> 

    </xsl:stylesheet>

但是当前输出不正确,它具有STORADE的重复值

    <?xml version="1.0" encoding="UTF-8"?>
    <CatalogItem>
    <RelationshipData>
      <Relationship>
         <RelationType>temperatureInformation_details</RelationType>
         <RelatedItems>
            <RelatedItem referenceKey="temperatureInformation_details-STORADE-maximumTemperature-FAH-80" />
            <RelatedItem referenceKey="temperatureInformation_details-HANDLING-maximumTemperature-XYZ-20" />
            <RelatedItem referenceKey="temperatureInformation_details-STORADE-maximumTemperature-FAH-80" />
         </RelatedItems>
</Relationship>
    </RelationshipData>
    </CatalogItem>

正确的输出应如下所示,其中删除了重复的温度代码。

     <?xml version="1.0" encoding="UTF-8"?>
    <CatalogItem>
    <RelationshipData>
      <Relationship>
         <RelationType>temperatureInformation_details</RelationType>
         <RelatedItems>
            <RelatedItem referenceKey="temperatureInformation_details-STORADE-maximumTemperature-FAH-80" />
            <RelatedItem referenceKey="temperatureInformation_details-HANDLING-maximumTemperature-XYZ-20" />

         </RelatedItems>
      </Relationship>
    </RelationshipData>
    </CatalogItem>

任何输入都会非常有用

1 个答案:

答案 0 :(得分:1)

使用Muenchian分组来识别重复项,密钥可以是

<xsl:key name="group" match="row" use="concat(attr[@name = 'temperatureCode'], '-', attrQual[@qual = 'FAH'])"/>

然后改变

                    <xsl:for-each select="party/pos/attrGroupMany[@name ='temperatureInformation']/row">                        

                    <xsl:for-each select="party/pos/attrGroupMany[@name ='temperatureInformation']/row[generate-id() = generate-id(key('group', concat(attr[@name = 'temperatureCode'], '-', attrQual[@qual = 'FAH']))[1])]">