2016-07-06 191 views
1

我想編寫一個XSLT,在該XSLT中我可以在翻譯時刪除重複節點。 在下面的消息中,由於溫度代碼對於1節點和3節點是相同的,所以對我來說是重複的。刪除XSLT中的重複節點

其中我使用

樣品輸入消息是

<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> 

我使用下面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> 

正確的輸出應該如下,其中重複的溫度Erate代碼被刪除。

 <?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> 

任何投入將是非常有益的

+0

從這裏開始:http://www.jenitennison.com/xslt/grouping/muenchian.html然後在SO上查找一些Muenchian分組的例子。 –

回答

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])]">