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

前端之家收集整理的这篇文章主要介绍了xml – 通过动态名称引用XSLT变量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我遇到了一个小问题.

XSL文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0"
  3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  4. xmlns:fn="http://www.w3.org/2005/xpath-functions">
  5. <xsl:template match="/">
  6.  
  7.  
  8. <xsl:variable name="unumericValue" select="10" />
  9. <xsl:variable name="uanotherValue" select="8" />
  10.  
  11.  
  12.  
  13. <xsl:for-each select="/root/try">
  14. <xsl:value-of select="var" />
  15. <xsl:variable name="min"><xsl:value-of select="@minimum" /></xsl:variable>
  16. <xsl:value-of select="@type" />
  17. <xsl:variable name="referenceName"><xsl:value-of select='concat("u",var)' /></xsl:variable>
  18. <xsl:value-of select="$referenceName" />
  19. <xsl:if test='$referenceName > $min'>
  20. <p>Do something.</p>
  21. </xsl:if>
  22. </xsl:for-each>
  23.  
  24. </xsl:template>
  25. </xsl:stylesheet>

XML文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?xml-stylesheet type="text/xsl" href="q1.xsl"?>
  3. <root>
  4. <try type="compare" minimum="9">
  5. <var>numericValue</var>
  6. <something>...</something>
  7. </try>
  8.  
  9. <try type="compare" minimum="10">
  10. <var>anotherValue</var>
  11. <something>...</something>
  12. </try>
  13. </root>

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

$referenceName不是对名称为“unumericValue”或其他名称的变量的引用.它只是字符串值“unumericValue”等等.所以永远不会超过$min.但是,通过一些额外的工作,有一个技巧可以通过名称查找变量:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3.  
  4. <xsl:variable name="numericValue" select="10" />
  5. <xsl:variable name="anotherValue" select="8" />
  6. <xsl:variable name="vars" select="document('')/*/xsl:variable" />
  7.  
  8. <xsl:template match="/">
  9. <xsl:variable name="referenceName" select="'numericValue'" />
  10. <xsl:variable name="referenceValue" select="$vars[@name = $referenceName]/@select" />
  11. Reference value: <xsl:value-of select="$referenceValue" />
  12. </xsl:template>
  13. </xsl:stylesheet>

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

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:v="variables-node"
  4. >
  5. <v:variables>
  6. <v:variable n="numericValue" value="10" />
  7. <v:variable n="nonNumericValue" value="Hello World" />
  8. </v:variables>
  9. <xsl:variable name="vars" select="document('')//v:variables/v:variable" />
  10.  
  11. <xsl:template match="/">
  12. <xsl:variable name="referenceName" select="'nonNumericValue'" />
  13. <xsl:variable name="referenceValue" select="$vars[@n = $referenceName]/@value" />
  14. <xsl:value-of select="concat('The variable with the name ',$referenceName,' has the value ',$referenceValue)"/>
  15. </xsl:template>
  16. </xsl:stylesheet>

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <xsl:stylesheet version="1.0"
  3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  4. xmlns:exslt="http://exslt.org/common"
  5. >
  6.  
  7. <xsl:variable name="varsRaw">
  8. <var n="computedValue" value="{concat('2 + 4 is ',2 + 4)}" />
  9. <var n="computedNumber" value="{22 div 7}" />
  10. </xsl:variable>
  11. <xsl:variable name="vars" select="exslt:node-set($varsRaw)/var" />
  12.  
  13. <xsl:template match="/">
  14. <xsl:variable name="referenceName" select="'computedValue'" />
  15. <xsl:variable name="referenceValue" select="$vars[@n = $referenceName]/@value" />
  16. <xsl:value-of select="concat('The variable with the name ',$referenceValue)"/>
  17.  
  18. <xsl:value-of select="' '"/>
  19.  
  20. <xsl:variable name="referenceName2" select="'computedNumber'" />
  21. <xsl:variable name="referenceValue2" select="$vars[@n = $referenceName2]/@value" />
  22. <xsl:value-of select="concat('The variable with the name ',$referenceName2,$referenceValue2)"/>
  23. </xsl:template>
  24. </xsl:stylesheet>

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

猜你在找的XML相关文章