How can we remove name space in level 1 elements of the xml using xslt

荒凉一梦 提交于 2019-12-12 03:56:19

问题


I have this xml

<Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <UserInfo xmlns="">
      <name>ss</name>
      <addr>XXXXXX</addr>
     </UserInfo>
</Request>

I want the output xml as

<Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       <UserInfo>
          <name>ss</name>
          <addr>XXXXXX</addr>
         </UserInfo>
    </Request>

Please help me in xsl..


回答1:


Your input and output are semantically the same, but if you want to remove that xmlns="", this will work:

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

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

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

When run on your sample input, the result is:

<Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <UserInfo>
    <name>ss</name>
    <addr>XXXXXX</addr>
  </UserInfo>
</Request>


来源:https://stackoverflow.com/questions/15978027/how-can-we-remove-name-space-in-level-1-elements-of-the-xml-using-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!