Re: [xsl] xslt 2.0 The namespace axis. Help or waste of space?

Subject: Re: [xsl] xslt 2.0 The namespace axis. Help or waste of space?
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 12 Oct 2009 09:02:38 -0400
At 2009-10-12 13:25 +0100, Dave Pawson wrote:
Seems to have come up as a topic recently.
It seems to be a rarely used axis. Is this true for you?

Not at all ... it is very useful.

If you use it, how?

(1) - accessing the namespace being used in a stylesheet without hard-coding it into template rules
- keeping with the maintenance principle of define once and re-use, one could use an internal declaration subset and entity for a namespace, or one could just define the namespace at the top of a stylesheet and then load a variable with the namespace-uri of the known stylesheet prefix (during the development of CVA files the namespace was changing as the vocabulary was changing, and I only had to change the namespace declaration at the top of my stylesheet rather than also changing it inside my stylesheet):

 <xsl:stylesheet xmlns:cva="....OASIS context/value association ns ...."
  <xsl:variable name="ccs:cva-namespace"
    <!--check the namespace of the input document element-->
    <xsl:if test="namespace-uri(/*)=$ccs:cva-namespace">
      ... unexpected input ...

(2) - synthesizing Schematron namespace declarations:
- without the namespace axis in XSLT 1.0 how would I read an arbitrary XML document and create declarations such as:

<xsl:variable name="ns"
select="$assoc-file//namespace::*[name(.) != 'xml' and
. != $ccs:cva-namespace and . != $ccs:sch-namespace]"/>
<xsl:for-each select="$ns">
<xsl:if test="generate-id(.)=generate-id($ns[name(.)=name(current())][1])">
<ns prefix="{name(.)}" uri="{.}"/>

(3) - any time one is synthesizing XPath addresses from an XML document
- at any point in time when you are in the XML document and you are synthesizing XSLT instructions with XPath addresses, reading the namespace axis equips you to write enough namespace declarations from the XML to have all prefixes in your XPath expression be resolved

(4) - finding the namespace prefix being used in an XSD schema for the target namespace:

select="name( /*/namespace::*[ . = /xsd:schema/@targetNamespace ] )"/>

(5) - generally useful when writing stylesheets that write stylesheets from XML documents as the synthesized stylesheet needs namespace declarations for the namespaces found in the XML document in order for the XPath addresses to work

(6) - a namespace is an important piece of the information set like any other piece of the information set so any time you need to know it, it is useful to walk the axis and find it
- I could probably find other examples in my code elsewhere, but you probably get the gist above ... for example in my UBL work I write "XPath files" which are enumerations of equivalent XPath addresses ... and for XPath expressions to be correct one need only know namespaces in scope that are relevant (not just *all* namespaces)

What makes it worthwhile in xslt 2.0?

It was worthwhile in XSLT 1.0 and I was saddened to see it deprecated in XSLT 2.0 (and invisible in XQuery 1) as I'll have to rework my algorithms to use the available XPath 2.0 functions if I need to write something portable across implementations.

I have no entries in the faq and have never used it myself.

I wish more people would use it. I think the Schematron vocabulary should never have had the explicit <ns/> element because all that is necessary is use actual XML namespace declarations in the elements and the skeleton could have read the namespace axis in order to synthesize the required XSLT. Adding a redundant (and required!) piece of information has been a bit of a burden in my code list XSLT validation work using Schematron, when it could have been done without it.

I hope this helps.

. . . . . . . . . . . . Ken

Upcoming: hands-on code list, UBL, XSLT, XQuery and XSL-FO classes
in Copenhagen Denmark and Washington DC USA, October/November 2009
Interested in other classes?
Crane Softwrights Ltd.
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:
Video overview:
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Male Cancer Awareness Nov'07
Legal business disclaimers:

Current Thread