Given the following stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template name="start">
<base-uris>
<node-base-uri>
<xsl:value-of select="base-uri(.)" />
</node-base-uri>
<node-base-uri-from-copy>
<xsl:variable name="doc">
<xsl:copy-of select="." />
</xsl:variable>
<xsl:value-of select="base-uri($doc)" />
</node-base-uri-from-copy>
</base-uris>
</xsl:template>
</xsl:stylesheet>
After transforming an arbitrary XML file with Saxon from the command line with the following command:
java net.sf.saxon.Transform -s:xml/index.xml -xsl:xsl/base-uri.xsl -it:start
I expected the same values for the base URIs pointing to the XML source file. But the base URI in the second case (with copy-of) points to the XSLT file.
<base-uris>
<node-base-uri>file:/xml/index.xml</node-base-uri>
<node-base-uri-from-copy>file:/xsl/base-uri.xsl</node-base-uri-from-copy>
</base-uris>
Motivation: In the "real world stylesheet" I use a template to include other XML sources. They are specified in the source XML itself (relative paths in a href attribute).
<xsl:template match="include" mode="includes">
<xsl:copy-of select="document(@href, .)/*"/>
</xsl:template>
From the Spec:
The base URI of a node is copied, except in the case of an element node having an xml:base attribute
My question(s):
First of all, I would like to know, how to preserve/set/copy the base URI from the XML file and not from the XSLT file.
Second, I do not understand the Spec and/or do not understand the xml:base attribute thing. I simply thought: I do not see any xml:base attribute in my code, so the base URI of the node should be copied.
Last remark:
Playing around, I came up with something like this, which feels clumsy or simply the wrong way to go:
<node-base-uri-work-around>
<xsl:variable name="doc">
<wrapper>
<xsl:attribute name="xml:base" select="base-uri(.)" />
<xsl:copy-of select="." />
</wrapper>
</xsl:variable>
<xsl:value-of select="base-uri($doc/wrapper)" />
</node-base-uri-work-around>
<xsl:variable name="doc"><xsl:copy-of select="." /></xsl:variable>, why don't you simply use<xsl:variable name="doc" select="."/>?