Re: [xsl] Temporary tree elements and namespaces: Further question

Subject: Re: [xsl] Temporary tree elements and namespaces: Further question
From: "Sam Byland" <shbyland@xxxxxxxxxxx>
Date: Thu, 14 Feb 2008 14:41:58 -0500
Richard,

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

Since you are using XSLT 1.0, setting the default namespace (xmlns=http://www.w3.org/1999/xhtml) will affect literal result elements heading to the output document, so in this case any literal result element written to the output document will end up in the "http://www.w3.org/1999/xhtml"; namespace by default.


For pattern matching, expressions, etc., in the stylesheet, names with no prefix will still refer to the null namespace. So the following:

<mappings>
<relationship topic="1561.htm" toc="toc9497.htm" />
<relationship topic="950.htm" toc="toc.htm" />
.
.
</mappings>

Which I load into a variable as follows:

<xsl:variable name="mappings"
select="document('map.map')/mappings/relationship" />

Which I'm assuming loads _all_ the relationships into $mappings.

is correct -- all relationships will be selected because no prefix means no namespace, and as you indicated, map.map elements are in the null namespace.


<xsl:value-of select="$mappings/relationship[@topic=@href]/@toc">

Where @href is in the default xhtml namespace and @topic and @toc are in the
new namespace.


The question is, where do I declare the new namespace and how do I ensure it
is applied to the relationships and attributes in map.map?

Even with Florent's correction:


<xsl:value-of select="mappings[@topic eq @href]/@toc"/>

I'm not sure what you're looking to do here since @href is not an attribute of anything in $mappings. However, you seem to indicate that @href is from another file perhaps, and is an attribute of an element that is in the http://www.w3.org/1999/xhtml namespace.... You can still handle that since you also defined the xhtml prefix to be bound to that namespace. Example:

Assume map.map is the following XML document:

<mappings>
  <relationship topic="1561.htm" toc="toc9497.htm"/>
  <relationship topic="950.htm" toc="toc.htm"/>
</mappings>

and assume Test.xml is the following XML document:

<SomeElement xmlns="http://www.w3.org/1999/xhtml"; href="1561.htm"/>

Then the following XSLT (adapted from your snippets of code and includes Florent's correction):

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

<xsl:variable name="mappings" select="document('map.map')/mappings/relationship" />

<xsl:variable name="xhtmlMappings" select="document('Test.xml')"/>

<xsl:template match="mappings">

<LiteralResultElement>

<xsl:value-of select="$mappings[@topic=$xhtmlMappings/xhtml:SomeElement/@href]/@toc"/>

</LiteralResultElement>

</xsl:template>

</xsl:stylesheet>

will output the following XML:

<LiteralResultElement xmlns="http://www.w3.org/1999/xhtml"; xmlns:xhtml="http://www.w3.org/1999/xhtml";>toc9497.htm</LiteralResultElement>

which you will note is an element in the http://www.w3.org/1999/xhtml namespace...

Does that help?

Cheers,

...sam

Current Thread