要表达的XPath查询条件:“set不包含any / all”或“set contains any / all”

时间:2013-03-26 18:29:05

标签: xml xpath

鉴于以下XML(来自W3Schools):

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

<book category="WEB">
  <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 category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>

</bookstore>

有没有办法退还满足的书籍集:

  1. (A,B,......)中的任何一个作为作者存在吗?
  2. 所有(A,B,......)都以作者身份存在?
  3. (A,B,......)中的任何一个不应该作为作者存在吗?
  4. 所有(A,B,......)不应该作为作者一起存在吗?
  5. 其中(A,B,...)是一组作者姓名。

1 个答案:

答案 0 :(得分:1)

以下是所有想要的表达。我假设提供的名称集不仅仅是字符串集(在XPath 1.0中没有这样的概念),而是作为节点集,每个节点的字符串值是我们必须具有的名称之一

我也在使用XSLT 1.0,这样我就可以显示四个XPath表达式中每个表达式的评估结果。

作为一组作者姓名,我选择了作者名为“XQuery Kick Start”的书名。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vAuthors" select="/*/*[3]/author"/>

 <xsl:template match="/">
  1. Any of (A, B, ...) exist as authors: <xsl:text/>

  <xsl:value-of select="boolean($vAuthors[. = /*/*/author])"/>

  2. All of (A, B, ...) exist as authors: <xsl:text/>

  <xsl:value-of select="not($vAuthors[not(. = /*/*/author)])"/>

  3. Any of (A, B, ...) should not exist as authors: <xsl:text/>

  <xsl:value-of select="not($vAuthors[. = /*/*/author])"/>

  4. All of (A, B, ...) should not exist as authors together: <xsl:text/>

  <xsl:value-of select=
    "not(/*/*[count(author[. = $vAuthors]) = count($vAuthors)])"/>

 </xsl:template>
</xsl:stylesheet>

在提供的XML文档上应用此转换时:

<bookstore>
    <book category="COOKING">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
    </book>
    <book category="CHILDREN">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>
    <book category="WEB">
        <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 category="WEB">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
    </book>
</bookstore>

评估XPath表达式并将其结果复制到输出中:

  1. Any of (A, B, ...) exist as authors: true

  2. All of (A, B, ...) exist as authors: true

  3. Any of (A, B, ...) should not exist as authors: false

  4. All of (A, B, ...) should not exist as authors together: false