2015-10-19 49 views
0

我只是沒有得到如何複製在XSL中工作...我有一些重複節點的文檔,我想排除它們。複製xml文檔,不包括重複節點

我的文件看起來是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<catalog> 
    <cd id="ab" lang="en" version="1"> 
     <title>Empire Burlesque</title> 
     <artist>Bob Dylan</artist> 
     <country>USA</country> 
     <company>Columbia</company> 
     <price>10.90</price> 
     <year>1985</year> 
    </cd> 
    <cd id="ab" lang="en" version="1"> 
     <title>Hide your heart</title> 
     <artist>Bonnie Tyler</artist> 
     <country>UK</country> 
     <company>CBS Records</company> 
     <price>9.90</price> 
     <year>1988</year> 
    </cd> 
    <cd id="cd" lang="en" version="1"> 
     <title>Greatest Hits</title> 
     <artist>Dolly Parton</artist> 
     <country>USA</country> 
     <company>RCA</company> 
     <price>9.90</price> 
     <year>1982</year> 
    </cd> 
    <cd id="cd" lang="en" version="1"> 
     <title>Still got the blues</title> 
     <artist>Gary Moore</artist> 
     <country>UK</country> 
     <company>Virgin records</company> 
     <price>10.20</price> 
     <year>1990</year> 
    </cd> 
    <cd id="de" lang="en" version="2"> 
     <title>Eros</title> 
     <artist>Eros Ramazzotti</artist> 
     <country>EU</country> 
     <company>BMG</company> 
     <price>9.90</price> 
     <year>1997</year> 
    </cd> 
</catalog> 

所以它有一堆用相同的ID,版本和郎元素。我想要做的就是複製整個文檔,但每個節點只有一次...

我該怎麼做?備份?複製?換各個羣組?組合?

我想這樣的輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<catalog> 
    <cd id="ab" lang="en" version="1"> 
     <title>Hide your heart</title> 
     <artist>Bonnie Tyler</artist> 
     <country>UK</country> 
     <company>CBS Records</company> 
     <price>9.90</price> 
     <year>1988</year> 
    </cd> 
    <cd id="cd" lang="en" version="1"> 
     <title>Still got the blues</title> 
     <artist>Gary Moore</artist> 
     <country>UK</country> 
     <company>Virgin records</company> 
     <price>10.20</price> 
     <year>1990</year> 
    </cd> 
    <cd id="de" lang="en" version="2"> 
     <title>Eros</title> 
     <artist>Eros Ramazzotti</artist> 
     <country>EU</country> 
     <company>BMG</company> 
     <price>9.90</price> 
     <year>1997</year> 
    </cd> 
</catalog> 
+0

您使用的是XSLT 1.0還是2.0? –

回答

1

它是一個簡單的問題分組,這裏是一個XSLT 3.0溶液(需要撒克遜9.6 PE或EE)使用for-each-group和複合鍵:

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

<xsl:output indent="yes"/> 

<xsl:template match="catalog"> 
    <xsl:copy> 
     <xsl:for-each-group select="cd" group-by="@id, @lang, @version" composite="yes"> 
      <xsl:copy-of select="current-group()[last()]"/> 
     </xsl:for-each-group> 
    </xsl:copy> 
</xsl:template> 

</xsl:transform> 

使用XSLT 2.0,您可以計算單個鍵,並將三個屬性值連接起來,並用屬性值中不存在的分隔符分隔。

+0

它是這個xsl:copy ...我真的不知道如何工作...所以用xsl:copy我必須寫xsl:copy,然後在裏面指定我想在複製的節點上應用的任何更改... ? – user3813234

+0

'xsl:copy'製作'catalog'元素的淺拷貝,然後'xsl:for-each-group'和'xsl:copy-of'裏面填充'catalog',並且最後一個拷貝在每個組中找到'cd'元素。 –