xslt 变量作用域及其用法 [英] xslt variable scope and its usage

查看:25
本文介绍了xslt 变量作用域及其用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 xslt 并且有一个关于如何在 diff 中使用 xslt 变量的问题.对于每个循环.我知道 xslt 不是过程语言,因此无法在另一个循环中访问在 for 循环中声明的变量.但是有什么方法可以声明全局变量然后在第一个 for 循环中分配一些值并在第二个 for 循环中使用该变量?

I am learning xslt and had one question about how can i use xslt variable in diff. for each loop. I know xslt isn't a procedural language so variable declared in for loop cannot be accessed in another loop. But is there any way I can just declare global variable then assign some value in first for loop and use that variable in second for loop?

任何想法将不胜感激.

谢谢

推荐答案

有什么方法可以声明全局变量然后分配一些第一个 for 循环中的值并在第二个 for 循环中使用该变量?

is there any way I can just declare global variable then assign some value in first for loop and use that variable in second for loop?

xsl:for-each 中为 xsl:variable(当然这只是初始化)赋值的方法是包括变量主体中的xsl:for-each.

The way to assign value to an xsl:variable (of course this is only initialization) from within an xsl:for-each, is to include the xsl:for-each in the body of the variable.

这是一个完整的例子:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
     <xsl:variable name="vMax">
       <xsl:for-each select="num">
        <xsl:sort data-type="number" order="descending"/>

        <xsl:if test="position() = 1">
         <xsl:value-of select="."/>
        </xsl:if>
       </xsl:for-each>
     </xsl:variable>

     Values close to the maximum:
<xsl:text/>

       <xsl:for-each select="num">
        <xsl:if test="not($vMax - . > 3) ">
         <xsl:value-of select="."/>
         <xsl:text>&#xA;</xsl:text>
        </xsl:if>
       </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时...

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

...它首先定义了一个 vMax 变量,该变量从包含在其主体中的 xsl:for-each 获取其值.

...it first defines a vMax variable that gets its value from the xsl:for-each contained in its body.

然后在第二个 xsl:for-each 中使用 vMax 变量来输出所有接近"如此计算的最大值的数字.

Then the vMax variable is used in the second xsl:for-each to output all numbers that are "close" to the so computed maximum.

产生了想要的、正确的结果:

     Values close to the maximum:
07
08
09
10

还可以通过使用递归调用的命名模板模拟分配"具有不同值的变量并将新值"作为参数传递给被调用模板.

这是一个展示这种技术的例子.在这里,我们正在计算包​​含在节点集的节点中的最大值.每次访问节点集中的下一个节点时,都会将当前最大值与该值进行比较,如有必要,新的最大值将成为下一个节点的值.然后我们递归调用相同的模板,将当前最大值作为新最大值传递:

Here is an example showing this technique. Here we are calculating the maximum of values, contained in nodes of a node-set. Every time we access the next node in the node-set, the current maximum is compared to this value and if necessary the new maximum becomes the value of the next node. We then call the same template recursively, passing as the value of the current maximum the new maximum:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
  <xsl:call-template name="max">
    <xsl:with-param name="pList" select="*"/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="max">
   <xsl:param name="pMax" select="-99999999"/>
   <xsl:param name="pList"/>

   <xsl:choose>
     <xsl:when test="$pList[1]">
       <xsl:variable name="vnewMax" select=
       "$pMax * ($pMax >= $pList[1])
       +
        $pList[1] * not($pMax >= $pList[1])
       "/>

       <xsl:call-template name="max">
        <xsl:with-param name="pMax" select="$vnewMax"/>
        <xsl:with-param name="pList" select="$pList[position() > 1]"/>
       </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
       <xsl:value-of select="$pMax"/>
     </xsl:otherwise>
   </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于同一个 XML 文档(上图)时,会产生想要的正确结果:

10

这篇关于xslt 变量作用域及其用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆