RE: [xsl] accessing xsl document root from an included stylesheet

Subject: RE: [xsl] accessing xsl document root from an included stylesheet
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 4 Dec 2009 12:14:10 -0000
>  <!-- Check namespace version -->
>  <xsl:template match="/">
> 	<xsl:variable name="xslmyn"><xsl:value-of 
> select="document('')/*/namespace::myn" /></xsl:variable>
> 	<xsl:variable name="matchmyn"><xsl:value-of 
> select="*/namespace::myn[.=$xslmyn]" /></xsl:variable>
> 	<xsl:if test="$matchmyn = ''">
> 		<xsl:message terminate="yes">Mismatch MYN 
> namespace version !!</xsl:message>
> 	</xsl:if>
> 	<xsl:apply-templates select="@*|node()"/>  </xsl:template>
> 
> And it works ! 

Try to avoid this construct:

<xsl:variable name="xslmyn">
         <xsl:value-of select="document('')/*/namespace::myn"
/></xsl:variable>

It's both verbose and inefficient compared with the much simpler

<xsl:variable name="xslmyn" select="document('')/*/namespace::myn" />

because if creates a result tree fragment containing a text node, rather
than just a pointer to your existing namespace node.

> 
> Here comes my question : I would like to put the above xsl 
> template in a separate stylesheet and to include it into each 
> XSL. Alas, by doing so, the "document('')" (see xmlmyn 
> variable) does no longer match the main stylesheet root, but 
> the included one

Namespaces declared in the stylesheet are scoped to the stylesheet module,
so you really ought to be testing that the namespace is declared correctly
in every module. You can follow the links to included and imported modules
by using document(/*/xsl:include/@href | /*/xsl:include/@href) (which needs
to be done recursively, of course). But there's no way of disovering in an
included module where it was included from.

However, it all seesm very long-winded to me. Why don't you just do

<xsl:template match="/">
  <xsl:if test="not(myn:rootElement)">
     <xsl:message terminate="yes">Root element must be named myn:rootElement
in the correct namespace</xsl:message>


Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 

Current Thread