Home > Enterprise >  How can I define a default namespace for my XSLT XPaths? (XSLT 2.0)
How can I define a default namespace for my XSLT XPaths? (XSLT 2.0)

Time:03-20

I have XML that I'd like to transform with XSL to view in a browser.

I have made the default namespace in my XML http://www.tei-c.org/ns/1.0. To avoid using prefixes in the XML and XSL, I added xpath-default-namespace="http://www.tei-c.org/ns/1.0" to my xsl:stylesheet element. I still seem to need prefixes in my xpaths, however, since xpaths with no prefixes do not work.

What am I missing? Shouldn't the xpath-default-namespace attribute ensure that I can use the default namespace in my xpaths, thus avoiding the need to prefix the (default) namespace?

I am hosting the XML and XSL on my own web server (Debian Bullseye, Apache, Raspberry Pi). Could there be an issue with the packages I have installed? For example, I have libxslt1.1 installed, which only "supports" XSLT 1.0 (which doesn't include the xpath-default-namespace attribute). But shouldn't the XML be transformed by the browser, and not by my server?

The XML

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="mezzo.xsl" version="2.0"?>

<TEI xmlns="http://www.tei-c.org/ns/1.0">
        <teiHeader>
                <fileDesc>
                        <titleStmt>Canto 1 of Inferno</titleStmt>
                </fileDesc>
        </teiHeader>
        <text>
        ...
        </text>
</TEI>

The XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://www.tei-c.org/ns/1.0">

<xsl:template match="/">
        <html>
                <body>
                        <h1>mezzo</h1>
                        <p>
                                <strong>Title Statement: </strong>
                        <xsl:value-of select="//titleStmt"/>
                        </p>
                </body>
        </html>
</xsl:template>

</xsl:stylesheet>

CodePudding user response:

XSLT defines a "forwards compatibility mode", which means that if you submit a stylesheet specifying version="2.0" to an XSLT processor that only supports 1.0, the processor ignores elements and attributes that it doesn't understand, in this case xpath-default-namespace.

(Whether this was a good design decision remains an open question. In this particular case the new attribute completely changes the meaning of the code, so an error message would have been better. But the 1.0 processor doesn't know that, of course.)

  • Related