XSL:在模板之间传递变量 [英] XSL: passing variables between templates

查看:27
本文介绍了XSL:在模板之间传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将一个变量从一个父模板传递给它的子元素?

Is it possible to pass a variable from one parent template to its child element ?

<xsl:template match="structure">
  <xsl:variable name="var"><xsl:value-of select="@path" /></xsl:variable>
  <xsl:apply-templates select="folders">
    <xsl:with-param name="var1" select="'{var}'"/>
  </xsl:apply-templates>
</xsl:template> 

此模板将匹配:

<xsl:template match="folder">
  <xsl:param name="var1"/>
  <xsl:value-of select="$var1"/>
</xsl:template>

你知道我想在匹配的模板中使用 var 作为 var1.

You see I wanna use var as var1 in the matched template.

我怎样才能做到这一点?

How can I make this work?

结构是这样的:

<structure path="C:\xampplite\htdocs\xampp">
  <folders>
    <folder name="img">
      <date>01/28/10 21:59:00</date>
      <size>37.4 KB</size>
    </folder>
 </folders>
</structure>

<xsl:template match="folder">
<xsl:variable name="var1"><xsl:value-of select="../../@path"/></xsl:variable>
<xsl:variable name="var2"><xsl:value-of select="@name" /></xsl:variable>
<xsl:variable name="var3"><xsl:value-of select="$var1"/>\<xsl:copy-of select="$var2"/>    </xsl:variable>
 <th colspan="2" align="left"  bgcolor="#FF5500"><a onclick="foo('{$var3}')"><xsl:value-of select="$var3"/></a></th>

在 jscript 函数中,字符串没有反斜杠.有谁知道为什么?

in the jscript function the string is without its backslashes. anyone knows why?

C:xampplitehtdocsxamppimg

C:xampplitehtdocsxamppimg

推荐答案

您可以将参数传递给您通过 调用的命名模板,例如:

You can pass parameters to named templates that you call via <xsl:call-template>, e.g.:

<xsl:call-template name="name">
   <xsl:with-param name="param" select="xpathexpr"/>
</xsl:call-template>

<xsl:template name="name">
   <xsl:param name="param"/>
   ...
</xsl:template>

当您调用命名模板时,上下文节点是当前上下文.因此,要为子节点调用命名模板,您需要使用 <xsl:for-each>:

When you call a named template, the context node is the current context. So to call a named template for child nodes, you need to change the current context by using <xsl:for-each>:

<xsl:for-each select="child">
   <xsl:call-template name="name">
      <xsl:with-param name="param" select="xpathexpr"/>
   </xsl:call-template>
</xsl:for-each>

不过,在您的情况下,不需要传递参数,因为您尝试使用的变量是可从上下文节点导航到的变量.而且你不需要使用所有这些变量(也不应该给一个变量起一个像 var1 这样没用的名字):

In your case, though, there's no need to pass parameters, since the variable that you're trying to use is something that's navigable to from the context node. And you don't need to use all those variables (nor should you ever give a variable a name as useless as var1):

<xsl:template match="folder">
   <xsl:variable name="linkarg" value="concat(../../@path, '\\', @name)"/>
   <xsl:variable name="linktext" value="concat(../../@path, '\', @name)"/>
   <th colspan="2" align="left"  bgcolor="#FF5500">
      <a onclick="foo('{$linkarg}')">
         <xsl:value-of select="$linktext"/>
      </a>
   </th>
</xsl:template>

另外,我很想使用 ancestor::structure[1]/@path 而不是 ../../@path,因为它使意图更加明确;您的版本表示从父元素的父元素获取 path 属性",而我的版本表示遍历祖先元素链,直到找到第一个名为 structure,并获取其 path 属性."

Also, I'd be tempted to use ancestor::structure[1]/@path rather than ../../@path, because it makes the intention a lot more explicit; your version means "get the path attribute from the parent of the parent element", while my version means "traverse up the chain of ancestor elements until you find the first one named structure, and get its path attribute."

这篇关于XSL:在模板之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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