XML technologies / XSLT / XSLT Cookbook / Querying XML

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

<xsl:template name="frage-von-kind">
  <xsl:with-param name="eltern" select=" 'Papa' "/>
  <xsl:value-of select="concat('Aber warum' '$eltern' '?')"/>
  <xsl:apply-templates select="vernuenftige_antwort"/>
  <xsl:call-template name="frage-von-kind">
    <xsl:with-param name="eltern" select="$eltern"/>
  </xsl:call-template>
</xsl:template>
<!-- Eltern, die Endrekursion nicht erkennen, riskieren, dass ihr Stack überläuft.-->

Introduction

This chapter presents recipes for using XSLT as an XML query language. Querying XML means extracting information from one or more XML documents to answer questions about facts and relationships within and between them. Querying an XML document involves the same kinds of questions as querying a relational database using SQL.

The “official” XML query language promoted by the W3C is not XSLT but XQuery. XSLT and XQuery share many similarities, but also important differences. For example, both use XPath. However, an XSLT script always uses XML syntax, whereas an XQuery script has both a human-friendly syntax and an XML syntax.

The idea of an XML query language separate from XSLT was controversial. Many in the XML community thought the overlap would be excessive. Indeed, any XQuery query could also be implemented in XSLT. In many cases, the XSLT solution is just as concise. XQuery’s advantage is that it is generally easier to understand than equivalent XSLT. Those familiar with SQL should find XQuery’s learning curve much gentler. However, understanding depends on familiarity, so these comparisons are not absolute.

A thorough explanation of XQuery or detailed comparison with XSLT is beyond this chapter’s scope. Instead, the query examples here help readers who have already invested time in XSLT and do not want to learn another XML-related language.

Examples of every possible XML query would be impossible. This chapter takes a two-part approach: first, simple, broadly applicable query examples provide building blocks that can be adapted to more complex problems. Second, a recipe gives solutions for most XML use cases in the W3C document XML Query Use Cases. Often, one of these solutions will resemble your own query problem. You then only need to adapt it to your XML data.

  

  

 

 

 

back to top