以下是我上一篇文章:XSLT take first element that can be mapped among list of predefined mappings
是否有一种方法来计算xsl:value-of中的变量,该变量的名称来自于XPATH的计算?
原始XML:
<sectors type="array">
<sector>industry</sector>
<sector>e-commerce</sector>
<sector>logistique</sector>
<sectors>这是我的XSLT (感谢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>在这里,$target的值要么是字符串:$sector_industry_materials、$sector_audit_accounting、$sector_service_logistics、$sector_distribution_mass_retail或$sector_other。
我希望根据以下其他XSLT从另一个文件计算这些字符串,并在当前文件中导入这些字符串:
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>因此,如果$target值是字符串$sector_industry_materials,则最后的输出必须是:
<sectorIdentifier>401</sectorIdentifier>但现在,我得到的只是:
<sectorIdentifier>$sector_distribution_mass_retail</sectorIdentifier>问题是,对于$sector_other,它是硬编码的,一切都很好,我得到:
<sectorIdentifier>456</sectorIdentifier>所以问题是变量的名字是动态的。
谢谢
发布于 2022-07-20 14:34:50
您可以使用与XML相同的技术并钻取导入的sector.xslt,使用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>如果您将s:sector/@target值更改为不具有变量名称引用的$,并且只具有与sector.xslt xsl:variable/@name匹配的值,则会更简单一些。
https://stackoverflow.com/questions/73052447
复制相似问题