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.

4

3 回答 3

4

你要求的东西是不可能的!XML 命名空间是 XML 语言的一部分,这就像要求剥离 Java 中的所有包或 C# 中的命名空间!

简而言之,您期望的 XML 输出是一个无效的 XML 文档,因此您不能从 XSLT 创建它,该 XSLT 旨在生成有效的 XML。

您可以通过从 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>

这会产生以下结果:

<wireframe>
    <secureInbox>
        <alertHeader>
            hello
        </alertHeader>
    </secureInbox>
</wireframe>
于 2012-03-13T08:26:32.187 回答
3

尽管这是非常糟糕的做法,但实际上可以生成这种格式不正确的 XML。您可以将 XSLT 的输出类型设置为文本,然后生成无命名空间标签,如下所示:

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

等等..

就个人而言,我认为这是“不要在家尝试这个”类别,但如果你不为正确的 xslt 风格付出一分钱,那就去吧!

于 2012-03-13T19:32:05.700 回答
2

您可以在 exclude 属性中省略两个使用的命名空间,如下所示:

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

这将确保使用的两个命名空间出现在根元素中,而不是出现在第一次使用它们的元素中;结果将是:

<?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>
于 2012-03-13T09:39:16.520 回答