Re: [xsl] Can a single XPath statement duplicate the functionality of this verbose <xsl:choose> statement?

Subject: Re: [xsl] Can a single XPath statement duplicate the functionality of this verbose <xsl:choose> statement?
From: Andrew Welch <andrew.j.welch@xxxxxxxxx>
Date: Mon, 24 Oct 2011 14:09:16 +0100
>  But I would love to get my hands on
> more examples like your <drink type="beer"/> resolutions.

I have started writing a 'book', well it's more of a pamphlet, that
you never know one day might get finished.

> I still do not understand, for instance, the emphasis in the literature in
> distinguishing between the "root" and "the document node"; it seems not to
> have any impact on writing code.

The document node contains the root element.  So for example:

[ document root]
<html>
  <head> ..</head>
  <body> ..</body>
</html>
[ /document root]

The <html> element is the "root element".  The "document node"
contains that root element, and has properties associated with it such
as the document uri.  The term "root node" is a little ambiguous as
it's the document-node() if it exists, or the root element if it
doesn't.

In general it won't affect your code much, but a common issue is when
you create variables and then change the sequence type:

<xsl:variable name="types">
  <type>A</type>
  <type>B</type>
  <type>C</type>
</xsl:variable>

Here the variable $types contains a document-node() (which is created
implicitly) that contains 3 <type> elements (the single root element
restriction doesn't apply to temporary trees, or the result tree).

It's equivalent to:

<xsl:variable name="types">
  <xsl:document>
    <type>A</type>
    <type>B</type>
    <type>C</type>
  </xsl:document>
</xsl:variable>

Those <type> elements are siblings, because they share a common parent
- the document-node().  You can access (say) the first <type> using
$types/type[1], because $type returns the document node, and /type[1]
returns its first <type> child.

If you change the sequence type of the variable to:

<xsl:variable name="types" as="element(type)+">
  <type>A</type>
  <type>B</type>
  <type>C</type>
</xsl:variable>

The variable $types now contains 3 parentless <type> elements.  They
are no longer siblings because they don't share a common parent, and
you access the first one using $types[1] because $types return the 3
elements.



-- 
Andrew Welch
http://andrewjwelch.com

Current Thread