1

给定以下 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, ...) 不应该作为作者一起存在吗?

其中 (A, B, ...) 是一组作者姓名。

4

1 回答 1

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
于 2013-03-27T04:33:37.327 回答