xml – 通过动态名称引用XSLT变量

我遇到了一个小问题.

XSL文件:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0"  
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions"> 
<xsl:template match="/"> 


<xsl:variable name="unumericValue" select="10" />
<xsl:variable name="uanotherValue" select="8" />



<xsl:for-each select="/root/try">
<xsl:value-of select="var" />
<xsl:variable name="min"><xsl:value-of select="@minimum" /></xsl:variable>
<xsl:value-of select="@type" />
<xsl:variable name="referenceName"><xsl:value-of select='concat("u",var)' /></xsl:variable>
<xsl:value-of select="$referenceName" />
<xsl:if test='$referenceName > $min'>
<p>Do something.</p>
</xsl:if>
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>

XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="q1.xsl"?>
<root>
<try type="compare" minimum="9">
<var>numericValue</var>
<something>...</something>
</try>

<try type="compare" minimum="10">
<var>anotherValue</var>
<something>...</something>
</try>
</root>

如您所见,XML文件有两个var-Elements,它们应与XSLT-File中的变量匹配.但是我不知道哪个语法是正确的.
$referenceName只是我想要使用的变量的名称.但我不知道如何将名称引用到现有变量.

$referenceName不是对名称为“unumericValue”或其他名称的变量的引用.它只是字符串值“unumericValue”等等.所以永远不会超过$min.但是,通过一些额外的工作,有一个技巧可以通过名称查找变量:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:variable name="numericValue" select="10" />
  <xsl:variable name="anotherValue" select="8" />
  <xsl:variable name="vars" select="document('')/*/xsl:variable" />

  <xsl:template match="/">
    <xsl:variable name="referenceName" select="'numericValue'" />
    <xsl:variable name="referenceValue" select="$vars[@name = $referenceName]/@select" />
    Reference value: <xsl:value-of select="$referenceValue" />
  </xsl:template>
</xsl:stylesheet>

这里要注意的一个很大的限制是,这只适用于恒定数值的变量.

这是一种使用常量字符串值模拟变量的方法:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 xmlns:v="variables-node"
>
  <v:variables>
    <v:variable n="numericValue" value="10" />
    <v:variable n="nonNumericValue" value="Hello World" />
  </v:variables>
  <xsl:variable name="vars" select="document('')//v:variables/v:variable" />

    <xsl:template match="/">
      <xsl:variable name="referenceName" select="'nonNumericValue'" />
      <xsl:variable name="referenceValue" select="$vars[@n = $referenceName]/@value" />
      <xsl:value-of select="concat('The variable with the name ',$referenceName,' has the value ',$referenceValue)"/>
    </xsl:template>
</xsl:stylesheet>

最后,一种用计算值模拟变量的方法:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:exslt="http://exslt.org/common"
>

  <xsl:variable name="varsRaw">
    <var n="computedValue" value="{concat('2 + 4 is ',2 + 4)}" />
    <var n="computedNumber" value="{22 div 7}" />
  </xsl:variable>
  <xsl:variable name="vars" select="exslt:node-set($varsRaw)/var" />

    <xsl:template match="/">
      <xsl:variable name="referenceName" select="'computedValue'" />
      <xsl:variable name="referenceValue" select="$vars[@n = $referenceName]/@value" />
      <xsl:value-of select="concat('The variable with the name ',$referenceValue)"/>

      <xsl:value-of select="'     '"/>

      <xsl:variable name="referenceName2" select="'computedNumber'" />
      <xsl:variable name="referenceValue2" select="$vars[@n = $referenceName2]/@value" />
      <xsl:value-of select="concat('The variable with the name ',$referenceName2,$referenceValue2)"/>
    </xsl:template>
</xsl:stylesheet>

最后一种方法实际上可能是最正统的,但需要依赖于XSLT处理器(至少在XSLT 1.0中)的node-set()函数.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇