2013-07-12 22 views
0

我有一個XSLT問題需要解決,對於XSLT來說相當新穎,這裏的XSLT專家非常棒,但是我無法爲我找到確切的解決方案問題,我需要消除重複的書籍&重複在我的情況是一個確切的書本類型+書名。但我不想將複製應用於任何其他節點,如CD或父節點內的任何其他節點,節點將保持不同,在某些示例中,我們甚至沒有單個節點。我將如何限制重複只適用於書籍節點。我一直在嘗試從輸入輸出視圖瞭解到這一點,我可能會錯過實際轉換髮生的情況,任何幫助都會有很大幫助XSLT 1.0用於消除特定子節點類型的重複項並排除其他的

已經感謝!

XML:

<ListOfRowIDWithListOfBooks xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/"> 
    <RowIDWithListOfBooks> 
    <ListOfBookInfo> 
     <book> 
     <BookType>Brand</BookType> 
     <BookName>jon</BookName> 
     </book> 
     <book> 
     <BookType>Brand</BookType> 
     <BookName>jon</BookName> 
     </book> 
     <CD> 
     <CDType>Country</CDType> 
     <CDName>MaxStar</CDName> 
     </CD> 
    </ListOfBookInfo> 
    </RowIDWithListOfBooks> 
</ListOfRowIDWithListOfBooks> 

XSLT: I have developed so far, Dimitre, thanks much sir! u have been a great help 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <!--Key--> 
    <xsl:key name="k-books" match="book" use="concat(BookType,'|',BookName)"/> 
    <!--Global match template--> 
    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 
    <!--Eliminate duplicate book nodes template--> 
    <xsl:template match="ListOfBookInfo"> 
      <xsl:copy> 
       <xsl:apply-templates select="book 
       [generate-id() 
       =generate-id(key('k-books',concat(BookType,'|',BookName))[1])]"/> 
      </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet>  

輸出:

<?xml version="1.0"?> 
<ListOfRowIDWithListOfBooks xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/"> 
<RowIDWithListOfBooks> 
<ListOfBookInfo><book> 
<BookType>Brand</BookType> 
<BookName>jon</BookName> 
</book></ListOfBookInfo> 
</RowIDWithListOfBooks> 
</ListOfRowIDWithListOfBooks> 

所需的輸出:

<?xml version="1.0"?> 
<ListOfRowIDWithListOfBooks xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/"> 
<RowIDWithListOfBooks> 
<ListOfBookInfo> 
<book> 
<BookType>Brand</BookType> 
<BookName>jon</BookName> 
</book> 
<CD> 
<CDType>Country</CDType> 
<CDName>MaxStar</CDName> 
</CD> 
</ListOfBookInfo> 
</RowIDWithListOfBooks> 
</ListOfRowIDWithListOfBooks> 

回答

1

你一定不要忘記在ListOfBookInfo可能出現的其他元素應用模板。

<xsl:template match="ListOfBookInfo"> 
    <xsl:copy> 
     <xsl:apply-templates select="book 
      [generate-id() 
      =generate-id(key('k-books',concat(BookType,'|',BookName))[1])]"/> 
     <!-- Apply templates for other element than book --> 
     <xsl:apply-templates select="@* | node()[name() != 'book']" /> 

    </xsl:copy> 
</xsl:template> 
+0

謝謝Jirka,工作完美! –