XML technologies / XSLT / XSLT Cookbook / Querying XML / Implementing W3C XML Query Use Cases in XSLT.

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You need a query similar to a use case in XML Query Use Cases but want XSLT instead of XQuery.

Solution

These examples solve most W3C query use cases using XSLT. Their descriptions closely follow the W3C document.

  1. Use case “XMP”: Experiences and examples.
    Example queries illustrate requirements gathered by W3C from database and document communities. The next four examples give their input data.

Example: bib.xml

<bib>
  <book year="1994">
    <title>TCP/IP Illustrated</title>
    <author><last>Stevens</last><first>W.</first></author>
    <publisher>Addison-Wesley</publisher>
    <price> 65.95</price>
  </book>
  <book year="1992">
    <title>Advanced Programming in the Unix environment</title>
    <author><last>Stevens</last><first>W.</first></author>
    <publisher>Addison-Wesley</publisher>
    <price>65.95</price>
  </book>
  <book year="2000">
    <title>Data on the Web</title>
    <author><last>Abiteboul</last><first>Serge</first></author>
    <author><last>Buneman</last><first>Peter</first></author>
    <author><last>Suciu</last><first>Dan</first></author>
    <publisher>Morgan Kaufmann Publishers</publisher>
    <price> 39.95</price>
  </book>
  <book year="1999">
    <title>The Economics of Technology and Content for Digital TV</title>
    <editor>
      <last>Gerbarg</last><first>Darcy</first>
      <affiliation>CITI</affiliation>
    </editor>
    <publisher>Kluwer Academic Publishers</publisher>
    <price>129.95</price>
  </book>
</bib>

Example: reviews.xml

<reviews>
  <entry>
    <title>Data on the Web</title>
    <price>34.95</price>
    <review>A very good discussion of semi-structured database systems and XML.</review>
  </entry>
  <entry>
    <title>Advanced Programming in the Unix environment</title>
    <price>65.95</price>
    <review>A clear and detailed discussion of UNIX programming.</review>
  </entry>
  <entry>
    <title>TCP/IP Illustrated</title>
    <price>65.95</price>
    <review>One of the best books on TCP/IP.</review>
  </entry>
</reviews>

Example: books.xml

<chapter>
  <title>Data Model</title>
  <section>
    <title>Syntax For Data Model</title>
  </section>
  <section>
    <title>XML</title>
    <section>
      <title>Basic Syntax</title>
    </section>
    <section>
      <title>XML and Semistructured Data</title>
    </section>
  </section>
</chapter>

Example: prices.xml

<prices>
  <book>
    <title>Advanced Programming in the Unix environment</title>
    <source>www.amazon.com</source>
    <price>65.95</price>
  </book>
  <book>
    <title>Advanced Programming in the Unix environment</title>
    <source>www.bn.com</source>
    <price>65.95</price>
  </book>
  <book>
    <title>TCP/IP Illustrated</title>
    <source>www.amazon.com</source>
    <price>65.95</price>
  </book>
  <book>
    <title>TCP/IP Illustrated</title>
    <source>www.bn.com</source>
    <price>65.95</price>
  </book>
  <book>
    <title>Data on the Web</title>
    <source>www.amazon.com</source>
    <price>34.95</price>
  </book>
  <book>
    <title>Data on the Web</title>
    <source>www.bn.com</source>
    <price>39.95</price>
  </book>
</prices>

<!-- Frage 1. Liste Bücher in bib.xml auf, die von Addison-Wesley nach 1991 veröffentlicht wurden, einschließlich ihres Jahres und Titels: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="copy.xslt"/>
  <xsl:template match="book[publisher = 'Addison-Wesley' and @year &gt; 1991]">
    <xsl:copy-of select="."/>
  </xsl:template>
  <xsl:template match="book"/>
</xsl:stylesheet>

<!-- Frage 2. Erzeuge eine einfache Liste aller Titel-Autor-Paare aus bib.xml, wobei jedes Paar in ein "result"-Element eingeschlossen ist: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <results>
      <xsl:apply-templates select="bib/book/author"/>
    </results>
  </xsl:template>
  <xsl:template match="author">
    <result>
      <xsl:copy-of select="preceding-sibling::title"/>
      <xsl:copy-of select="."/>
    </result>
  </xsl:template>
</xsl:stylesheet>

<!-- Frage 3. Liste für jedes Buch in bib.xml Titel und Autoren auf, und zwar in einem "result"-Element gruppiert: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="bib">
    <results>
      <xsl:for-each select="book">
        <result>
          <xsl:copy-of select="title"/>
          <xsl:copy-of select="author"/>
        </result>
      </xsl:for-each>
    </results>
  </xsl:template>
</xsl:stylesheet>

<!-- Frage 4. Liste für jeden Autor in bib.xml den Namen des Autors und die Titel aller Bücher dieses Autors auf, und zwar in einem "result"-Element gruppiert: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <results>
      <xsl:for-each select="//author[not(.=preceding::author)]">
        <result>
          <xsl:copy-of select="."/>
          <xsl:for-each select="/bib/book[author=current()]">
            <xsl:copy-of select="title"/>
          </xsl:for-each>
        </result>
      </xsl:for-each>
    </results>
    <!-- Frage 5. Liste für alle Bücher, die sowohl auf http://www.bn.com (bib.xml) als auch auf http://www.amazon.com (reviews.xml) gefunden wurden, den Titel des Buches und den Preis bei der jeweiligen Quelle auf: -->
    <xsl:variable name="bn" select="document('bib.xml')"/>
    <xsl:variable name="amazon" select="document('reviews.xml')"/>
  </xsl:template>
</xsl:stylesheet>

<!--Lösung 1 -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <books-with-prices>
      <xsl:for-each select="$bn//book[title = $amazon//entry/title]">
        <book-with-prices>
          <xsl:copy-of select="title"/>
          <price-amazon>
            <xsl:value-of select="$amazon//entry[title=current()/title]/price"/>
          </price-amazon>
          <price-bn>
            <xsl:value-of select="price"/>
          </price-bn>
        </book-with-prices>
      </xsl:for-each>
    </books-with-prices>
  </xsl:template>
</xsl:stylesheet>

<!--Lösung 2-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <books-with-prices>
      <xsl:for-each select="$bn//book">
        <xsl:variable name="bn-book" select="."/>
        <xsl:for-each select="$amazon//entry[title=$bn-book/title]">
          <book-with-prices>
            <xsl:copy-of select="title"/>
            <price-amazon>
              <xsl:value-of select="price"/>
            </price-amazon>
            <price-bn>
              <xsl:value-of select="$bn-book/price"/>
            </price-bn>
          </book-with-prices>
        </xsl:for-each>
      </xsl:for-each>
    </books-with-prices>
  </xsl:template>
</xsl:stylesheet>

<!-- Frage 6. Liste für jedes Buch, das mindestens einen Autor hat, den Titel und die ersten beiden Autoren sowie ein leeres "et-al"-Element auf, wenn das Buch weitere Autoren hat: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="bib">
    <xsl:copy>
      <xsl:for-each select="book[author]">
        <xsl:copy>
          <xsl:copy-of select="title"/>
          <xsl:copy-of select="author[position() &lt;= 2]"/>
          <xsl:if test="author[3]">
            <et-al/>
          </xsl:if>
        </xsl:copy>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

<!-- Frage 7. Liste in alphabetischer Reihenfolge die Titel und Jahre aller Bücher auf, die nach 1991 von Addison-Wesley veröffentlicht wurden: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="bib">
    <xsl:copy>
      <xsl:for-each select="book[publisher = 'Addison-Wesley' and @year &gt; 1991]">
        <xsl:sort select="title"/>
        <xsl:copy>
          <xsl:copy-of select="@year"/>
          <xsl:copy-of select="title"/>
        </xsl:copy>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

<!-- Frage 8. Suche im Dokument books.xml alle Abschnitts-(section-) oder Kapitel-(chapter-)titel, die das Wort "XML" enthalten, ungeachtet der Verschachtelungsebene: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <results>
      <xsl:copy-of select="(//chapter/title | //section/title)[contains(.,'XML')]"/>
    </results>
  </xsl:template>
</xsl:stylesheet>

<!-- Frage 9. Suche im Dokument prices.xml den Minimalpreis für jedes Buch in Form eines "minprice"-Elements mit dem Buchtitel als Titelattribut: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:include href="../math/math.min.xslt"/>
  <xsl:template match="/">
    <results>
      <xsl:for-each select="//book/title[not(. = ./preceding::title)]">
        <xsl:variable name="min-price">
          <xsl:call-template name="math:min">
            <xsl:with-param name="nodes" select="//book[title = current()]/price"/>
          </xsl:call-template>
        </xsl:variable>
        <minprice title="{.}">
          <price>
            <xsl:value-of select="$min-price"/>
          </price>
        </minprice>
      </xsl:for-each>
    </results>
  </xsl:template>
</xsl:stylesheet>

<!-- Frage 10. Liefere für jedes Buch mit einem Autor das Buch mit seinem Titel und seinen Autoren zurück. Liefere für jedes Buch mit einem Herausgeber eine Referenz mit dem Buchtitel und der Zugehörigkeit des Herausgebers zurück: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="bib">
    <xsl:copy>
      <xsl:for-each select="book[author]">
        <xsl:copy>
          <xsl:copy-of select="title"/>
          <xsl:copy-of select="author"/>
        </xsl:copy>
      </xsl:for-each>
      <xsl:for-each select="book[editor]">
        <reference>
          <xsl:copy-of select="title"/>
          <org><xsl:value-of select="editor/affiliation"/></org>
        </reference>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

<!-- Frage 11. Suche Paare von Büchern, die unterschiedliche Titel, aber die gleichen Autoren (möglicherweise in anderer Reihenfolge) haben: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:include href="query.equal-values.xslt"/>
  <xsl:template match="bib">
    <xsl:copy>
      <xsl:for-each select="book[author]">
        <xsl:variable name="book1" select="."/>
        <xsl:for-each select="./following-sibling::book[author]">
          <xsl:variable name="same-authors">
            <xsl:call-template name="query:equal-values">
              <xsl:with-param name="nodes1" select="$book1/author"/>
              <xsl:with-param name="nodes2" select="author"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:if test="string($same-authors)">
            <book-pair>
              <xsl:copy-of select="$book1/title"/>
              <xsl:copy-of select="title"/>
            </book-pair>
          </xsl:if>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

  1. Use case “TREE”: Preserving hierarchy.
    Some XML types have flexible mixed content and many optional elements. Their structure varies, and ordering and nesting matter. Query languages should extract elements while preserving structure. This case uses a flexible type named Book.

    The DTD and XML input follow in two examples.

Example: book.dtd

<!ELEMENT book (title, author+, section+)>
  <!ELEMENT title (#PCDATA)>
  <!ELEMENT author (#PCDATA)>
  <!ELEMENT section (title, (p | figure | section)* )>
  <!ATTLIST section
      id         ID    #IMPLIED
      difficulty CDATA #IMPLIED>
  <!ELEMENT p (#PCDATA)>
  <!ELEMENT figure (title, image)>
  <!ATTLIST figure
      width   CDATA    #REQUIRED
      height  CDATA   #REQUIRED >
  <!ELEMENT image EMPTY>
  <!ATTLIST image
      source  CDATA   #REQUIRED >

Example: book.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book SYSTEM "book.dtd">
<book>
  <title>Data on the Web</title>
  <author>Serge Abiteboul</author>
  <author>Peter Buneman</author>
  <author>Dan Suciu</author>
  <section id="intro" difficulty="easy">
    <title>Introduction</title>
    <p>Text ... </p>
    <section>
      <title>Audience</title>
      <p>Text ... </p>
    </section>
    <section>
      <title>Web Data and the Two Cultures</title>
      <p>Text ... </p>
      <figure height="400" width="400">
        <title>Traditional client/server architecture</title>
        <image source="csarch.gif"/>
      </figure>
      <p>Text ... </p>
    </section>
  </section>
  <section id="syntax" difficulty="medium" >
    <title>A Syntax For Data</title>
    <p>Text ... </p>
    <figure height="200" width="500">
      <title>Graph representations of structures</title>
      <image source="graphs.gif"/>
    </figure>
    <p>Text ... </p>
    <section>
      <title>Base Types</title>
      <p>Text ... </p>
    </section>
    <section>
      <title>Representing Relational Databases</title>
      <p>Text ... </p>
      <figure height="250" width="400">
        <title>Examples of Relations</title>
         <image source="relations.gif"/>
      </figure>
    </section>
    <section>
      <title>Representing Object Databases</title>
      <p>Text ... </p>
    </section>
  </section>
</book>

<!-- Frage 1. Bereite ein (geschachteltes) Inhaltsverzeichnis für Book1 vor, in dem alle Abschnitte und deren Titel aufgelistet werden. Die Originalattribute der einzelnen <section>-Elemente sollen beibehalten werden, falls welche existieren: -->
<xsl:template match="book">
  <toc>
    <xsl:apply-templates/>
  </toc>
</xsl:template>
<!-- Kopieren des Elements von toc -->
<xsl:template match="section | section/title | section/title/text()">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>
<!-- Unterdrücken anderer Elemente -->
<xsl:template match="* | text()"/>

<!-- Frage 2. Bereite eine (einfache) Abbildungsliste für Book1 vor, in der alle Abbildungen und deren Titel aufgelistet werden. Die Originalattribute der einzelnen <figure>-Elemente sollen beibehalten werden, falls welche existieren: -->
<xsl:template match="book">
  <figlist>
    <xsl:for-each select=".//figure">
      <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:copy-of select="title"/>
      </xsl:copy>
    </xsl:for-each>
  </figlist>
</xsl:template>

<!-- Frage 3. Wie viele Abschnitte (section) und wie viele Abbildungen (figure) sind in Book1? -->
<xsl:template match="/">
  <section-count><xsl:value-of select="count(//section)"/></section-count>
  <figure-count><xsl:value-of select="count(//figure)"/></figure-count>
</xsl:template>

<!-- Frage 4. Wie viele Hauptabschnitte gibt es in Book1? -->
<xsl:template match="book">
  <top_section_count>
     <xsl:value-of select="count(section)"/>
  </top_section_count>
</xsl:template>

<!-- Frage 5. Erzeuge eine einfache Liste der section-Elemente in Book1. Anstelle seiner Originalattribute soll jedes section-Element zwei Attribute haben, die den Titel des Abschnitts und die Anzahl der Abbildungen enthalten, die sich unmittelbar im Abschnitt befinden: -->
<xsl:template match="book">
  <section_list>
    <xsl:for-each select=".//section">
      <section title="{title}" figcount="{count(figure)}"/>
    </xsl:for-each>
  </section_list>
</xsl:template>

<!-- Frage 6. Erzeuge eine geschachtelte Liste der section-Elemente in Book1, wobei ihre Originalattribute und die ursprüngliche Hierarchie erhalten bleiben. Füge in jedem section-Element den Titel des Abschnitts sowie ein Element hinzu, das die Anzahl der Abbildungen enthält, die sich unmittelbar im Abschnitt befinden (siehe Beispiel "Die Lösung, so wie ich die englischen Anforderungen interpretieren würde" und Beispiel "Was der W3C-Anwendungsfall, basierend auf einem Beispielergebnis und XQuery, haben möchte"). -->

Example: My interpretation of the English requirements.

<xsl:template match="book">
  <toc>
    <xsl:apply-templates select="section"/>
  </toc>
</xsl:template>

<xsl:template match="section">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:copy-of select="title"/>
    <figcount><xsl:value-of select="count(figure)"/></figcount>
    <xsl:apply-templates select="section"/>
  </xsl:copy>
</xsl:template>

Example: The W3C intent inferred from output and XQuery.

<xsl:template match="book">
  <toc>
    <xsl:for-each select="//section">
      <xsl:sort select="count(ancestor::section)"/>
      <xsl:apply-templates select="."/>
    </xsl:for-each>
  </toc>
</xsl:template>

<xsl:template match="section">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:copy-of select="title"/>
    <figcount><xsl:value-of select="count(figure)"/></figcount>
    <xsl:apply-templates select="section"/>
  </xsl:copy>
</xsl:template>

  1. Use case “SEQ”: Sequence-based queries.
    Queries use document element order. Sequence matters less in traditional database or object systems but can be essential in structured documents. This case queries a medical report:

<!DOCTYPE report [
  <!ELEMENT report (section*)>
  <!ELEMENT section (section.title, section.content)>
  <!ELEMENT section.title  (#PCDATA )>
  <!ELEMENT section.content  (#PCDATA | anesthesia | prep | incision | action | observation )*>
  <!ELEMENT anesthesia (#PCDATA)>
  <!ELEMENT prep ( (#PCDATA | action)* )>
  <!ELEMENT incision ( (#PCDATA | geography | instrument)* )>
  <!ELEMENT action ( (#PCDATA | instrument )* )>
  <!ELEMENT observation (#PCDATA)>
  <!ELEMENT geography (#PCDATA)>
  <!ELEMENT instrument (#PCDATA)>
]>
<report>
  <section>
    <section.title>Procedure</section.title>
    <section.content>The patient was taken to the operating room where she was placed in supine position and <anesthesia>induced under general anesthesia.</anesthesia> <prep><action>A Foley catheter was placed to decompress the bladder</action> and the abdomen was then prepped and draped in sterile fashion.</prep> <incision>A curvilinear incision was made <geography>in the midline immediately infraumbilical</geography> and the subcutaneous issue was divided <instrument>using electrocautery.</instrument></incision> The fascia was identified and <action>#2 0 Maxon stay sutures were placed on each side of the midline.</action> <incision>The fascia was divided using <instrument>electrocautery</instrument> and the peritoneum was entered.</incision> <observation>The small bowel was identified.</observation> and <action> the <instrument>Hasson trocar</instrument> was placed under direct visualization.</action> <action>The <instrument>trocar</instrument> was secured to the fascia using the stay sutures.</action></section.content>
  </section>
</report>
<!-- Frage 1. Welche Instrumente wurden im Abschnitt Procedure von Report1 beim zweiten Einschnitt (incision) verwendet? -->
<xsl:template match="section[section.title = 'Procedure']">
  <xsl:copy-of select="(.//incision)[2]/instrument"/>
</xsl:template>
<!-- Frage 2. Welches sind die ersten beiden Instrumente, die im Abschnitt Procedure von Report1 zum Einsatz kamen? -->
<xsl:template match="section[section.title = 'Procedure']">
  <xsl:copy-of select="(.//instrument)[position( ) <= 2]"/>
</xsl:template>
<!-- Frage 3. Welche Instrumente wurden in Report1 bei den ersten beiden Aktionen nach dem zweiten Einschnitt verwendet? -->
<xsl:template match="report">
  <!-- i2 = Zweiter Einschnitt im gesamten Bericht -->
  <xsl:variable name="i2" select="(.//incision)[2]"/>
  <!-- Beziehe die Instrumente, die in den ersten beiden Aktionen verwendet werden, die auf i2 folgen -->
  <xsl:copy-of select="($i2/following::action)[position( ) &lt;= 2]/instrument"/>
</xsl:template>
<!-- Frage 4. Suche in Report1 "Procedure"-Abschnitte, in denen vor dem ersten Einschnitt (incision) kein anesthesia-Element auftaucht: -->
<xsl:template match="section[section.title = 'Procedure']">
  <xsl:variable name="i1" select="(.//incision)[1]"/>
  <xsl:if test=".//anesthesia[preceding::incision = $i1]">
    <xsl:copy-of select="current( )"/>
  </xsl:if>
</xsl:template>
<!-- Wenn das Ergebnis nicht leer ist, kommt es in Kürze zur Anklage! Frage 5. Was passierte in Report1 zwischen dem ersten und dem zweiten Schnitt? -->
<xsl:template match="report">
  <critical_sequence>
    <!-- i1 = Erster Einschnitt im gesamten Bericht -->
    <xsl:variable name="i1" select="(.//incision)[1]"/>
    <!-- i2 = Zweiter Einschnitt im gesamten Bericht -->
    <xsl:variable name="i2" select="(.//incision)[2]"/>
    <!-- Kopiere alle auf i1 folgenden Geschwisterknoten, die kein Vorgängerelement i2 haben und nicht selbst i2 sind -->
    <xsl:for-each select="$i1/following-sibling::node( ) [not(./preceding::incision = $i2) and not(. = $i2)]">
      <xsl:copy-of select="."/>
    </xsl:for-each>
  </critical_sequence>
</xsl:template>

Warning!
Queries 4 and 5 assume unique string values for elements incision. True in this sample, this need not hold generally. For precision, use recipe Finding the last day of a month. Query 4’s test should be:

test=".//anesthesia[count(./preceding::incision | $i1) = count(./preceding::incision)]"

  1. Use case “R”: Relational data.
    XML query languages should access relational data. A database can expose each table as XML, with its root Document representing the table, nested elements for rows, and children for columns. Nullable columns become optional elements; absence means null.

    An auction database has USERS with unique user IDs. Users sell items or bid. ITEMS lists current and recent offers with seller IDs. BIDS stores submitted bids by bidder ID and item number.

    Only a subset is implemented because there are many queries. Implement the rest to practice XSLT; see the next three examples.

Example: users.xml

<users>
  <user_tuple>
    <userid>U01</userid>
    <name>Tom Jones</name>
    <rating>B</rating>
  </user_tuple>
  <user_tuple>
    <userid>U02</userid>
    <name>Mary Doe</name>
    <rating>A</rating>
  </user_tuple>
  <user_tuple>
    <userid>U03</userid>
    <name>Dee Linquent</name>
    <rating>D</rating>
  </user_tuple>
  <user_tuple>
    <userid>U04</userid>
    <name>Roger Smith</name>
    <rating>C</rating>
  </user_tuple>
  <user_tuple>
    <userid>U05</userid>
    <name>Jack Sprat</name>
    <rating>B</rating>
  </user_tuple>
  <user_tuple>
    <userid>U06</userid>
    <name>Rip Van Winkle</name>
    <rating>B</rating>
  </user_tuple>
</users>

Example: items.xml

<items>
  <item_tuple>
    <itemno>1001</itemno>
    <description>Red Bicycle</description>
    <offered_by>U01</offered_by>
    <start_date>99-01-05</start_date>
    <end_date>99-01-20</end_date>
    <reserve_price>40</reserve_price>
  </item_tuple>
  <item_tuple>
    <itemno>1002</itemno>
    <description>Motorcycle</description>
    <offered_by>U02</offered_by>
    <start_date>99-02-11</start_date>
    <end_date>99-03-15</end_date>
    <reserve_price>500</reserve_price>
  </item_tuple>
  <item_tuple>
    <itemno>1003</itemno>
    <description>Old Bicycle</description>
    <offered_by>U02</offered_by>
    <start_date>99-01-10</start_date>
    <end_date>99-02-20</end_date>
    <reserve_price>25</reserve_price>
  </item_tuple>
  <item_tuple>
    <itemno>1004</itemno>
    <description>Tricycle</description>
    <offered_by>U01</offered_by>
    <start_date>99-02-25</start_date>
    <end_date>99-03-08</end_date>
    <reserve_price>15</reserve_price>
  </item_tuple>
  <item_tuple>
    <itemno>1005</itemno>
    <description>Tennis Racket</description>
    <offered_by>U03</offered_by>
    <start_date>99-03-19</start_date>
    <end_date>99-04-30</end_date>
    <reserve_price>20</reserve_price>
  </item_tuple>
  <item_tuple>
    <itemno>1006</itemno>
    <description>Helicopter</description>
    <offered_by>U03</offered_by>
    <start_date>99-05-05</start_date>
    <end_date>99-05-25</end_date>
    <reserve_price>50000</reserve_price>
  </item_tuple>
  <item_tuple>
    <itemno>1007</itemno>
    <description>Racing Bicycle</description>
    <offered_by>U04</offered_by>
    <start_date>99-01-20</start_date>
    <end_date>99-02-20</end_date>
    <reserve_price>200</reserve_price>
  </item_tuple>
  <item_tuple>
    <itemno>1008</itemno>
    <description>Broken Bicycle</description>
    <offered_by>U01</offered_by>
    <start_date>99-02-05</start_date>
    <end_date>99-03-06</end_date>
    <reserve_price>25</reserve_price>
  </item_tuple>
</items>

Example: bids.xml

<bids>
  <bid_tuple>
    <userid>U02</userid>
    <itemno>1001</itemno>
    <bid>35</bid>
    <bid_date>99-01-07 </bid_date>
  </bid_tuple>
  <bid_tuple>
    <userid>U04</userid>
    <itemno>1001</itemno>
    <bid>40</bid>
    <bid_date>99-01-08</bid_date>
  </bid_tuple>
  <bid_tuple>
    <userid>U02</userid>
    <itemno>1001 </itemno>
    <bid>45</bid>
    <bid_date>99-01-11</bid_date>
  </bid_tuple>
  <bid_tuple>
    <userid>U04</userid>
    <itemno>1001</itemno>
    <bid>50</bid>
    <bid_date>99-01-13</bid_date>
  </bid_tuple>
  <bid_tuple>
    <userid>U02</userid>
    <itemno>1001</itemno>
    <bid>55</bid>
    <bid_date>99-01-15</bid_date>
  </bid_tuple>
  <bid_tuple>
    <userid>U01</userid>
    <itemno>1002</itemno>
    <bid>400</bid>
    <bid_date>99-02-14</bid_date>
  </bid_tuple>
  <bid_tuple>
    <userid>U02</userid>
    <itemno>1002</itemno>
    <bid>600</bid>
    <bid_date>99-02-16</bid_date>
  </bid_tuple>
  <bid_tuple>
    <userid>U03</userid>
    <itemno>1002</itemno>
    <bid>800</bid>
    <bid_date>99-02-17</bid_date>
  </bid_tuple>
  <bid_tuple>
    <userid>U04</userid>
    <itemno>1002</itemno>
    <bid>1000</bid>
    <bid_date>99-02-25</bid_date>
  </bid_tuple>
  <bid_tuple>
    <userid>U02</userid>
    <itemno>1002</itemno>
    <bid>1200</bid>
    <bid_date>99-03-02</bid_date>
  </bid_tuple>
  <bid_tuple>
    <userid>U04</userid>
    <itemno>1003</itemno>
    <bid>15</bid>
    <bid_date>99-01-22</bid_date>
  </bid_tuple>
  <bid_tuple>
    <userid>U05</userid>
    <itemno>1003</itemno>
    <bid>20</bid>
    <bid_date>99-02-03</bid_date>
  </bid_tuple>
  <bid_tuple>
    <userid>U01</userid>
    <itemno>1004</itemno>
    <bid>40</bid>
    <bid_date>99-03-05</bid_date>
  </bid_tuple>
  <bid_tuple>
    <userid>U03</userid>
    <itemno>1007</itemno>
    <bid>175</bid>
    <bid_date>99-01-25</bid_date>
  </bid_tuple>
  <bid_tuple>
    <userid>U05</userid>
    <itemno>1007</itemno>
    <bid>200</bid>
    <bid_date>99-02-08</bid_date>
  </bid_tuple>
  <bid_tuple>
    <userid>U04</userid>
    <itemno>1007</itemno>
    <bid>225</bid>
    <bid_date>99-02-12</bid_date>
  </bid_tuple>
</bids>

<!-- Frage 1. Liste die Objektnummer und die Beschreibung aller Fahrräder auf, die momentan in einer Auktion angeboten werden, sortiert nach Objektnummer: -->
<xsl:include href="../date/date.date-time.xslt"/>
<!-- Damit das Ergebnis wie das W3C-Beispiel aussieht -->
<xsl:param name="today" select="'1999-01-21'"/>
<xsl:template match="items">
  <xsl:variable name="today-abs">
    <xsl:call-template name="date:date-to-absolute-day">
      <xsl:with-param name="date" select="$today"/>
    </xsl:call-template>
  </xsl:variable>
  <result>
    <xsl:for-each select="item_tuple">
      <xsl:sort select="itemno" data-type="number"/>
      <xsl:variable name="start-abs">
        <xsl:call-template name="date:date-to-absolute-day">
          <xsl:with-param name="date" select="start_date"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:variable name="end-abs">
        <xsl:call-template name="date:date-to-absolute-day">
          <xsl:with-param name="date" select="end_date"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:if test="$start-abs &lt;= $today-abs and $end-abs &gt;= $today-abs and contains(description, 'Bicycle')">
        <xsl:copy>
          <xsl:copy-of select="itemno"/>
          <xsl:copy-of select="description"/>
        </xsl:copy>
      </xsl:if>
    </xsl:for-each>
  </result>
</xsl:template>

<!-- Frage 2. Liste für alle Fahrräder die Objektnummer, die Beschreibung und das höchste Gebot (falls vorhanden) auf, sortiert nach Objektnummer: -->
<xsl:include href="../math/math.max.xslt"/>
<xsl:template match="items">
  <result>
    <xsl:for-each select="item_tuple[contains(description,'Bicycle')]">
      <xsl:sort select="itemno" data-type="number"/>
      <xsl:variable name="bids" select="document('bids.xml')//bid_tuple[itemno=current()/itemno]/bid"/>
      <xsl:variable name="high-bid">
        <xsl:call-template name="math:max">
          <xsl:with-param name="nodes" select="$bids"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:copy>
        <xsl:copy-of select="itemno"/>
        <xsl:copy-of select="description"/>
        <high_bid><xsl:if test="$bids"><xsl:value-of select="$high-bid"/></xsl:if></high_bid>
      </xsl:copy>
    </xsl:for-each>
  </result>
</xsl:template>

<!-- Frage 3. Suche Fälle, in denen ein Benutzer mit einer Bewertung schlechter (alphabetisch: größer) als "C" ein Objekt mit einem Mindestpreis von mehr als 1.000 anbietet: -->
<!-- Nicht unbedingt notwendig, aber die Spezifikation definiert kein Bewertungssystem, weshalb wir es dynamisch ableiten! -->
<xsl:variable name="ratings">
  <xsl:for-each select="document('users.xml')//user_tuple/rating">
    <xsl:sort select="." data-type="text"/>
    <xsl:if test="not(. = ./preceding::rating)">
      <xsl:value-of select="."/>
    </xsl:if>
  </xsl:for-each>
</xsl:variable>
<xsl:template match="items">
  <result>
    <xsl:for-each select="item_tuple[reserve_price &gt; 1000]">
      <xsl:variable name="user" select="document('users.xml')//user_tuple[userid = current()/offered_by]"/>
      <xsl:if test="string-length(substring-before($ratings,$user/rating)) &gt; string-length(substring-before($ratings,'C'))">
        <warning>
          <xsl:copy-of select="$user/name"/>
          <xsl:copy-of select="$user/rating"/>
          <xsl:copy-of select="description"/>
          <xsl:copy-of select="reserve_price"/>
        </warning>
      </xsl:if>
    </xsl:for-each>
  </result>
</xsl:template>

<!-- Frage 4. Liste Objektnummern und Beschreibungen von Objekten auf, für die es keine Gebote gibt: -->
<xsl:template match="items">
  <result>
    <xsl:for-each select="item_tuple">
      <xsl:if test="not(document('bids.xml')//bid_tuple[itemno = current()/itemno])">
        <no_bid_item>
          <xsl:copy-of select="itemno"/>
          <xsl:copy-of select="description"/>
        </no_bid_item>
      </xsl:if>
    </xsl:for-each>
  </result>
</xsl:template>

  1. Use case “SGML”: Standard Generalized Markup Language.
    These queries and sample document were created for a 1992 conference on Standard Generalized Markup Language (SGML). For this use, the Document Type Definition (DTD) and sample were converted from SGML to XML.

    These queries are omitted because they resemble the other cases.

  2. Use case “TEXT”: Full-text search.
    Company profiles and news about public relations, mergers and acquisitions form this case. It illustrates text searches and different results combining profile and news information.

    Company-name searches are word-based, ignoring case and permitting arbitrary whitespace between words.

    All queries are expressible in XSLT 1.0 but may require much searching. Hard cases test whether any string in a set occurs in another string, or inspect subunits such as sentence boundaries.

    Techniques in XPath suggest solutions. Extensive searching needs generic utilities, discussed in chapter Extending and embedding XSLT, which revisits complex queries. Here we solve two simple cases and list others to illustrate XSLT 1.0 challenges, highlighting difficult parts.

<!-- Frage 1. Suche alle News-Objekte, in denen der Name "Foobar Corporation" im Titel auftaucht: -->
<xsl:template match="news">
  <result>
    <xsl:copy-of select="news_item/title[contains(., 'Foobar Corporation')]"/>
  </result>
</xsl:template>

<!-- Frage 2. Erzeuge für jedes News-Objekt, das für die Gorilla Corporation relevant ist, ein "item summary"-Element (Objektzusammenfassung). Der Inhalt der Objektzusammenfassung besteht aus dem Titel, dem Datum und dem ersten Absatz des News-Objekts, getrennt durch Punkte. Ein News-Objekt ist relevant, wenn der Name des Unternehmens irgendwo im Inhalt des News-Objekts erwähnt wird: -->
<xsl:template match="news">
  <result>
    <xsl:for-each select="news_item[contains(content,'Gorilla Corporation')]">
      <item_summary>
        <xsl:value-of select="normalize-space(title)"/>. <xsl:text/>
        <xsl:value-of select="normalize-space(date)"/>. <xsl:text/>
        <xsl:value-of select="normalize-space(content/par[1])"/>
      </item_summary>
    </xsl:for-each>
  </result>
</xsl:template>

  1. Use case “PARTS”: Recursive parts explosion.
    A recursive query builds arbitrarily deep hierarchy from flat database structures.

    This database records parts contained in other parts.

    Flat input represents each distinct part with element <part> bearing attributes partid and name. A part may belong to a larger one, whose partid is held in attribute partof. In a relational table, partid would be primary key and partof a foreign key referencing partid. The query must turn these links into structural hierarchy. Input follows this DTD:

<!partlist DOCTYPE [
    <!ELEMENT partlist (part*)>
    <!ELEMENT part EMPTY>
    <!ATTLIST part
        partid  CDATA  #REQUIRED
        partof  CDATA  #IMPLIED
        name    CDATA  #REQUIRED>
]>

Attributes partid and partof could be ID and IDREF but are character data here, perhaps for easy relational extraction. Each attribute partof selects one partid. Parts lacking attribute partof are not contained in another part.

Output follows this DTD:

<!DOCTYPE parttree [
    <!ELEMENT parttree (part*)>
    <!ELEMENT part (part*)>
    <!ATTLIST part
        partid  CDATA  #REQUIRED
        name    CDATA  #REQUIRED>
]>

Example conforming data:

<?xml version="1.0" encoding="ISO-8859-1"?>
<partlist>
  <part partid="0" name="car"/>
  <part partid="1" partof="0" name="engine"/>
  <part partid="2" partof="0" name="door"/>
  <part partid="3" partof="1" name="piston"/>
  <part partid="4" partof="2" name="window"/>
  <part partid="5" partof="2" name="lock"/>
  <part partid="10" name="skateboard"/>
  <part partid="11" partof="10" name="board"/>
  <part partid="12" partof="10" name="wheel"/>
  <part partid="20" name="canoe"/>
</partlist>

<!-- Frage 1. Konvertiere das Beispieldokument aus dem "partlist"- in das "parttree"-Format (siehe den DTD-Abschnitt für Definitionen). Im resultierenden Dokument wird die Teilezugehörigkeit dadurch repräsentiert, dass ein <part>-Element in einem anderen enthalten ist. Jedes Teil, das nicht Bestandteil eines anderen Teils ist, sollte als separates Top-Level-Dokument im Ausgabedokument auftauchen: -->
<xsl:template match="partlist">
  <parttree>
    <!-- Beginne mit dem Teil, das nicht Bestandteil eines anderen ist -->
    <xsl:apply-templates select="part[not(@partof)]"/>
  </parttree>
</xsl:template>
<xsl:template match="part">
  <part partid="{@partid}" name="{@name}">
    <xsl:apply-templates select="../part[@partof = current( )/@partid]"/>
  </part>
</xsl:template>
<!-- Es stellt sich heraus, dass diese Art der Transformation in XSLT einfacher zu kodieren und zu verstehen ist als in XQuery. Zum Vergleich kommt hier die XQuery-Lösung, die vom W3C-Artikel angeboten wird: -->
define function one_level (element $p) returns element
{
    <part partid="{ $p/@partid }" name="{ $p/@name }" >
        {
            for $s in document("partlist.xml")//part
            where $s/@partof = $p/@partid
            return one_level($s)
        }
    </part>
}

<parttree>
  {
    for $p in document("partlist.xml")//part[empty(@partof)]
    return one_level($p)
  }
</parttree>

Even without XQuery expertise, you can see it requires explicit recursion while XSLT’s apply-templates and pattern matching allow a more declarative solution. The difference is modest, but I find XSLT more elegant here.

  1. Use case “REF”: Reference-based queries. Removed from the latest W3C version.
    References matter in XML. This case presents a reference-heavy database and representative queries.

    File census.xml contains one element per census participant. Each element person records name, occupation and optional spouse as attributes. Attribute spouse is IDREF pointing to ID attribute name of element spouse.

    Parent-child links use nesting. A child appears under one parent, never both, accounting for death, divorce and remarriage. “Children of X” includes the spouse’s children: if spouses Joe and Martha contain Sam and Dave respectively, both children belong to both spouses. Each census person has zero, one or two parents.

    Input document census.xml follows this DTD:

<!DOCTYPE census [
  <!ELEMENT census (person*)>
  <!ELEMENT person (person*)>
  <!ATTLIST person
       name    ID      #REQUIRED
       spouse  IDREF   #IMPLIED
       job     CDATA   #IMPLIED >
]>

Example: Two friendly families linked by several marriages:

<census>
  <person name="Bill" job="Teacher">
    <person name="Joe" job="Painter" spouse="Martha">
      <person name="Sam" job="Nurse">
        <person name="Fred" job="Senator" spouse="Jane"></person>
      </person>
      <person name="Karen" job="Doctor" spouse="Steve"></person>
    </person>
    <person name="Mary" job="Pilot">
      <person name="Susan" job="Pilot" spouse="Dave"></person>
    </person>
  </person>
  <person name="Frank" job="Writer">
    <person name="Martha" job="Programmer" spouse="Joe">
      <person name="Dave" job="Athlete" spouse="Susan"></person>
      <person name="John" job="Artist">
        <person name="Helen" job="Athlete"></person>
        <person name="Steve" job="Accountant" spouse="Karen">
          <person name="Jane" job="Doctor" spouse="Fred"></person>
        </person>
      </person>
    </person>
  </person>
</census>

<!-- Frage 1. Suche Marthas Ehepartner: -->
<xsl:strip-space elements="*"/>
<xsl:template match="person[@spouse='Martha']">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
  </xsl:copy>
</xsl:template>

<!-- Frage 2. Suche Eltern von Sportlern: -->
<xsl:template match="census">
  <xsl:variable name="everyone"  select="//person"/>
  <result>
  <!-- Für jede Person mit Kindern -->
    <xsl:for-each select="$everyone[person]">
      <xsl:variable name="spouse" select="$everyone[@spouse=current( )/@name]"/>
      <xsl:if test="./person/@job = 'Athlete' or $spouse/person/@job = 'Athlete'">
        <xsl:copy>
          <xsl:copy-of select="@*"/>
        </xsl:copy>
      </xsl:if>
    </xsl:for-each>
  </result>
</xsl:template>

<!-- Frage 3. Suche Leute, die den gleichen Beruf haben wie einer ihrer Elternteile: -->

Try it yourself.

<!-- Frage 4. Liste Namen von Eltern und Kindern auf, die den gleichen Beruf haben, und liste deren Berufe auf: -->
<xsl:template match="census">
  <xsl:variable name="everyone"  select="//person"/>
  <result>
    <!-- Für jede Person mit Kindern -->
    <xsl:for-each select="$everyone[person]">
      <xsl:variable name="spouse" select="$everyone[@spouse=current( )/@name]"/>
      <xsl:apply-templates select="person[@job = current( )/@job]">
        <xsl:with-param name="parent" select="@name"/>
      </xsl:apply-templates>
      <xsl:apply-templates select="person[@job = $spouse/@job]">
        <xsl:with-param name="parent" select="$spouse/@name"/>
      </xsl:apply-templates>
    </xsl:for-each>
  </result>
</xsl:template>
<xsl:template match="person">
  <xsl:param name="parent"/>
  <match parent="{$parent}"  child="{@name}" job="{@job}"/>
</xsl:template>

<!-- Frage 5. Liste Namenspaare von Großeltern und Enkeln auf: -->
<xsl:template match="census">
  <xsl:variable name="everyone"  select="//person"/>
  <result>
    <!-- Für jedes Enkelkind -->
    <xsl:for-each select="$everyone[../../../person]">
      <!-- Hole Großelternteil1 (das garantiert existiert für jedes Enkelkind) -->
      <grandparent name="{../../@name}" grandchild="{@name}"/>
      <!-- Hole Großelternteil2, das der Ehepartner von Großelternteil1 ist, falls aufgeführt -->
      <xsl:if test="../../@spouse">
        <grandparent name="{../../@spouse}" grandchild="{@name}"/>
      </xsl:if>
      <!-- Hole die Namen des Ehepartners des Elternteils dieser Person (d.h. je nachdem die Mutter oder den Vater) -->
      <xsl:variable name="spouse-of-parent" select="../@spouse"/>
      <!-- Hole die Eltern des Ehepartners des Elternteils (spouse-of-parent), falls vorhanden -->
      <xsl:variable name="gp3" select="$everyone[person/@name=$spouse-of-parent]"/>
      <xsl:if test="$gp3">
        <grandparent name="{$gp3/@name}" grandchild="{@name}"/>
        <xsl:if test="$gp3/@spouse">
          <grandparent name="{$gp3/@spouse}" grandchild="{@name}"/>
        </xsl:if>
      </xsl:if>
    </xsl:for-each>
  </result>
</xsl:template>

<!-- Frage 6. Suche Leute ohne Kinder: -->
<xsl:strip-space elements="*"/>
<xsl:template match="census">
  <xsl:variable name="everyone"  select="//person"/>
  <result>
    <xsl:for-each select="$everyone[not(./person)]">
      <xsl:variable name="spouse" select="$everyone[@name = current( )/@spouse]"/>
      <xsl:if test="not ($spouse) or not($spouse/person)">
        <xsl:copy-of select="."/>
      </xsl:if>
    </xsl:for-each>
  </result>
</xsl:template>

<!-- Frage 7. Liste die Namen aller Nachfahren von Joe auf. Zeige jeden Nachfahren als Element mit dem Namen des Nachfahrens als Inhalt und seinem oder ihrem Familienstatus sowie der Anzahl der Kinder als Attribute. Sortiere die Nachfahren erstens in absteigender Reihenfolge anhand der Anzahl der Kinder sowie zweitens alphabetisch nach dem Namen: -->
<xsl:variable name="everyone" select="//person"/>
<xsl:template match="census">
  <result>
    <xsl:apply-templates select="//person[@name='Joe']"/>
  </result>
</xsl:template>
<xsl:template match="person">
  <xsl:variable name="all-desc">
    <xsl:call-template name="descendants">
      <xsl:with-param name="nodes" select="."/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:for-each select="exsl:node-set($all-desc)/*">
    <xsl:sort select="count(./* | $everyone[@name = current( )/@spouse]/*)" order="descending" data-type="number"/>
    <xsl:sort select="@name"/>
    <xsl:variable name="mstatus" select="normalize-space(substring('No Yes',boolean(@spouse)* 3+1,3))"/>
    <person married="{$mstatus}" nkids="{count(./* | $everyone[@name = current( )/@spouse]/*)}">
      <xsl:value-of select="@name"/>
    </person>
  </xsl:for-each>
</xsl:template>
<xsl:template name="descendants">
  <xsl:param name="nodes"/>
  <xsl:param name="descendants" select="/.."/>
  <xsl:choose>
    <xsl:when test="not($nodes)">
      <xsl:copy-of select="$descendants"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="descendants">
        <xsl:with-param name="nodes" select="$nodes[position( ) &gt; 1] | $nodes[1]/person | id($nodes[1]/@spouse)/person"/>
        <xsl:with-param name="descendants" select="$descendants | $nodes[1]/person | id($nodes[1]/@spouse)/person"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

This query works but is ugly! Gathering descendants into a sortable node set requires extension function node-set. Function id( ) cannot find spouses because it works relative to each node’s document, while these nodes are copies. Instead locate elements spouse through a variable containing all elements person. Compare XQuery:

define function descrip (element $e) returns element
{
    let $kids := $e/* union $e/@spouse = &gt; person/*
    let $mstatus :=  if ($e[@spouse]) then "Yes" else "No"
    return
        <person married={ $mstatus } nkids={ count($kids) } &gt; { $e/@name/text( ) }</person>


define function descendants (element $e)
{
    if (empty($e/* union $e/@spouse = &gt; person/*)) 
    then $e
    else $e union descendants($e/* union $e/@spouse = &gt; person/*)
}

descrip(descendants(//person[@name = "Joe"])) sortby(@nkids descending, .)

Discussion

XSLT 1.0

This chapter offers assorted ready-made queries rather than generic recipes. XML queries vary widely. W3C’s classification and these XSLT solutions give a foundation for many query tasks.

Space prevents including XQuery for all problems, but comparing approaches is instructive; consult the W3C use-case document.

Commenting every query would be impractical. Basic XSLT knowledge should suffice. Alternatives may improve these solutions, strongly influenced by W3C’s XQuery. I varied constructs, sometimes choosing iterative xsl:for-each, sometimes declarative patterns and xsl:apply-templates.

XSLT 2.0

This long chapter does not repeat every solution in 2.0, but selected problems show improvements with XPath 2.0 and XSLT 2.0:

  • Use for-each-group or distinct-values( ) to eliminate duplicates.

<!-- Frage 4 (aus Beispiel "prices.xml"). Liste für jeden Autor in bib.xml den Namen des Autors und die Titel aller Bücher dieses Autors auf, und zwar in einem "result"-Element gruppiert: -->
<xsl:for-each-group select="//author" group-by=".">
  <result>
    <xsl:copy-of select="."/>
    <xsl:for-each select="/bib/book[author eq current-grouping-key( )]">
      <xsl:copy-of select="title"/>
    </xsl:for-each>
  </result>
</xsl:for-each-group>
<!-- Verwendung von distinct-values( ) -->
<xsl:for-each select="distinct-values(//author)">
  <result>
    <xsl:copy-of select="."/>
    <xsl:for-each select="/bib/book[author eq .]">
       <xsl:copy-of select="title"/>
    </xsl:for-each>
  </result>
</xsl:for-each>

  • Avoid copying nodes by using xsl:sequence.

<!-- Frage 8. Suchen Sie im Dokument books.xml alle Abschnitts-(section-) oder Kapiteltitel (chapter), die das Wort "XML" enthalten, und zwar ungeachtet ihrer Schachtelungsebene: -->
<results>
  <xsl:sequence select="(//chapter | //section)/title)[contains(.,'XML')]"/>
</results>

  • Use functions and sequences to simplify queries.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ckbk="http://www.ora.com/XSLTCkbk">
  <xsl:key name="person-key" match="person" use="@name"/>
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:variable name="everyone" select="//person" as="item( )*"/>
  <xsl:template match="census">
    <result>
      <xsl:apply-templates select="//person[@name='Joe']"/>
    </result>
  </xsl:template>
  <xsl:template match="person">
    <!-- Kein Bedarf für die Konvertierung von Knotenmengen. Verwenden Sie die descendants-Funktion direkt -->
    <xsl:for-each select="ckbk:descendants(.,/)[current( ) != .]">
      <xsl:sort select="count(./* | $everyone[@name = current( )/@spouse]/*)" order="descending" data-type="number"/>
      <xsl:sort select="@name"/>
      <xsl:variable name="mstatus" select="if (@spouse) then 'Yes'   else 'No'"/>
      <person married="{$mstatus}" nkids="{count(./* | key('person-key', @spouse)/*)}">
        <xsl:value-of select="@name"/>
      </person>
    </xsl:for-each>
  </xsl:template>
  <!-- Beachten Sie, dass diese Funktion einfacher ist als das Template in der XSLT 1.0-Lösung. Wir übergeben sie im Dokument, da sie in Funktionen unbekannt ist.-->
  <xsl:function  name="ckbk:descendants">
    <xsl:param name="nodes" as="item( )*"/>
    <xsl:param name="doc"/>
    <xsl:sequence select="$nodes, for $person in $nodes return ckbk:descendants( ($person/person, key('person-key', $person/@spouse,$doc)/person), $doc)"/>
  </xsl:function>
</xsl:stylesheet>

See also

Evan Lenz also discusses XSLT in place of XQuery in XQuery: Reinventing the Wheel?. For a detailed XQuery introduction, see XQuery – Introduction to querying XML data.

  

Back to top

 

 

back to top