I have the following xml and looking to produce an out put which contains only GENRE_1 and GENRE_3 and any other book ids. This means GENRE_4, 5 and 6 will be stripped out. I have tried using the sample xslt but not getting it right. Will appreciate any help.
<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>
EXPECTED OUTPUT
<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>
If I understand correctly your new(!) requirement, you want to exclude any book that has an id that starts with GENRE
, except GENRE_1
and GENRE_3
:
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>