Skip to content
The Chat Archive
kimi-k2-0711-preview LMArena Human Preference 140K

What is $this in Saxon xsl file?

  1. User

    What is $this in Saxon xsl file?

  2. Assistant

    In a Saxon XSLT file, $this is not a built-in or special variable like position() or current(). Instead, it is typically a user-defined variable that is declared and assigned within the stylesheet.

    Common Contexts Where $this Appears:

    1. User-Defined Variable
      You might see something like:

      <xsl:variable name="this" select="."/>
      

      Here, $this is just a variable holding the current context node (.). This is often done for clarity or to avoid issues with changing context in nested expressions.

    2. Higher-Order Functions (Saxon-PE/EE)
      In Saxon’s implementation of XSLT 3.0, $this can appear in inline functions (lambda expressions) as a parameter. For example:

      <xsl:variable name="square" select="function($this) { $this * $this }"/>
      

      Here, $this is the parameter name for the function.

    3. Custom Templates or Functions
      Some stylesheets use $this as a convention (similar to self in 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 $this is 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 $this is used, I can give a more precise explanation!

  3. User

    Is there a way to display the file name that's being matched by xsl:template ?

  4. Assistant

    Yes.
    In Saxon you can obtain the URI of the document node that is being processed with the standard XPath 2.0+ function document-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 to doc()/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 -t or -warnings option (or just rely on the default xsl:message output).

  5. User

    I got error "Unknown system function: document-uri", is there another way?

  6. 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 says version="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):

    1. 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.xml
      

      In 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>
      
    2. Switch the stylesheet to XSLT 2.0/3.0
      If you are using Saxon 9 or later, change the version declaration and you can use document-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.
  7. User

    OK, if not the file name, how can I display the whole body if matched a node?

  8. 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:message is 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.

  9. User

    How to check saxon version?

  10. Assistant

    There are several quick ways to find out which Saxon version you are running.

    1. 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
      
    2. Command line (class name)

      java net.sf.saxon.Version
      

      Output is a single line, e.g.

      Saxon-HE 11.4
      
    3. 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.7 or 11.4.

    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.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.