使用XSL删除所有XML命名空间,但只删除一个

时间:2014-10-30 17:23:09

标签: xml xslt namespaces

我在另一篇文章中使用了一些XSL代码,从我的XML文档中删除了所有名称空间。

我使用了以下代码:



<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes"/>

    <!-- Stylesheet to remove all namespaces from a document -->
    <!-- NOTE: this will lead to attribute name clash, if an element contains
        two attributes with same local name but different namespace prefix -->
    <!-- Nodes that cannot have a namespace are copied as such -->

    <!-- template to copy elements -->
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* | node()"/>
        </xsl:element>
    </xsl:template>

    <!-- template to copy attributes -->
    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>

    <!-- template to copy the rest of the nodes -->
    <xsl:template match="comment() | text() | processing-instruction()">
        <xsl:copy/>
    </xsl:template>
	
</xsl:stylesheet>
&#13;
&#13;
&#13;

1-有人能给我一个关于这段代码如何工作的简单解释吗? 2-如何删除文档的所有命名空间,但是删除Schema部分(xs:schema)中的命名空间?

XML示例:

    <?xml version="1.0" encoding="utf-16"?>
<DataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="timestamp" type="xs:base64Binary" minOccurs="0" />
                <xs:element name="Name" type="xs:string" minOccurs="0" />
                <xs:element name="No_" type="xs:string" minOccurs="0" />
                <xs:element name="Account_x0020_Type" type="xs:string" minOccurs="0" />
                <xs:element name="Indentation" type="xs:int" minOccurs="0" />
                <xs:element name="Company" type="xs:int" minOccurs="0" />
                <xs:element name="ParentAccount" type="xs:string" minOccurs="0" />
                <xs:element name="nodesc" type="xs:string" minOccurs="0" />
                <xs:element name="AccountID" type="xs:int" minOccurs="0" />
                <xs:element name="Code" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <NewDataSet>
      <Table diffgr:id="Table1" msdata:rowOrder="0">
        <No_>11</No_>
        <Company>1</Company>
        <AccountID>2224</AccountID>
      </Table>
      </NewDataSet>
  </diffgr:diffgram>
</DataSet>

期望的输出:

    <?xml version="1.0" encoding="utf-16"?>
<DataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="timestamp" type="xs:base64Binary" minOccurs="0" />
                <xs:element name="Name" type="xs:string" minOccurs="0" />
                <xs:element name="No_" type="xs:string" minOccurs="0" />
                <xs:element name="Account_x0020_Type" type="xs:string" minOccurs="0" />
                <xs:element name="Indentation" type="xs:int" minOccurs="0" />
                <xs:element name="Company" type="xs:int" minOccurs="0" />
                <xs:element name="ParentAccount" type="xs:string" minOccurs="0" />
                <xs:element name="nodesc" type="xs:string" minOccurs="0" />
                <xs:element name="AccountID" type="xs:int" minOccurs="0" />
                <xs:element name="Code" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <diffgr>
    <NewDataSet>
      <Table id="Table1">
        <No_>11</No_>
        <Company>1</Company>
        <AccountID>2224</AccountID>
      </Table>
      </NewDataSet>
  </diffgr>
</DataSet>

1 个答案:

答案 0 :(得分:2)

你快到了 - 试试:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- "copy" an element while removing any namespace -->
<xsl:template match="*">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
</xsl:template>

<!-- "copy" an attribute while removing any namespace -->
<xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
        <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

<!-- copy the entire schema part as is -->
<xsl:template match="xs:schema">
    <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>

<强>加了:

  

是否有一种简单的方法可以将根节点名称从<DataSet>更改为   <NewDataSet>

不确定。只需添加另一个模板:

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