首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT计算变量,其名称来自xpath计算。

XSLT计算变量,其名称来自xpath计算。
EN

Stack Overflow用户
提问于 2022-07-20 13:23:04
回答 1查看 45关注 0票数 1

以下是我上一篇文章:XSLT take first element that can be mapped among list of predefined mappings

是否有一种方法来计算xsl:value-of中的变量,该变量的名称来自于XPATH的计算?

原始XML:

代码语言:javascript
复制
<sectors type="array">
   <sector>industry</sector>
   <sector>e-commerce</sector>
   <sector>logistique</sector>
<sectors>

这是我的XSLT (感谢https://stackoverflow.com/a/73040079/10767428):

代码语言:javascript
复制
<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

代码语言:javascript
复制
<?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,则最后的输出必须是:

代码语言:javascript
复制
<sectorIdentifier>401</sectorIdentifier>

但现在,我得到的只是:

代码语言:javascript
复制
<sectorIdentifier>$sector_distribution_mass_retail</sectorIdentifier>

问题是,对于$sector_other,它是硬编码的,一切都很好,我得到:

代码语言:javascript
复制
<sectorIdentifier>456</sectorIdentifier>

所以问题是变量的名字是动态的。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-20 14:34:50

您可以使用与XML相同的技术并钻取导入的sector.xslt,使用XPath选择值:

代码语言:javascript
复制
<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匹配的值,则会更简单一些。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73052447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档