user5458829
user5458829

Reputation: 163

replacing attribute value in XML

I have an incoming XMl where i want to replace value of an attribute if it has one particular value. The parent element of the attribute "Algorithm" is "Transform" which is many such nodes in XML

Incoming 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>

Can you please let me know what is the issue in this XSL.

Upvotes: 0

Views: 159

Answers (1)

Tim C
Tim C

Reputation: 70618

You have not accounted for namespaces in your XSLT. In your actual XML, there is almost certainly a namespace declaration (probably on the root element), of the form...

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

(If there isn't, then your XML is not namespace compliant, and won't be able to be processed by XSLT).

This means the element Transform belongs to that namespace in your XML, but your XSLT is looking for an element called Transform in no namespace.

What you need to do is add the namespace declaration to your XSLT, and use the ds prefix in front of the Transform name in the match

<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>

Do note, this current XSLT is looking for an attribute with the value http://www.w3.org/TR/2001/REC-xml-c14n-20010315 which is not actually shown in your sample XML.

Upvotes: 1

Related Questions