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. Let’s have a look at how we can achieve XML manipulation with XSLT.

Where is this useful?

Imagine a gateway protecting an internal web service. If that web service is WS-A enabled, it therefore has a wsa:From field that 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="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://ws.samples.engage.bt.com" xmlns:uddi="urn:uddi-org:api_v2" xmlns:wsa05="http://www.w3.org/2005/08/addressing" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/ http://schemas.xmlsoap.org/soap/envelope/">
   <soap12:Header>
      <wsa05:To>http://somelocation</wsa05:To>
      <wsa05:Action>urn:greet</wsa05:Action>
      <wsa05:From>
         <wsa05:Address>http://internaluri.intra.mycompany.com</wsa05:Address>
      </wsa05:From>
   </soap12:Header>
   <soap12:Body>
      <ns1:greet soap12:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <in0 xsi:type="xsd:string">Philip</in0>
      </ns1:greet>
   </soap12:Body>
</soap12:Envelope>

The XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
   <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:copy>
         <xsl:choose>
            <xsl:when test="local-name()='Address'">
               <xsl:if test="namespace-uri()='http://www.w3.org/2005/08/addressing'">
                  <xsl:value-of disable-output-escaping="yes" select="$fromVirtualUri" />
               </xsl:if>
            </xsl:when>
            <xsl:otherwise>
               <xsl:apply-templates select="@*|*|processing-instruction()|comment()|text()">
                  <xsl:with-param name="virtualUri" />
               </xsl:apply-templates>
            </xsl:otherwise>
         </xsl:choose>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

Other uses for XML manipulation with XSLT

XSLT and XML can be be used to translate between different message formats. For instance, if you update a service that requires a specific XML structure, you can use XSLT to let clients still use the old format and have a gateway translate from the old to the new format. This is essentially XML manipulation with XSLT.

XSLT could also be used to visualize XML in a better way. Combined with CSS and standards such as SVG, it makes for a compelling XML visualizer.

XML manipulation with XSLT FAQ

What is XML?

Extensible Markup Language is a markup language and file format for storing, transmitting, and reconstructing arbitrary data. It defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. Wikipedia

What is XSLT?

XSLT is a language originally designed for transforming XML documents into other XML documents, or other formats such as HTML for web pages, plain text, or XSL Formatting Objects, which may subsequently be converted to other formats, such as PDF, PostScript, and PNG. Wikipedia

What is XACML?

XACML, the eXtensible Access Control Markup Language, is the de-factor standard (OASIS) for attribute-based (ABAC) and policy-based access control (PBAC). It enables fine-grained authorization use cases.