1

I am working on XML to XML transformations through XSLT. I want to remove the name spaces in output xml. For that I have used Exclude result prefix option, but in the output i still see the namespaces.

Sorce XML:

 <?xml version="1.0" encoding="ISO-8859-1"?>
 <aaa>
 hello
 </aaa>

XSLT written:

 <?xml version="1.0" encoding="utf-8"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"      xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:simple="aaaa" xmlns:xlink="http://www.w3.org/1999/xlink"      xmlns:tcm="http://www.tridion.com/ContentManager/5.0" exclude-result-prefixes="msxsl simple wireframe widget tcdl tcm xlink"      xmlns:wireframe="bbb" xmlns:widget="ccc" xmlns:tcdl="tcdl">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
 <xsl:template match="/">
  <wireframe:wireframe>
       <wireframe:si>
         <widget:ah>
         <xsl:value-of select="aaa" />
               </widget:ah> 
         </wireframe:si>
 </wireframe:wireframe>
 </xsl:template>
   </xsl:stylesheet>

OUTPUT produced:

 <?xml version="1.0" encoding="utf-8"?>
 <wireframe:wireframe xmlns:wireframe="aaaa">
   <wireframe:si>
     <widget:ah xmlns:widget="bbb">
 hello
 </widget:ah>
   </wireframe:si>
 </wireframe:wireframe>

Output Expexcted:

 <?xml version="1.0" encoding="utf-8"?>
 <wireframe:wireframe>
   <wireframe:si>
     <widget:ah>
 hello
 </widget:ah>
   </wireframe:si>
 </wireframe:wireframe>

Please tell me how to avoid namespace appearance in output XML.

Thank you in advance.

3
  • Can you provide a snippet of the offending XML and XSLT files. Commented Mar 13, 2012 at 8:11
  • The expected output you provided is not valid XML, since it lacks namespace definitions. Why do you even need it? Commented Mar 13, 2012 at 8:40
  • Just for clarification, are you trying to create references to user controls, taglibs or something simila? As newtover asks, it is not clear why you need the prefixes. Commented Mar 13, 2012 at 12:01

3 Answers 3

4

You could leave out the two used namespaces in the exclude attribute as follows:

exclude-result-prefixes="msxsl simple xlink tcm tcdl"

which will make sure that the two namespaces which are used appear in the root element, not in the element where they are first used; the result will be:

<?xml version="1.0" encoding="UTF-8"?>
<wireframe:wireframe xmlns:widget="ccc" xmlns:wireframe="bbb">
    <wireframe:si>
        <widget:ah>
 hello
 </widget:ah>
    </wireframe:si>
</wireframe:wireframe>
Sign up to request clarification or add additional context in comments.

Comments

4

You are asking for something that is not possible! An XML namespace is part of the language of XML, this is like asking to strip of all the packages in Java or the namespaces in C#!

In a nutshell, you expected XML output is an invalid XML document and hence you cannot create this from XSLT, which is designed to produce valid XML.

You could remove the namespace prefixes altogether by removing them from your XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
    <xsl:template match="/">
        <wireframe>
            <secureInbox>
                <alertHeader>
                    <xsl:value-of select="aaa" />
                </alertHeader>
            </secureInbox>
        </wireframe>
    </xsl:template>
</xsl:stylesheet>

Which produces the following result:

<wireframe>
    <secureInbox>
        <alertHeader>
            hello
        </alertHeader>
    </secureInbox>
</wireframe>

Comments

3

Although it is horribly bad practice, it is in fact possible to generate this non-wellformed XML. You can set the output type of the XSLT to text, and then generate your namespace-less tags as follows:

<xsl:text disable-output-escaping="yes">&amp;lt;wireframe:wireframe&amp;gt;</xsl:text>

and so on..

Personally, I consider this one for the 'do not try this at home' category, but if you don't give a dime for proper xslt style, go ahead!

2 Comments

I wouldn't be such a purist. There are often good reasons to do something like this. Say for example, you wanted to generate a fragment of serialised XML to embed within a larger document. As soon as you specify the output type as text, it isn't XML any more! Anyway, +1 for actually answering the question.
I can't give you a +1, like Dominic, I argue this is not outputting valid XML, so not really answering the question. Had the question been "I am working on XML to Text transformations through XSLT..." you would get my vote.

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.