Datypic Logo
XQuery

XQuery

Priscilla Walmsley (pwalmsley@datypic.com)

ISBN: 1491915102

2nd edition, 2015, O'Reilly Media, Inc.

Chapter 27: XQuery for XSLT Users

Please note that the book contains many inline examples and informal tables that are not provided here.

Example 27-1. A pull stylesheet
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="catalog">
    <ul>
      <xsl:for-each select="product">
        <li>Product #: <xsl:value-of select="number"/></li>
        <li>Product name: <xsl:value-of select="name"/></li>
      </xsl:for-each>
    </ul>
  </xsl:template>
</xsl:stylesheet>
Example 27-2. A push stylesheet
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="catalog">
    <ul>
      <xsl:apply-templates/>
    </ul>
  </xsl:template>
  <xsl:template match="product">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="number">
    <li>Product #: <xsl:value-of select="."/></li>
  </xsl:template>
  <xsl:template match="name">
    <li>Product name: <xsl:value-of select="."/></li>
  </xsl:template>
  <xsl:template match="node()"/>
</xsl:stylesheet>
Example 27-3. A push stylesheet on narrative content
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="p">
    <para><xsl:apply-templates/></para>
  </xsl:template>
  <xsl:template match="b">
    <Strong><xsl:apply-templates/></Strong>
  </xsl:template>
  <xsl:template match="i">
    <Italics><xsl:apply-templates/></Italics>
  </xsl:template>
</xsl:stylesheet>
Example 27-4. An attempt at a pull stylesheet on narrative content
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="p">
    <para>
      <xsl:for-each select="node()">
        <xsl:choose>
          <xsl:when test="self::text()">
             <xsl:value-of select="."/>
          </xsl:when>
          <xsl:when test="self::b">
            <Strong><xsl:value-of select="."/></Strong>
          </xsl:when>
          <xsl:when test="self::i">
            <Italics><xsl:value-of select="."/></Italics>
          </xsl:when>
        </xsl:choose>
      </xsl:for-each>
    </para>
  </xsl:template>
</xsl:stylesheet>
Example 27-5. Emulating templates with user-defined functions
declare function local:apply-templates($nodes as node()*) as node()* {
  for $node in $nodes
  return typeswitch ($node)
        case element(p) return local:p-template($node)
        case element(b) return local:b-template($node)
        case element(i) return local:i-template($node)
        case element() return local:apply-templates($node/(@*|node()))
        default return $node
};
declare function local:p-template($node as node()) as node()* {
   <para>{local:apply-templates($node/(@*|node()))}</para>
};
declare function local:b-template($node as node()) as node()* {
   <Strong>{local:apply-templates($node/(@*|node()))}</Strong>
};
declare function local:i-template($node as node()) as node()* {
   <Italics>{local:apply-templates($node/(@*|node()))}</Italics>
};
local:apply-templates(doc("p.xml")/p)
Datypic XQuery Services