0

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

2 回答 2

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()-中的 id not(id='GENRE_1' or id='GENRE_3'),就像 michael.hor257k 给出的正确答案一样是正确的方法。

于 2014-12-03T23:10:20.263 回答
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>
于 2014-12-03T23:52:11.290 回答