(Q20) Find titles of books in which sailing is mentioned in every paragraph.

FOR $b IN //book
WHERE EVERY $p IN $b//para SATISFIES
   contains($p, "sailing")
RETURN $b/title

XSLT equivalent to (Q20)

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <xsl:for-each select="//book">
      <xsl:if test="count(.//para)=count(.//para[contains(., 'sailing')]) and .//para">
          <!-- OR: "not(.//para[not(contains(., 'sailing')]) and .//para" -->
        <xsl:copy-of select="title"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
</xsl:transform>
  • The first solution determines whether the number of para elements equals the number of para elements that contain "sailing". If so, then we know there are no para elements that do not contain "sailing". Since this also might mean that there are simply no para elements, we ensure that that is not the case by additionally testing that there is at least one of them.
  • The second solution tests whether any of the nodes in the node-set do not satisfy the given constraint. If any do not, then the test will return false. Since we want to determine whether or not every descendant para element contains "sailing", we test the emptiness (via not()) of the node-set consisting of para elements that do not contain "sailing". If it is empty, then we at least know there are no para elements that do not contain "sailing". Since this also might mean that there are simply no para elements, we ensure that that is not the case by additionally testing that there is at least one of them.
  • XPath/XSLT 2.0 promise to provide explicit support for existential and universal quantifiers.
<<<  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15    >>>