Search code examples
xmlxsltwsdlxslt-1.0xslt-2.0

How does match works in xsl template?


I have an xsl like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                version="1.0">

    <xsl:template match="wsdl:definitions">
        <!-- code here -->
    </xsl:template>

</xsl:stylesheet>

in this xsl , what does this line do:

<xsl:template match="wsdl:definitions">

from my understanding its going to strictly match wsdl:definitions tags which is like this <wsdl:definitions> in the given xml (correct me if I'm wrong here).

But I was literally confused the moment, I give this input xml:

<xxx:definitions xmlns:xxx="http://schemas.xmlsoap.org/wsdl/" xmlns:xxxx1="http://www.w3.org/2006/05/addressing/wsdl"  >
</xxx:definitions>

it still matches <xxx:definitions> and does the transformation as needed. How this can be possible given that in my xsl sheet I have given it to match wsdl:definitions? Is match something to do with xmlns of the given input xml?


Solution

  • The namespace prefix is just a name for the URI. If the URIs match the namespaces are identical. The xmlns adds a prefix to the URI. There can even be many prefixes for the same namespace in a single document.

    So you are allowed to use whatever prefixes you want for the namespaces and the XSL will always match them properly. This will help with documents originating from different sources not needing to use the same prefixes and still be understood.