如何打破 XSLT for-each 循环? [英] How to break XSLT for-each loop?

查看:34
本文介绍了如何打破 XSLT for-each 循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在 XSLT for-each 循环中不可能中断.但是从以下示例中,如果我想显示某物"元素是否存在.然后我将通过循环,一旦我得到它,我想打破循环.

I know break is not possible in XSLT for-each loop. But from following example, If I want to show 'something' element exist or not. Then I will go through loop and once I got it I want to break loop.

  1. 我会让一个布尔变量说 isExist=false;
  2. 我会一一遍历所有节点.
  3. 一旦我得到某物",我会将其值更改为 isExist=true
  4. 想在这里打破循环.

现在我想中断循环因为我想将状态显示为真或假.但是如果我在第 3 步之后不使用 break,它将进入循环并将其更改为 false,我将获得 false 状态.

Now I want to break loop bcoz I want to show status as true or false. But If I will not use break after step 3, it will go to loop and make change it to false and I will get status as false.

我的问题是:如果不可能打破,那么如何达到这个结果?

我正在关注此链接 如何为不连续的元素中断 XSLT for-each 循环?

根据这个链接,我会得到前两次出现,但它没有说某物"存在与否.

According to this link I will get first two occurrence but it doen't say 'something' exist or not.

<root>
  <item/>
  <item/>
  <something/>
  <item/>
</root>

推荐答案

这不是变量在 XSLT 中的工作方式 - 变量总是在词法上限定于它们的包含元素,并且一旦设置就无法更改.

That's not how variables work in XSLT - variables are always lexically scoped to their containing element, and once set they cannot be changed.

您需要学会以声明方式而非程序方式进行思考 - 告诉处理器您希望它找到什么,而不是如何寻找它.XPath 表达式 boolean(/root/something) 会给你你想要的 - 如果有一个 something 元素作为 root 的子元素,则为 true> 否则为假.

You need to learn to think more declaratively rather than procedurally - tell the processor what you want it to find rather than how you would look for it. The XPath expression boolean(/root/something) will give you what you want - true if there is a something element as a child of the root and false otherwise.

这篇关于如何打破 XSLT for-each 循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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