开发者

remove namespace for a particular element

开发者_运维问答

Is there any way to remove namespace for a particular element?


This transformation:

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

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="x:*">
  <xsl:element name="{local-name()}">
    <xsl:copy-of select="namespace::*[not(. = namespace-uri(..))]"/>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

removes any element from the "my:x" namespace and puts it into "no namespace".

For example, when applied to the following XML document:

<t>
  <x:a  xmlns:x="my:x"/>
</t>

the wanted, correct result is produced:

<t>
   <a/>
</t>

To remove only a specific element from a specific namespace, the template overriding the identity rule must just be made more specific to match only the wanted element.


Your XSLT output is a fresh object (Node or text) and you can copy or transform each element to a new one without a namespace. Assume your input uses a namespace "http://www.foo/". You create elements of the same name (and possibly the same children) without a namespace.

<xsl:stylesheet xmnls:h="http://www.foo/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<!-- other elements and root omitted -->

    <xsl:template match="h:table">
      <table>
        <!-- copy or transform attributes and children -->
      </table>
    </xsl:table>

</xsl:stylesheet>

will create a new node with the default namespace which doesn't require a prefix and I think is what you need. [There may be more elegant ways using xsl:copy... -

UPDATE: ... and @Dmitri has shown them!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜