What is $this in Saxon xsl file?
- User
What is $this in Saxon xsl file?
- Assistant
In a Saxon XSLT file,
$thisis not a built-in or special variable likeposition()orcurrent(). Instead, it is typically a user-defined variable that is declared and assigned within the stylesheet.Common Contexts Where
$thisAppears:-
User-Defined Variable
You might see something like:<xsl:variable name="this" select="."/>Here,
$thisis just a variable holding the current context node (.). This is often done for clarity or to avoid issues with changing context in nested expressions. -
Higher-Order Functions (Saxon-PE/EE)
In Saxon’s implementation of XSLT 3.0,$thiscan appear in inline functions (lambda expressions) as a parameter. For example:<xsl:variable name="square" select="function($this) { $this * $this }"/>Here,
$thisis the parameter name for the function. -
Custom Templates or Functions
Some stylesheets use$thisas a convention (similar toselfin object-oriented programming) to refer to the current node or object being processed.
Key Takeaway:
- Not a Saxon-specific keyword—just a variable name.
- Check the stylesheet to see where
$thisis declared (<xsl:variable name="this" ...>or as a function parameter). - No special behavior unless explicitly defined by the stylesheet author.
If you share a snippet of the XSLT where
$thisis used, I can give a more precise explanation! -
- User
Is there a way to display the file name that's being matched by xsl:template ?
- Assistant
Yes.
In Saxon you can obtain the URI of the document node that is being processed with the standard XPath 2.0+ functiondocument-uri(.).
Inside a template that is matching something in that document you can therefore output (or store) the file name like this:<xsl:template match="/"> <!-- full URI --> <xsl:message>Processing: <xsl:value-of select="document-uri(.)"/></xsl:message> <!-- just the last path segment (the file name) --> <xsl:message>File name: <xsl:value-of select="tokenize(document-uri(.), '/')[last()]"/></xsl:message> </xsl:template>document-uri(.)returns the system identifier that Saxon assigned to the document (usually the file name or the URI supplied todoc()/document()).tokenize(..., '/')[last()]strips everything except the final path component, giving you the simple file name.
If you are running from the command line and want to see the messages, use the
-tor-warningsoption (or just rely on the defaultxsl:messageoutput). - User
I got error "Unknown system function: document-uri", is there another way?
- Assistant
document-uri()is an XPath 2.0/3.0 function.
If Saxon is telling you it is unknown, your stylesheet is almost certainly being executed in XSLT 1.0 mode (either because the root element saysversion="1.0"or because you are using an old Saxon release that only supports XSLT 1.0).In pure XSLT 1.0 there is no standard function that returns the URI of the source document, so you have to pass the information in yourself.
Typical ways to do it with Saxon (any version):
-
Use a stylesheet parameter
When you run Saxon from the command line (or from Ant, Java, etc.) supply the file name as a parameter:Command line:
java -jar saxon.jar -s:input.xml -xsl:transform.xsl -o:out.xml fileName=input.xmlIn the stylesheet (works in 1.0 or 2.0):
<xsl:param name="fileName" select="'unknown.xml'"/> <xsl:template match="/"> <xsl:message>Processing: <xsl:value-of select="$fileName"/></xsl:message> </xsl:template> -
Switch the stylesheet to XSLT 2.0/3.0
If you are using Saxon 9 or later, change the version declaration and you can usedocument-uri():<xsl:stylesheet version="2.0" ...> ... <xsl:template match="/"> <xsl:message> <xsl:value-of select="tokenize(document-uri(/), '/')[last()]"/> </xsl:message> </xsl:template> </xsl:stylesheet>Then run Saxon in the normal way; it will automatically use the 2.0/3.0 processor.
So:
- If you must stay in XSLT 1.0, pass the file name in via a parameter.
- If you can move to XSLT 2.0/3.0,
document-uri()(or the parameter approach) will both work.
-
- User
OK, if not the file name, how can I display the whole body if matched a node?
- Assistant
In XSLT 1.0 you can’t use
document-uri(), but you can still output the entire body (sub-tree) of the node that your template matched.
The simplest way is to copy it verbatim with<xsl:copy-of>:<xsl:template match="body"> <!-- show the whole <body>…</body> element --> <xsl:copy-of select="."/> </xsl:template>If you want it as a plain-text dump (mark-up included) inside an
<xsl:message>or a text node, use<xsl:copy-of>wrapped in<xsl:message>:<xsl:template match="body"> <xsl:message> MATCHED BODY: <xsl:copy-of select="."/> </xsl:message> </xsl:template>(When the result of
xsl:messageis serialized by Saxon, the element and its descendants are written out in lexical form, so you’ll see the full markup.)That’s all that’s required—no extension functions or parameters needed.
- User
How to check saxon version?
- Assistant
There are several quick ways to find out which Saxon version you are running.
-
Command line (stand-alone JAR)
java -jar saxon9he.jar -? # or saxon.jar, saxon-pe.jar, etc.The banner printed to stderr starts with something like
Saxon-HE 9.9.1.7J from Saxonica -
Command line (class name)
java net.sf.saxon.VersionOutput is a single line, e.g.
Saxon-HE 11.4 -
From within an XSLT stylesheet (works in both 1.0 and 2.0/3.0)
<xsl:value-of select="system-property('xsl:product-version')"/>When executed with Saxon this returns the exact release string, e.g.
9.9.1.7or11.4. -
From Java code
System.out.println( net.sf.saxon.Version.getProductVersion());
Any of these will tell you the precise Saxon edition (HE/PE/EE) and version number.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.