如何使用XSLT去除特定节点

时间:2014-12-03 22:36:22

标签: xslt xpath apply-templates

我有以下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>

2 个答案:

答案 0 :(得分: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之外的所有内容。

更新:对于更新后问题中的明确要求:保留所有ID为GENRE_1或GENRE_3或ID与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>

正如michael.hor257k的评论中所提到的,在匹配模式中使用contains()不会起作用,以防您必须处理超过9种类型。在这种情况下,只需匹配您想要保留在not() - not(id='GENRE_1' or id='GENRE_3')中的ID - 就像michael.hor257k给出的正确答案一样,这是正确的方法。

答案 1 :(得分:0)

如果我理解了您的新(!)要求,您希望排除任何ID为以GENRE开头的图书,GENRE_1GENRE_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>