TEIC / atop

Another TEI ODD Processor
Other
9 stars 2 forks source link

naming convention prevents XPath parameters #33

Closed sydb closed 1 week ago

sydb commented 2 weeks ago

Using Saxon on the command line a parameter is passed in as a string by specifying paraName=paramValue (where the value may need to be quoted, especially if it has spaces, depending on your shell’s rules). A parameter is passed in as an expression by specifying ?paraName=paramExpression (same caveat on quoting). That is,

A param preceded by a leading question mark (?) is interpreted as an XPath expression. For example, ?time=current-dateTime() sets the value of the stylesheet parameter $time to the value of the current date and time, as an instance of xs:dateTime, while ?debug=false() sets the value of the parameter $debug to the boolean value false.

But (I just learned)

… The static context for this XPath expression includes only the standard namespaces conventionally bound to the prefixes xs, fn, xsi, and saxon.

Since the atop prefix is not bound, I think this means that the value of atop:pDebug can never be set. (At least, not with Saxon from the command line. It can be set when running Saxon HE 12.3 from oXygen 26.1. I wonder how they do that.)

If you want to prove this to yourself, try

java -jar /path/to/saxon-he-12.5.jar -xsl:/path/to/file/below.xslt -it ?atop:pBug="true()"

Here is the file below.xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
  xmlns:atop="http://www.tei-c.org/ns/atop"
  exclude-result-prefixes="#all"
  version="3.0">

  <xd:doc scope="stylesheet">
    <xd:desc>A test. Input is ignored.</xd:desc>
  </xd:doc>

  <xsl:output method="text" item-separator="&#x0A;"/>

  <xd:doc>
    <xd:desc>If set to true(), say “this is good”; if set to false(), say “this is BAD”.</xd:desc>
  </xd:doc>
  <xsl:param name="atop:pBug" select="true()" as="xs:boolean"/>

  <xd:doc>
    <xd:desc>whichever output messasge is selected by atop:pBug, say it this many times</xd:desc>
  </xd:doc>
  <xsl:param name="atop:pRepeat" select="3" as="xs:integer"/>

  <xd:doc>
    <xd:desc>Initial template: match document root, output desired message as text.</xd:desc>
  </xd:doc>
  <xsl:template match="/" name="xsl:initial-template" as="item()*">
    <xsl:if test="not( $atop:pRepeat castable as xs:positiveInteger )">
      <xsl:message terminate="yes" select="'You fool!'"/>
    </xsl:if>
    <xsl:text>&#x0A;</xsl:text>
    <xsl:iterate select="1 to $atop:pRepeat">
      <xsl:sequence select="if ($atop:pBug) then 'this is good' else 'this is BAD'"/>
    </xsl:iterate>
    <xsl:text>&#x0A;</xsl:text>
  </xsl:template>

</xsl:stylesheet>
dmj commented 1 week ago

You can use the clark notation and provide the namespace name instead of the prefix:

java -jar /path/to/saxon-he-12.5.jar -xsl:/path/to/file/below.xslt -it ?{http://www.tei-c.org/ns/atop}pBug="true()"
sydb commented 1 week ago

Excellent! Thank you, @dmj! Makes for ugly, clunky, parameter, but at least it works.