我有以下 xml 并希望生成一个仅包含 GENRE_1 和 GENRE_3 以及任何其他书籍 ID 的输出。这意味着 GENRE_4、5 和 6 将被删除。我曾尝试使用示例 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>