是否可以在xsl:key指令中使用变量? [英] Is it possible to use a variables inside xsl:key instruction?

查看:78
本文介绍了是否可以在xsl:key指令中使用变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Relating to this question, is it possible to use variables inside xsl:key? I want to do smth like this:

<xsl:key name="ChargesKey" match="$ChargesForDisplay/charge" use="Name"/>

我正在将XSLT 1.0与ASP.Net一起使用

I'm using XSLT 1.0 with ASP.Net

推荐答案

我相信我可以放心地假设您所指的是动态生成的节点集变量(与从源DOM中选择的变量相比,这些变量是微不足道的) ,是的,可以对动态生成的节点集变量的内容执行键匹配(如我针对该问题进行的演示).

I believe I can safely assume that you are referring to dynamically-generated node-set variables (as opposed to those selected from the source DOM, which are trivial), and yes it is possible to perform a key-match on the contents of a dynamically generated node-set variable (as I demonstrate for this question).

假设您有一个像这样的变量:

Assuming you have a variable like this:

<xsl:variable name="ChargesForDisplay">
<charge>
   <Name>Name1</Name>
</charge>
<charge>
   <Name>Name2</Name>
</charge>
<charge>
   <Name>Name1</Name>
</charge>
<charge>
   <Name>Name3</Name>
</charge>
</xsl:variable>

您将这样定义密钥:

<xsl:key name="ChargesKey" match="charge" use="Name"/>

然后您可以像这样应用它:

And then you can apply it like this:

  <xsl:template match="/">
      <xsl:apply-templates select="msxsl:node-set($ChargesForDisplay)" />
  </xsl:template>

  <xsl:template
   match="charge[generate-id(.)=generate-id(key('ChargesKey',Name)[1])]">
    <xsl:variable name="matchingItems" select="key('ChargesKey', Name)" />
    ...
  </xsl:template>

当然,如果变量包含从源XML DOM中选择的节点,那么它就是相同的方法,只是不需要使用msxsl:node-set().

Of course, if the variable contains a selection of nodes from the source XML DOM, then it's just the same approach, except you don't need to use msxsl:node-set().

我怀疑在源XML文档中也存在的节点名称上具有键或多个动态生成的节点集变量可能会导致分组产生意外的结果(因为key()函数将从两个位置定位节点)变量和源文档).因此,建议在节点上定义键,这些键只能出现在一个特定变量中,而不会出现在其他任何地方.

I suspect that having a key on a node-name that's also present in the source XML document or multiple dynamically-generated node-set variables may cause grouping to produce unexpected results(because the key() function would locate nodes from both the variable and the source document). For this reason, I'd suggest defining keys on nodes that would only be present in one particular variable and nowhere else.

这篇关于是否可以在xsl:key指令中使用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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