Re: [xsl] More on my problem with namespaces

Subject: Re: [xsl] More on my problem with namespaces
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Tue, 09 Nov 2010 16:10:35 +0000
Namespaces are not attributes in the XDM view of the world, so you can't create them using the xsl:attribute instruction. Use the xsl:namespace instruction instead. Alternatively, since you know statically what namespace you want output, you can just add the declaration xmlns:ns3="http://www.w3.org/2001/XMLSchema"; to the xsl:stylesheet element.

Or more simply, you could rewrite

<xsl:element name="calypso:value">
                       <xsl:attribute name="xsi:type">ns3:string</xsl:attribute>
                       <xsl:attribute
name="ns3">http://www.w3.org/2001/XMLSchema</xsl:attribute>
                       <xsl:value-of select="."/>
                   </xsl:element>


as


<calypso:value xsi:type="ns3:string"
               xmlns:ns3="http://www.w3.org/2001/XMLSchema";

   <xsl:value-of select="."/>
</calypso:value>

Michael Kay
Saxonica

On 09/11/2010 15:59, Nick Leaton wrote:
I thought I had a solution to my namespace problem, but on
investigation I haven't
On further investigation, I haven't got it going, so I've produced a
cut down version
showing the problem

here is a test xml file

===================
<?xml version="1.0" encoding="UTF-8"?>
<bonds>
    <isin>123</isin>
    <isin>456</isin>
    <isin>789</isin>
</bonds>
===================


Here is the xslt cut down =================== <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:calypso="http://www.calypso.com/xml"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; version="2.0">

<xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <bonds>
            <xsl:for-each select="//isin">
            <bond>
                <calypso:secCode>
                    <calypso:name>ISIN</calypso:name>
                    <xsl:element name="calypso:value">
                        <xsl:attribute name="xsi:type">ns3:string</xsl:attribute>
                        <xsl:attribute
name="ns3">http://www.w3.org/2001/XMLSchema</xsl:attribute>
                        <xsl:value-of select="."/>
                    </xsl:element>
                </calypso:secCode>
            </bond>
        </xsl:for-each>
        </bonds>
    </xsl:template>
</xsl:stylesheet>

===================

Here is the output

===================
<?xml version="1.0" encoding="UTF-8"?>
<bonds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
       xmlns:calypso="http://www.calypso.com/xml";>
   <bond>
      <calypso:secCode>
         <calypso:name>ISIN</calypso:name>
         <calypso:value xsi:type="ns3:string"
ns3="http://www.w3.org/2001/XMLSchema";>123</calypso:value>
      </calypso:secCode>
   </bond>
   <bond>
      <calypso:secCode>
         <calypso:name>ISIN</calypso:name>
         <calypso:value xsi:type="ns3:string"
ns3="http://www.w3.org/2001/XMLSchema";>456</calypso:value>
      </calypso:secCode>
   </bond>
   <bond>
      <calypso:secCode>
         <calypso:name>ISIN</calypso:name>
         <calypso:value xsi:type="ns3:string"
ns3="http://www.w3.org/2001/XMLSchema";>789</calypso:value>
      </calypso:secCode>
   </bond>
</bonds>
===================

Here is the desired output

===================
<?xml version="1.0" encoding="UTF-8"?>
<bonds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
       xmlns:calypso="http://www.calypso.com/xml";>
   <bond>
      <calypso:secCode>
         <calypso:name>ISIN</calypso:name>
         <calypso:value xsi:type="ns3:string"
xmlns:ns3="http://www.w3.org/2001/XMLSchema";>123</calypso:value>
      </calypso:secCode>
   </bond>
   <bond>
      <calypso:secCode>
         <calypso:name>ISIN</calypso:name>
         <calypso:value xsi:type="ns3:string"
xmlns:ns3="http://www.w3.org/2001/XMLSchema";>456</calypso:value>
      </calypso:secCode>
   </bond>
   <bond>
      <calypso:secCode>
         <calypso:name>ISIN</calypso:name>
         <calypso:value xsi:type="ns3:string"
xmlns:ns3="http://www.w3.org/2001/XMLSchema";>789</calypso:value>
      </calypso:secCode>
   </bond>
</bonds>
===================


The difference is that I need


ns3="http://www.w3.org/2001/XMLSchema";

output as

xmlns:ns3="http://www.w3.org/2001/XMLSchema";


Any ideas? -- Nick

Current Thread