Re: [xsl] xsl:namespace

Subject: Re: [xsl] xsl:namespace
From: "Vladimir Nesterovsky" <vladimir@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 20 Feb 2009 17:54:30 +0200
Have I succeded?

No. :-)


Examples you have used are too artificial and can be written without xsl:namespace.

I would suggest an example when one needs to output QName attribute.

<xsl:variable name="value" as="xs:QName" select="..."/>

<element value="{$value}">
 <xsl:namespace
   name="{prefix-from-QName($value)}"
   select="{namespace-uri-from-QName($value)}/>
...
</element>

P.S. This in fact raises another question on how to correctly handle attributes with QName values.
--
Vladimir Nesterovsky
http://www.nesterovsky-bros.com/



I'm trying to make a code example that makes xsl:namespace
understandable. In my opinion one of the most difficult xsl elements.
Now here is my question: Have I succeded?

Output:
  <price xmlns:xs="http://www.w3.org/2001/XMLSchema";
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
         xsi:type="xs:decimal">23.50</price>

Stylesheet:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output indent="yes"/>
<xsl:param name="ns-prefix" select="'xs'"/>
<xsl:param name="ns-identifier"
select="'http://www.w3.org/2001/XMLSchema'"/>


   <xsl:template name="start">
       <test>

<!-- alternative 1 -->
       <xsl:element name="price">
           <xsl:attribute name="xsi:type" select="'xs:decimal'"
namespace="http://www.w3.org/2001/XMLSchema-instance"/>
           <xsl:namespace name="xs"
select="'http://www.w3.org/2001/XMLSchema'"/>
           <xsl:text>23.50</xsl:text>
       </xsl:element>

<!-- alternative 2 -->
       <price xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xml:xs="http://www.w3.org/2001/XMLSchema"; xsi:type="xs:decimal">
           <xsl:text>23.50</xsl:text>
       </price>

<!-- alternative 3 -->
       <price xsi:type="{$ns-prefix}:decimal"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance'">
           <xsl:namespace name="{$ns-prefix}" select="$ns-identifier"/>
           <xsl:text>23.50</xsl:text>
       </price>

</test>

   </xsl:template>
</xsl:stylesheet>

1) When we create the price element using xsl:element, we only need
xsl:namespace to create the namespace for the attribute value.

2) When we create the price element the literal way we don't need
xsl:namespace. We can put the needed namespace declarations into the
literal element directly. We can even put them in the xsl:stylesheet
element and they will end up in output as we want it.

3) If we want a dynamic solution, we need xsl:namespace even when
creating the output element the literal way. We can now change "xs" to
"xsd" with a parameter from outside.

Current Thread