Replace an XML element value using XSLT

The aim is to take a document, look for a specific element (by local name and URI rather than name including prefix) and replace its value.

Where is this useful? Imagine a gateway protecting an internal web service. If that web service is WS-A enabled, it therefores has a wsa:From field which contains its internal URI. Obviously the gateway abstracts the internal service from what is seen outside as a ‘virtual’ or ‘logical’ service. One therefore needs to update the wsa:From field to reflect an external URI.

Sample XML:

<?xml version=”1.0″ encoding=”UTF-8″?>
<soap12:Envelope xmlns:soap12=”” xmlns:xsd=”” xmlns:xsi=”” xmlns:wsa05=”” xmlns:uddi=”urn:uddi-org:api_v2″ xmlns:ns1=”” xsi:schemaLocation=””>
<ns1:greet soap12:encodingStyle=””>
<in0 xsi:type=”xsd:string”>Philip</in0>

The XSL to be applied here is

<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”2.0″ xmlns:xsl=”” xmlns:xs=”” xmlns:fn=”” xmlns:soapenv=”” xmlns:wsa=””>
<xsl:output method=”xml” version=”1.0″ encoding=”UTF-8″ indent=”yes”/>
<xsl:variable name=”fromVirtualUri”>http://virtualFromValue</xsl:variable>
<xsl:template match=”node()|@*”>
<xsl:when test=”local-name()=’Address'”>
<xsl:if test=”namespace-uri()=’'”>
<xsl:value-of disable-output-escaping=”yes” select=”$fromVirtualUri”></xsl:value-of>
<xsl:apply-templates select=”@*|*|processing-instruction()|comment()|text()”>
<xsl:with-param name=”virtualUri”></xsl:with-param>