How to Strip out specific Node using XSLT

87 Views Asked by At

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>
2

There are 2 best solutions below

0
On BEST ANSWER

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>
4
On

A simple solution is just to have an empty template matching all books not having the genre id that you want to keep:

<?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>

This copies everything except books that have not the ids you want to keep.

Update: For the clarified requirement in the updated question: to keep all books that either have the id GENRE_1 or GENRE_3 or have an id not matching GENRE:

<?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>

As mentioned in the comments by michael.hor257k, using contains() in the match pattern won't work in case you have to handle more than 9 genres. In this case just matching the ids you want to keep in the not() - not(id='GENRE_1' or id='GENRE_3') - like in the correct answer given by michael.hor257k is the right approach.