Editing xml elements with xslt stylesheet

7 10 2009

In the last days I have spent some time using xsl to modify my xml messages in run time. Since I saw xsl the first time with the help of Pierre, a friend of me. I realized the amount of different things that you can do with xsl and how power it can be.

So what I have done this days is editing the values of xml and removing elements. So for that I will give you a tip of how to do so.

The source of the example will be this xml.

<?xml version=”1.0″ encoding=”UTF-8″?>
<soap:Envelope xmlns:soap=”http://www.w3.org/2003/05/soap-envelope”>
<soap:Header>
<wsa:To xmlns:wsa=”http://www.w3.org/2005/08/addressing”>http://localhost:7070/WsSongStore/services/WsSongStore</wsa:To>
<wsa:Action xmlns:wsa=”http://www.w3.org/2005/08/addressing”>http://songstore.org/cap/mgmt/CreateOperation</wsa:Action>
<wsa:MessageID xmlns:wsa=”http://www.w3.org/2005/08/addressing”>uuid:fe33274e-1ca7-daca-ceab-13e4417560c2</wsa:MessageID>
<wsa:From xmlns:wsa=”http://www.w3.org/2005/08/addressing”>
<wsa:Address>http://www.w3.org/2005/08/addressing/role/anonymous</wsa:Address>
</wsa:From>
<vSTS:FederationUUID xmlns:vSTS=”http://www.example.com/example-STS”>aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa</vSTS:FederationUUID>
<vSTS:businessKey xmlns:vSTS=”http://www.example.com/example-STS”>1a555434-ca9e-4c31-b59a-dc3c07f6dad1</vSTS:businessKey>
<user:username xmlns:user=”http://www.example.com/ssc-User”>juanlu</user:username>
</soap:Header>
<soap:Body>
<pfx1:CreateOperation xmlns:pfx1=”http://songstore.org/cap/mgmt”/>
</soap:Body>
</soap:Envelope>

And what we will do first is to change the uri of the element to within the header for a different value. And for that we will apply the xsl stylesheet below that what it does is look through the xml tree for the element we say in the when statement and change its value for the one stored in the variable that we declared at the beginning.

<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”2.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:xs=”http://www.w3.org/2001/XMLSchema” 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”>
<xsl:output method=”xml” version=”1.0″ encoding=”UTF-8″ indent=”yes”/>
<xsl:variable name=”fromVirtualUri”>http://localhost:8080/WsSongStore/services/WsSongStore</xsl:variable>
<xsl:template match=”node()|@*”>
<xsl:copy>
<xsl:choose>
<xsl:when test=”local-name()=’To’”>
<xsl:if test=”namespace-uri()=’http://www.w3.org/2005/08/addressing’”>
<xsl:value-of disable-output-escaping=”yes” select=”$fromVirtualUri”></xsl:value-of>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select=”@*|*|processing-instruction()|comment()|text()”>
<xsl:with-param name=”virtualUri”></xsl:with-param>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

The second transformation removes elements from the xml, may be because we don’t want them anymore in the message because we have used them already and the message that the other component is expecting does not contain them. So the following  xsl loos for the element we  say on the variable that we declare at the beginning and copies everything to the exit but that element, that is skipped due to the when declaration.

<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”2.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:xs=”http://www.w3.org/2001/XMLSchema” 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”>
<xsl:output method=”xml” version=”1.0″ encoding=”UTF-8″ indent=”yes”/>
<xsl:variable name=”tag”>username</xsl:variable>
<xsl:template match=”node()|@*”>
<xsl:choose>
<xsl:when test=”local-name()!=$tag”>
<xsl:copy>
<xsl:apply-templates select=”node()|@*|*|processing-instruction()|comment()|text()”/>
</xsl:copy>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

I hope this helps you.