[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] xsl:namespace


Subject: [xsl] xsl:namespace
From: Jesper Tverskov <jesper@xxxxxxxxxxx>
Date: Fri, 20 Feb 2009 14:34:46 +0100

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.

Cheers,
Jesper Tverskov

http://www.xmlkurser.dk
http://www.xmlplease.com


Current Thread
Keywords
xsl