通过XSL键走/循环:如何? [英] Walk/loop through an XSL key: how?

查看:156
本文介绍了通过XSL键走/循环:如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < xsl:key 

有没有一种方法可以遍历一个键并输出它包含的所有值? name =kElementmatch =Element / Element [@idref]use =@ idref/>

我这样做:

 < xsl:for-each select =key('kElement','。')> 
< li>< xsl:value-of select =。 />< /锂>
< / xsl:for-each>

但是,这不起作用。

问题很简单:如何做到这一点?

解决方案

你不能。这不是什么关键。



您可以通过调用 key()当且仅当每个元素的键是相同的。



如果您需要循环遍历所有键的定义,您可以使用< key> 元素的 match =...属性。



所以如果你有这样的文件:

 < root> 
< element name =Bill/>
< element name =Francis/>
< element name =Louis/>
< element name =Zoey/>
< / root>

定义这样一个键:

 < xsl:key name =survivorsmatch =elementuse =@ name/> 

您可以循环使用匹配内容属性:

 < xsl:for-each select =element> 
<! - stuff - >
< / xsl:for-each>

另外,如果每个元素都有一些共同点:$ b​​
$ b

 < root> 
< element name =Billclass =survivor/>
< element name =Francisclass =survivor/>
< element name =Louisclass =survivor/>
< element name =Zoeyclass =survivor/>
< / root>

然后你可以像这样定义你的键:

 < xsl:key name =survivorsmatch =elementuse =@ class/> 

然后遍历所有像这样的元素:

 < xsl:for-each select =key('survivors','survivor')> 
<! - stuff - >
< / xsl:for-each>

因为每个元素为 class








$ b $ c>< xsl:key name =kElementmatch =Element / Element [@idref]use =@ idref/>

所以你可以循环它所有的东西:

 < xsl:for-each select =元素/元素[@idref]> 
<! - stuff - >
< / xsl:for-each>


Is there a way to walk-through a key and output all the values it contains?

   <xsl:key name="kElement" match="Element/Element[@idref]" use="@idref" />

I though of it this way:

<xsl:for-each select="key('kElement', '.')">
  <li><xsl:value-of select="." /></li>
</xsl:for-each>

However, this does not work. I simply want to list all the values in a key for testing purposes.

The question is simply: how can this be done?

解决方案

You can't. That's not what keys are for.

You can loop through every element in a key using a single call to key() if and only if the key of each element is the same.

If you need to loop over everything the key is defined over, you can use the expression in the match="..." attribute of your <key> element.

So if you had a file like this:

<root>
  <element name="Bill"/>
  <element name="Francis"/>
  <element name="Louis"/>
  <element name="Zoey"/>
</root>

And a key defined like this:

<xsl:key name="survivors" match="element" use="@name"/>

You can loop through what the key uses by using the contents of its match attribute:

<xsl:for-each select="element">
  <!-- stuff -->
</xsl:for-each>

Alternatively, if each element had something in common:

<root>
  <element name="Bill" class="survivor"/>
  <element name="Francis" class="survivor"/>
  <element name="Louis" class="survivor"/>
  <element name="Zoey" class="survivor"/>
</root>

Then you could define your key like this:

<xsl:key name="survivors" match="element" use="@class"/>

And iterate over all elements like this:

<xsl:for-each select="key('survivors', 'survivor')">
  <!-- stuff -->
</xsl:for-each>

Because each element shares the value "survivor" for the class attribute.

In your case, your key is

<xsl:key name="kElement" match="Element/Element[@idref]" use="@idref" />

So you can loop through everything it has like this:

<xsl:for-each select="Element/Element[@idref]">
  <!-- stuff -->
</xsl:for-each>

这篇关于通过XSL键走/循环:如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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