Usability - Productivity - Business - The web - Singapore & Twins

XSLT expression when the default namespace is missing

When dealing with XML XSLT is your SwissArmy Knive of data manipulation. Consider a snippet of XML like this:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                    <List DocTemplateUrl=""...
There are namespaces defined for SOAP, XMLSchema and XMLSchema-Instance, but not for the "payload". When trying to transform this XML using XSLT the usual expression <xsl:template match="GetListCollectionResult"> will not yield any results. I initially was puzzled by that. Relying on geany (highly recommended for light editing), EMACS or Windows Notepad would have left me in the dark. Luckily my oXygen XML Editor revelaled the actual Xpath expression saving me hours of research. There are two ways to target the element properly. One is to add a default namespace into your source XML. This is usually a bad idea, since it requires some manual intervention into data you might retrieve automatically. The other option is to use the local name function in XSLT. So your template would look like this:
<xsl:template match="*[local-name()='GetListCollectionResult']" >.
To get to the "meat" (rather than playing with the wrappers) in the above code snippet you could use this template:
    <xsl:template match="/">
        <!-- Jump the soap wrapper directly to the list -->
        <xsl:apply-templates select="/soap:Envelope/soap:Body/*[local-name()='GetListResponse']/*[local-name()='GetListResult']/*[local-name()='List']" />
As ususal YMMV

Posted by on 25 January 2011 | Comments (0) | categories: Software


  1. No comments yet, be the first to comment