2013-05-29 152 views
1

我知道如何在所有刪除的命名空間,但我需要做的僅僅是刪除特定的命名空間前綴,例如改變這個文件(除去xenc前綴):如何刪除命名空間前綴離開命名空間值(XSLT)?

<?xml version="1.0" encoding="UTF-8"?> 
<xenc:EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"> 
<xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc"/> 
<ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> 
    <xenc:EncryptedKey xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"> 
     <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5"/> 
    </xenc:EncryptedKey> 
    <ds:X509Data> 
     <ds:X509Certificate>AAA=</ds:X509Certificate> 
    </ds:X509Data> 
</ds:KeyInfo> 

成這樣:

<?xml version="1.0" encoding="UTF-8"?> 
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#"> 
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc"/> 
<ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> 
    <EncryptedKey xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"> 
     <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5"/> 
    </EncryptedKey> 
    <ds:X509Data> 
     <ds:X509Certificate>AAA=</ds:X509Certificate> 
    </ds:X509Data> 
</ds:KeyInfo> 

你能幫我如何可以使用XSLT來完成?

回答

2

與nwellnhof幾乎相同的解決方案。但在樣式表中使用默認的namesepace。 地址:xmlns="http://www.w3.org/2001/04/xmlenc#"

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" 
       xmlns="http://www.w3.org/2001/04/xmlenc#" > 
    <xsl:output indent="yes"/> 

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

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


</xsl:stylesheet> 
1

請嘗試以下樣式表。它包含標識轉換和一個模板以剝離xenc:*元素的名稱空間。請注意,不處理xenc:*屬性。

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"> 

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

<xsl:template match="xenc:*"> 
    <xsl:element name="{local-name()}" namespace="http://www.w3.org/2001/04/xmlenc#"> 
     <xsl:apply-templates select="node() | @*"/> 
    </xsl:element> 
</xsl:template> 

</xsl:stylesheet>