views:

285

answers:

2

I want to following xml file output:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
- <T0020 xsi:schemaLocation="http://www.safersys.org/namespaces/T0020V1 T0020V1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.safersys.org/namespaces/T0020V1"&gt;
- <INTERFACE>
  <NAME>SAFER</NAME> 
  <VERSION>04.02</VERSION> 
  </INTERFACE>

for that i have following xslt file:

<xsl:template match="T0020" >
    <xsl:copy>
    <xsl:attribute name="xsi:schemaLocation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;http://www.safersys.org/namespaces/T0020V1 T0020V1.xsd </xsl:attribute>

  //some code here...............//

 <xsl:copy>

so i add xmlns="http://www.safersys.org/namespaces/T0020V1" attribute under <T0020> tag??

A: 

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*">
        <xsl:element name="{name()}" namespace="http://www.safersys.org/namespaces/T0020V1"&gt;
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="T0020">
        <xsl:element name="{name()}" namespace="http://www.safersys.org/namespaces/T0020V1"&gt;
            <xsl:attribute name="xsi:schemaLocation">http://www.safersys.org/namespaces/T0020V1 T0020V1.xsd</xsl:attribute>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

With this input:

<T0020>
    <INTERFACE>
        <NAME>SAFER</NAME>
        <VERSION>04.02</VERSION>
    </INTERFACE>
</T0020>

Output:

<T0020 xsi:schemaLocation="http://www.safersys.org/namespaces/T0020V1 T0020V1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.safersys.org/namespaces/T0020V1"&gt;
    <INTERFACE>
        <NAME>SAFER</NAME>
        <VERSION>04.02</VERSION>
    </INTERFACE>
</T0020>

Note: Namespace node are not attributes nodes. If you want that elements in no namespace gets output under some namespace you need the xsl:element/@namespace.

Alejandro
+2  A: 

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vDefaultNS"
  select="'http://www.safersys.org/namespaces/T0020V1'"/&gt;

 <xsl:template match="*">
  <xsl:element name="{name()}" namespace="{$vDefaultNS}">
   <xsl:copy-of select="namespace::* | @*"/>
   <xsl:apply-templates/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<T0020 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.safersys.org/namespaces/T0020V1 T0020V1.xsd"
>
 <INTERFACE>
  <NAME>SAFER</NAME>
  <VERSION>04.02</VERSION>
 </INTERFACE>
</T0020>

produces the wanted result:

<T0020 xmlns="http://www.safersys.org/namespaces/T0020V1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.safersys.org/namespaces/T0020V1 T0020V1.xsd">
   <INTERFACE>
      <NAME>SAFER</NAME>
      <VERSION>04.02</VERSION>
   </INTERFACE>
</T0020>

Do note that xmlns is not an attribute, but denotes a namespace declaration.

Dimitre Novatchev
@Dimitre: +1 for short solution. But It looks like the provided stylesheet fragment is adding an schema instance declaration because maybe before there was no schema. That also could be the cause for changing elements namespace.
Alejandro
@Alejandro: I don't want to guess here. THe question was "How to add xmlns"... so I am just showing this.
Dimitre Novatchev