Following my previous post : XSLT take first element that can be mapped among list of predefined mappings
Is there a way to evaluate a variable, in a xsl:value-of, whose name is coming from the evaluation of XPATH ?
The original XML :
<sectors type="array">
<sector>industry</sector>
<sector>e-commerce</sector>
<sector>logistique</sector>
<sectors>
This is my XSLT (thanks to https://stackoverflow.com/a/73040079/10767428) :
<xsl:import href="./sector.xslt"/>
<s:sector source="metallurgy" target="$sector_industry_materials" />
<s:sector source="accounting" target="$sector_audit_accounting" />
<s:sector source="logistics" target="$sector_service_logistics" />
<s:sector source="mass-distribution" target="$sector_distribution_mass_retail" />
<xsl:template match="sectors">
<sectorIdentifier>
<xsl:variable name="sector" select="sector[. = document('')/*/s:sector/@source][1]"/>
<xsl:choose>
<xsl:when test="$sector">
<xsl:variable name="target" select="document('')/*/s:sector[@source=$sector]/@target"/>
<xsl:value-of select="$target" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$sector_other"/>
</xsl:otherwise>
</xsl:choose>
</sectorIdentifier>
</xsl:template>
Here, the value of $target are either the strings : $sector_industry_materials, $sector_audit_accounting, $sector_service_logistics, $sector_distribution_mass_retail or $sector_other.
I want those strings to be evaluated against the following other XSLT, from another file, and imported in current file :
sector.xslt :
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="sector_industry_materials" >401</xsl:variable>
<xsl:variable name="sector_audit_accounting" >374</xsl:variable>
<xsl:variable name="sector_service_logistics" >422</xsl:variable>
<xsl:variable name="sector_distribution_mass_retail" >387</xsl:variable>
<xsl:variable name="sector_other" >456</xsl:variable>
</xsl:stylesheet>
So the final output, in case the $target value was the string $sector_industry_materials, must be :
<sectorIdentifier>401</sectorIdentifier>
But for now, all I get is :
<sectorIdentifier>$sector_distribution_mass_retail</sectorIdentifier>
The thing is that with $sector_other, which is hard-coded, everything works fine, and I get :
<sectorIdentifier>456</sectorIdentifier>
SO the problem must be that name of the variables are dynamic.
Thank you
xsl:evaluateonly in XSLT 3.0 (Saxon 10 HE and later, Saxon 9.8 PE/EE and later, Saxon-JS 2) or as processor specific, proprietary extension in earlier versions. Which XSLT 1.0 processor exactly do you use?industrydirectly to401,accountingto374and so on? This map could be hard-coded into your stylesheet or - if you prefer - it could be stored in an external XML document. Such document does not need to be an XSLT stylesheet; in fact, making it so only complicates things.metallurgyand401in the XML file itself, instead of going through the process of finding firstsector_industry_materials. Thank you for your time.key('sector', $mapA/key)[1]and then lookup the corresponding value in map B usingkey('mapB', $foundValue). At no point should there be a need to evaluate a string as an XPath expression.