Re: [xsl] Indirect referencing in XSLT/XPath?

Subject: Re: [xsl] Indirect referencing in XSLT/XPath?
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 5 Apr 2025 16:24:05 -0000
On 05/04/2025 16:35, Roger L Costello costello@xxxxxxxxx wrote:
Is there any way to do indirect referencing in XSLT/XPath?


I think it would boil to having a parser and an evaluator that perhaps
uses xsl:evaluate the number of times you findB  a $ in front of what
would be an variable/parameter name, with the handicap that I think you
need to make sure you have all global parameters passed to xsl:evaluate.

So a hack would be e.g.


<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; B version="3.0" B xmlns:xs="http://www.w3.org/2001/XMLSchema"; B xmlns:mf="http://example.com/mf"; B exclude-result-prefixes="#all" B expand-text="yes">

B <xsl:output method="text"/>

B  <xsl:function name="mf:evaluate" as="item()*">
B B B  <xsl:param name="expression" as="xs:string"/>
B B B  <xsl:param name="context-item" as="item()?"/>
B B B  <xsl:param name="params" as="map(xs:QName, item()*)"/>
B B B  <xsl:evaluate xpath="$expression" context-item="$context-item"
with-params="$params"/>
B  </xsl:function>

B <xsl:param name="UNITS" as="xs:string">METERS</xsl:param>

B <xsl:param name="ALTITUDE" as="xs:string">UNITS</xsl:param>

B <xsl:param name="AIRCRAFT" as="xs:string">ALTITUDE</xsl:param>

B  <xsl:template match="/" name="xsl:initial-template">
B B B  <xsl:value-of select="analyze-string('$$$AIRCRAFT',
'(\$+)(\p{L}[\p{L}0-9]*)')!*:match!fold-left((1 to
string-length(*:group[@nr = 1])), *:group[@nr = 2], function($name, $n)
{ mf:evaluate('$' || $name, (), map { QName('', 'AIRCRAFT') : $AIRCRAFT,
QName('', 'ALTITUDE') : $ALTITUDE, QName('', 'UNITS'): $UNITS }) })"/>
B  </xsl:template>

</xsl:stylesheet>


Perhaps these days with the availability of Invisible XML it would be better to write a grammar for the $$$indentifier syntax and that way parse any $$$identifier expression into an XML tree which could then probably process cleaner than above hack with analyze-string does.


And of course it ain't pretty like your built-in SNOBOL stuff but I guess in a declarative language like XSLT/XPath where variables/parameters are bound to expressions and are not macros you need some parsing and evaluation implemented to deal with your desired interpretation (if I understood that right) of $$$identifier.

Current Thread