2017-06-28 103 views
0

我有一個傳入的XMl,我想要替換某個屬性的值,如果它有一個特定的值。 「算法」被「轉換」,這是在XML很多這樣的節點屬性的父元素替換XML中的屬性值

傳入XML:

<ds:SignedInfo> 
     <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> 
     <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/> 
     <ds:Reference URI="#pfx41d8ef22-e612-8c50-9960-1b16f15741b3"> 
     <ds:Transforms> 
      <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/> 
      <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> 
     </ds:Transforms> 
     <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/> 
     <ds:DigestValue>yJN6cXUwQxTmMEsPesBP2NkqYFI=</ds:DigestValue> 
     </ds:Reference> 
    </ds:SignedInfo> 

XSL:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
     <xsl:output omit-xml-declaration="yes" indent="yes"/> 
     <xsl:param name="pNewType" select="'myNewType'"/> 
     <xsl:template match="node()|@*"> 
      <xsl:copy> 
       <xsl:apply-templates select="node()|@*"/> 
      </xsl:copy> 
     </xsl:template> 
     <xsl:template match="Transform/@Algorithm[.='http://www.w3.org/TR/2001/REC-xml-c14n-20010315']"> 
      <xsl:attribute name="Algorithm"> 
       <xsl:value-of select="'http://www.w3.org/2001/10/xml-exc-c14n#'"/> 
      </xsl:attribute> 
     </xsl:template> 
    </xsl:stylesheet> 

能否請你讓我知道什麼是這個XSL的問題。

+0

[在XSLT中使用名稱空間前綴匹配元素]可能的重複(https://stackoverflow.com/questions/5100724/matching-elements-with-namespace-prefix-in-xslt) – Filburt

+0

您能否讓我知道。我們如何解決這個問題。 – user5458829

+0

你*看過*鏈接的帖子和答案嗎? – Filburt

回答

1

您在XSLT中沒有考慮到名稱空間。在您的實際XML,幾乎肯定是一個命名空間聲明(可能是根元素),形式...

xmlns:ds="http://..." 

(如果沒有,那麼你的XML不符合命名空間,並獲不能被XSLT處理)。

這意味着元素Transform屬於您的XML中的該名稱空間,但您的XSLT正在尋找名稱空間中名爲Transform的元素。

你需要做的是命名空間聲明添加到您的XSLT,並使用ds前綴在Transform名稱前面的比賽中

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

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

    <xsl:template match="ds:Transform/@Algorithm[.='http://www.w3.org/TR/2001/REC-xml-c14n-20010315']"> 
     <xsl:attribute name="Algorithm"> 
      <xsl:value-of select="'http://www.w3.org/2001/10/xml-exc-c14n#'"/> 
     </xsl:attribute> 
    </xsl:template> 
</xsl:stylesheet> 

待辦事項,此電流XSLT正在尋找一個屬性其值http://www.w3.org/TR/2001/REC-xml-c14n-20010315實際上沒有顯示在您的示例XML中。