How can I retrieve the XML source file name using XSL 1.0 code?
There is no way to obtain the name or path of the file being transformed by XSL. Due to the nature of XSL, the data being transformed might not be from a file, it might just be transforming a stream of data.
I know this is old, but other people may still come upon this in looking for an answer.
The only way I know of to do this in XSLT 1.0 is to use a script function inside the XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="http://tempuri.org/msxsl">
<xsl:output method="xml" version="1.0"/>
<msxsl:script language="JScript" implements-prefix="user">
<![CDATA[
function getFilename(context){
return context.nextNode.url;
}
]]>
</msxsl:script>
<xsl:template match="/">
<INPUT>
<filename><xsl:value-of select="user:getFilename(/)"/></filename>
</INPUT>
</xsl:template>
</xsl:stylesheet>
This will generate an XML output such as:
<?xml version="1.0" encoding="UTF-16"?>
<INPUT xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="http://tempuri.org/msxsl">
<filename>file:///C:/XLST/My%20Test%20XML%20File.txt.xml</filename>
</INPUT>
In this case, I used a file named C:/XLST/My Test XML File.txt
.
The vb.net
code used for importing text files first converts that file into XML and appends the .xml
extension to the source filename, and is then processed by the Transform.
This is why my filename ends in .txt.xml
Normal XLST string functions can then be used as needed to replace the %20
with ' '
and perhaps only return the filename part and not the entire path and filename.
With Xalan there is the following, non-official function :
<xsl:value-of select="document-location()"/>
I use it on a transformation of a local file and it gives me the absolute path of the XML file being transformed. I found that function browsing the Xalan code, it is part of the class FuncDoclocation
.
In XSLT 2.0, there are two relevant functions: base-uri() and document-uri().
In XSLT 1.0, you have to pass the URL or filename as a parameter to the stylesheet, unless the processor offers extension functions for the purpose.