2013-07-17 35 views
0

我有XML:如何刪除名稱空間,如果它不使用?

<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <med:PutEmployee xmlns:med="https://services"> 
     <med:employees> 
     <med:Employee> 
      <med:Name xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true">Мария</med:Name> 
      <med:SNILS>111-111-111-11</med:SNILS> 
     </med:Employee> 
     </med:employees> 
    </med:PutEmployee> 
</soapenv:Body> 

我刪除了parametr 「@i:nill」 使用XSLT:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
    exclude-result-prefixes="i">    
    <xsl:template match="node() | @*"> 
     <xsl:copy> 
     <xsl:apply-templates select="node() | @*[name()!='i:nil']" /> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

運行XSLT,我得到的XML:

<?xml version="1.0"?> 
<?xml version="1.0"?> 
<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <med:PutEmployee xmlns:med="https://services"> 
     <med:employees> 
     <med:Employee> 
      <med:Name xmlns:i="http://www.w3.org/2001/XMLSchema-instance">Мария</med:Name> 
      <med:SNILS>111-111-111-11</med:SNILS> 
     </med:Employee> 
     </med:employees> 
    </med:PutEmploy> 

left xmlns:i="http://www.w3.org/2001/XMLSchema-instance"

如何將其刪除?

我試圖添加exclude-result-prefixes = "i",它並沒有幫助

回答

2

這應該做的伎倆:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
       exclude-result-prefixes="i"> 
    <xsl:output omit-xml-declaration="yes"/> 

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

    <xsl:template match="@i:nil" /> 

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

當你的樣品輸入運行,其結果是:

<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <med:PutEmployee xmlns:med="https://services"> 
    <med:employees> 
     <med:Employee> 
     <med:Name>Мария</med:Name> 
     <med:SNILS>111-111-111-11</med:SNILS> 
     </med:Employee> 
    </med:employees> 
    </med:PutEmployee> 
</soapenv:Body> 
3

如果您使用的是XSLT 2.0,請使用

<xsl:copy copy-namespaces="no"> 
相關問題