How can we remove name space in level 1 elements o

2019-08-17 06:12发布

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条回答
贪生不怕死
2楼-- · 2019-08-17 06:43

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>
查看更多
登录 后发表回答