[xsl] accessing xsl document root from an included stylesheet

Subject: [xsl] accessing xsl document root from an included stylesheet
From: Jan Bilik <jan.bilik.w@xxxxxxxxx>
Date: Fri, 4 Dec 2009 12:44:54 +0100
Hello,

I am using xslt 1.0.

I am managing my own namespace, let's say "http://mynamespace/v1.1";,
associated to a stable alias, let's say "myn": In order to access my
XML elements linked to this namespace, I have the following XSL
header:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0"
	xmlns:myn="http://mynamespace/v1.1";>

When I make changes to my grammar,  I update the version number in my
namespace, in order to avoid running "old" stylesheets onto new XMLs.
Thus, my new XSL header looks like:

 <xsl:stylesheet ...
	xmlns:myn="http://mynamespace/v1.2";>


As I want not to process anything if myn versions do not match between
the XSL and the XML being processed, I have added the following code
at the beginning of my stylesheet:

 <!-- 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 ! If the namespace for the "myn" alias is not the same in
my XSL and my XML, the message is displayed and process is aborted.

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:

mynmatch.xsl:
---------
 <?xml version="1.0" encoding="UTF-8"?>

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

 <!-- Check RCD 1.2 namespace version of input XML -->
<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>

 </xsl:stylesheet>


mainstylesheet.xsl:
---------
<?xml version="1.0" encoding="UTF-8"?>

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0"
	xmlns:myn="http://mynamespace/v1.2";>

<xsl:include href="mynmatch.xsl" />

<xsl:output method="text" encoding="UTF-8" />

...


Anybody knows if there is a way to access the xmlns:myn value of
mainstylesheet.xsl from mynmatch.xsl ??

Jan

Current Thread