创建递归 XSL:If 语句?
我正在尝试设置一个 XSL:IF 语句,该语句仅显示节点位于两个值之间的条目。 很简单,对吧? 这只是一个 if 大于和 if 小于。 问题是,我需要针对最多 52 个节点进行测试,而不是针对一个节点进行测试。
假设我有一些如下所示的 XML:
<container>
<entry>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
</entry>
</container>
现在假设给定的范围为 9–15。 因为某些节点属于该范围,所以我想显示该条目。 但如果给我的范围是 11-15,则没有一个节点适合,所以我不希望它显示。
问题是...我完全不知道你会怎么做。 我知道您可以 IF 单个值,但我想不出测试每个节点的简单方法。
顺便说一句,这一切都是在 Symphony CMS 的最新稳定版本中完成的。
[编辑] 前两个结果的问题是它们显示 ITEM 节点,我正在寻找的是仅返回至少有一个匹配的 ITEM 节点的 ENTRY 节点。 我不确定任何解决方案将如何帮助解决这个问题。
I'm trying to set up an XSL:IF statement that only shows entries that have a node that falls between two values. Simple enough, right? It's just a if greater than and if less than. Problem is, instead of testing it against one node I need to test it against up to 52.
Let's say I have some XML that looks like this:
<container>
<entry>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
</entry>
</container>
Now say I'm given a range of 9–15. Because some of the nodes fall into that range I want to display that entry. But if I was given a range of 11–15 none of the nodes fit so I wouldn't want it displayed.
The problem is... I have absolutely no idea how you would do this. I know you can IF a single value but I can't think of a simple way to test each node.
By the way, this is all being done inside the latest stable release of the Symphony CMS.
[edit]
The problem with the first two results is they display the ITEM nodes, what I'm looking for is to return only ENTRY nodes that have at least one ITEM node that matches. I'm not sure how any of the solutions would help this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
匹配上的嵌套谓词来完成此操作:该表达式将读作“包含值在 9 到 15 之间的项目的条目”。
You can accomplish this using a nested predicate on the
<entry>
matches:The expression would read as "entries which contain items whose values are between 9 and 15".
XPath 语句“container/entry/item”指的是所有匹配的项目。 谓词 [number(.) >= 9 and number(.) <= 15] 会削减该列表。 某些 XSLT 操作(例如 xsl:value-of)具有仅获取第一个值的隐含过滤器。 在这些情况下,您可以使用 xsl:for-each:
The XPath statement 'container/entry/item' refers to all matching items. The predicate [number(.) >= 9 and number(.) <= 15] pares that list down. Some XSLT operations (e.g., xsl:value-of) have an implied filter that only grabs the first value. In these cases, you can use xsl:for-each:
这个怎么样……您可以在 for-each 循环中做任何您想做的事情,或者您可以只获取变量中返回的节点集并在其他地方使用它。
How about this.... You can do whatever you want to inside the for-each loop or you can just take the nodeset returned in the variable and use it somewhere else.