Home > Software engineering >  XSLT evaluate variable whose name is coming from xpath evaluation
XSLT evaluate variable whose name is coming from xpath evaluation

Time:07-21

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

CodePudding user response:

You could use the same technique and drill into the imported sector.xslt as an XML doc, selecting the value with XPath:

<xsl:when test="$sector">
  <xsl:variable name="target" select="document('')/*/s:sector[@source=$sector]/@target"/>
  <!-- <xsl:value-of select="$target" /> -->
  <xsl:value-of select="document('sector.xslt')/xsl:stylesheet/xsl:variable[contains($target, @name)]"/>
</xsl:when>

It would be a little more straightforward if you changed the s:sector/@target values not to have $ for variable name references, and just had the value to match the sector.xslt xsl:variable/@name.

  • Related