RE: [xsl] Attributes?

Subject: RE: [xsl] Attributes?
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Fri, 2 Jul 2004 09:07:43 +0100
>     In XSLT 1.0, match="g" matches exactly the same elements as 
>     match="//g".
>     There's a slight difference in 2.0, because match="//g" 
>     will only match g elements that are in a tree with a 
>     document node as its root - which means the processor has 
>     extra checks to do in this case.
> Confused. Don't *all* elements exist in a tree with a document
> node as its root?
> Under what circumstances might an element *not* have such a 
> root please?

No, XSLT 2.0 (and XQuery) allow you to create free-standing nodes. For
example, there was a question a few days ago (I think on the Saxon list
rather than here) from someone who wanted to write a function that returned
a set of attribute nodes:

<xsl:function name="my:atts" as="attribute()">
  <xsl:param name="p"/>
  <xsl:attribute name="a" select="{$p+1}"/>
  <xsl:attribute name="b" select="{$p+2}"/>

(Essentially the same as an attribute-set, except that attribute sets do not
allow parameters.)

You can copy these attributes to an element, like this:

 <xsl:copy-of select="my:atts(17)"/>

But you can also apply-templates to them:

<xsl:apply-templates select="my:atts(29)"/>

If you try to find the parent of one of these attributes, you find it hasn't
got one:

<xsl:value-of select="count(my:atts(6)/..)"/> displays "0".

And of course the same applies to elements.

Michael Kay

Current Thread