2014-12-03 26 views
0

我有以下xml並希望生成一個只包含GENRE_1和GENRE_3以及任何其他書籍ID的輸出。這意味着GENRE_4,5和6將被剝離。我曾嘗試使用示例xslt,但沒有把它做對。將感謝任何幫助。如何使用XSLT去除特定節點

<bookstore xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<Payload xmlns:ns0="http://www.themindelectric.com/"> 
    <books xmlns:ns0="http://www.themindelectric.com/collections/"> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_1</id> 
      <title lang="en">Everyday Italian</title> 
      <author>Giada De Laurentiis</author> 
      <year>2001</year> 
      <price>30.00</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_3</id> 
      <title lang="en">Harry Potter</title> 
      <author>J K. Rowling</author> 
      <year>2003</year> 
      <price>29.99</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>TEST_3</id> 
      <title lang="en">Harry Potter</title> 
      <author>J K. Rowling</author> 
      <year>2003</year> 
      <price>29.99</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>ANOTHER_1</id> 
      <title lang="en">Harry Potter</title> 
      <author>J K. Rowling</author> 
      <year>2003</year> 
      <price>29.99</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_5</id> 
      <title lang="en">XQuery Kick Start</title> 
      <author>James McGovern</author> 
      <author>Per Bothner</author> 
      <author>Kurt Cagle</author> 
      <author>James Linn</author> 
      <author>Vaidyanathan Nagarajan</author> 
      <year>2003</year> 
      <price>49.99</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_1</id> 
      <title lang="en">Learning XML</title> 
      <author>Erik T. Ray</author> 
      <year>2005</year> 
      <price>39.95</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_1</id> 
      <title lang="en">XQuery Kick Start</title> 
      <author>James McGovern</author> 
      <author>Per Bothner</author> 
      <author>Kurt Cagle</author> 
      <author>James Linn</author> 
      <author>Vaidyanathan Nagarajan</author> 
      <year>2007</year> 
      <price>49.99</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_6</id> 
      <title lang="en">Learning Java</title> 
      <author>Testing</author> 
      <year>2005</year> 
      <price>39.95</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_4</id> 
      <title lang="en">XQuery Kick Start</title> 
      <author>James McGovern</author> 
      <author>Per Bothner</author> 
      <author>Kurt Cagle</author> 
      <author>James Linn</author> 
      <author>Vaidyanathan Nagarajan</author> 
      <year>2007</year> 
      <price>49.99</price> 
     </book> 
    </books> 
</Payload> 

預期輸出

<bookstore xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<Payload xmlns:ns0="http://www.themindelectric.com/"> 
    <books xmlns:ns0="http://www.themindelectric.com/collections/"> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_1</id> 
      <title lang="en">Everyday Italian</title> 
      <author>Giada De Laurentiis</author> 
      <year>2001</year> 
      <price>30.00</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_3</id> 
      <title lang="en">Harry Potter</title> 
      <author>J K. Rowling</author> 
      <year>2003</year> 
      <price>29.99</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>TEST_3</id> 
      <title lang="en">Harry Potter</title> 
      <author>J K. Rowling</author> 
      <year>2003</year> 
      <price>29.99</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>ANOTHER_1</id> 
      <title lang="en">Harry Potter</title> 
      <author>J K. Rowling</author> 
      <year>2003</year> 
      <price>29.99</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_1</id> 
      <title lang="en">Learning XML</title> 
      <author>Erik T. Ray</author> 
      <year>2005</year> 
      <price>39.95</price> 
     </book> 
     <book xmlns:ns0="http://www.themindelectric.com"> 
      <id>GENRE_1</id> 
      <title lang="en">XQuery Kick Start</title> 
      <author>James McGovern</author> 
      <author>Per Bothner</author> 
      <author>Kurt Cagle</author> 
      <author>James Linn</author> 
      <author>Vaidyanathan Nagarajan</author> 
      <year>2007</year> 
      <price>49.99</price> 
     </book> 

    </books> 
</Payload> 

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ns0="http://www.themindelectric.com"> 

<xsl:template match="node() | @*"> 
     <xsl:copy> 
     <xsl:apply-templates select="node() | @*" /> 
    </xsl:copy> 
</xsl:template> 
<xsl:template match="/bookstore/Payload/books/book[starts-with(id,'GENRE')]"> 
<xsl:call-template name="genre"/> 
</xsl:template> 
<xsl:template name="genre"> 
<xsl:choose> 
    <xsl:when 
     test="count(/bookstore/Payload/books/book[id='GENRE_1']) != 0 or 
count(/bookstore/Payload/books/book[id='GENRE_3']) != 0"> 
     <xsl:copy> 
      <xsl:apply-templates select="node() | @*" /> 
     </xsl:copy> 
    </xsl:when> 
    <xsl:otherwise /> 
</xsl:choose> 
</xsl:template> 
</xsl:stylesheet> 

回答

0

如果我理解正確的話你的新的要求,要排除任何以GENRE開始,除了GENRE_1GENRE_3一個id任何一本書:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="book[starts-with(id, 'GENRE') and not(id='GENRE_1' or id='GENRE_3')]"/> 

</xsl:stylesheet> 
+0

萬謝謝。這解決了我的要求 – reuel 2014-12-04 00:17:22

0

一個簡單的解決方案只是有一個空的模板匹配沒有要保持風格ID的所有書籍:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:ns0="http://www.themindelectric.com"> 
    <xsl:template match="node() | @*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node() | @*" /> 
    </xsl:copy> 
    </xsl:template> 
    <xsl:template match="book[not(id='GENRE_1' or id='GENRE_3' 
         or id='TEST_3' or id='ANOTHER_1')]"/> 
</xsl:stylesheet> 

這會複製除書籍以外沒有要保留的所有書籍的所有內容。

更新:對於更新的問題澄清的要求:保持,要麼有ID GENRE_1或GENRE_3或有一個ID不一致曲風所有的書:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:ns0="http://www.themindelectric.com"> 
    <xsl:template match="node() | @*"> 
     <xsl:copy> 
     <xsl:apply-templates select="node() | @*" /> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="book[starts-with(id, 'GENRE_') and 
         not(contains(id,1) or contains(id,3))]"/> 
</xsl:stylesheet> 

正如在評論中提到michael.hor257k,在匹配模式下使用將無法​​處理超過9種流派的情況。在這種情況下,只需匹配您想保留在not() - not(id='GENRE_1' or id='GENRE_3')中的ID - 就像michael.hor257k給出的正確答案一樣,這是正確的方法。 (!)

+0

謝謝爲你的答案。如果我知道xml中的特定id,你的解決方案將會很好地工作。請原諒我的示例xml。實際上,我不會知道書的id,所以如果xml包含TEST_4或ANOTHER_X,它將不起作用。我的示例解決方案爲所有以GENRE開頭的ID創建了一個模板,其中所有這些僅對GENRE_1和3以及任何其他非GENRE Book ID感興趣。歡呼 – reuel 2014-12-03 23:28:43

+0

@reuel很高興,如果我能夠幫助你。我剛剛爲澄清的要求更新了我的答案。 – 2014-12-03 23:51:48

+1

您的情況通過GENRE_10。 – 2014-12-03 23:54:42