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">
- <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">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??

1
  • Good question (+1). See my answer for a short and simple solution. :) Commented Jul 27, 2010 at 13:39

2 Answers 2

2

This transformation:

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

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

 <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.

Sign up to request clarification or add additional context in comments.

2 Comments

+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: I don't want to guess here. THe question was "How to add xmlns"... so I am just showing this.
1

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">
    <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">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="T0020">
        <xsl:element name="{name()}" namespace="http://www.safersys.org/namespaces/T0020V1">
            <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">
    <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.

1 Comment

+1 for a very good solution that simply addresses the problem with no digression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.