[xsl] matching elements by name and type to detect invalid xpaths

Subject: [xsl] matching elements by name and type to detect invalid xpaths
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Mon, 6 Oct 2008 18:08:41 +0100
I know I moaned about input validation the other day but I hadn't come
across this:

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:xs="http://www.w3.org/2001/XMLSchema";>

    <xsl:import-schema>
        <xs:schema>
            <xs:complexType name="root">
                <xs:sequence>
                    <xs:element name="child" />
                </xs:sequence>
            </xs:complexType>
        </xs:schema>
    </xsl:import-schema>

    <xsl:template match="element(root, root)">
            <xsl:value-of select="wrong" />
    </xsl:template>

</xsl:stylesheet>

Compile that with a schema-aware processor (Saxon SA in this case) and
you are told:

"The complex type root does not allow a child element named wrong;"

You don't even have to validate any input, because the match pattern
contains the type it knows that "wrong" is not a child of <root>...
fantastic :)

I think this means you can enjoy the benefits of the paths in your
XSLT being checked against the schema _without_ being forced to
validate the input each time... which is a huge benefit.

(sorry if you were well aware of this already)


-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Current Thread