创建递归 XSL:If 语句?

发布于 2024-07-25 03:05:24 字数 898 浏览 3 评论 0原文

我正在尝试设置一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嗫嚅 2024-08-01 03:05:24

您可以使用 匹配上的嵌套谓词来完成此操作:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:for-each select="container/entry[item[number(.) >= 9 and number(.) <= 15]]">
            <!-- this will loop over <entry>s which contain <item>s within your range -->
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

该表达式将读作“包含值在 9 到 15 之间的项目的条目”。

You can accomplish this using a nested predicate on the <entry> matches:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:for-each select="container/entry[item[number(.) >= 9 and number(.) <= 15]]">
            <!-- this will loop over <entry>s which contain <item>s within your range -->
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

The expression would read as "entries which contain items whose values are between 9 and 15".

怪我太投入 2024-08-01 03:05:24
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:copy-of select="container/entry/item[number(.) >= 9 and number(.) <= 15]"/>
    </xsl:template>
</xsl:stylesheet>

XPath 语句“container/entry/item”指的是所有匹配的项目。 谓词 [number(.) >= 9 and number(.) <= 15] 会削减该列表。 某些 XSLT 操作(例如 xsl:value-of)具有仅获取第一个值的隐含过滤器。 在这些情况下,您可以使用 xsl:for-each:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:for-each select="container/entry/item[number(.) >= 9 and number(.) < 15]">
            <xsl:value-of select="."/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:copy-of select="container/entry/item[number(.) >= 9 and number(.) <= 15]"/>
    </xsl:template>
</xsl:stylesheet>

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:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:for-each select="container/entry/item[number(.) >= 9 and number(.) < 15]">
            <xsl:value-of select="."/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
梦断已成空 2024-08-01 03:05:24

这个怎么样……您可以在 for-each 循环中做任何您想做的事情,或者您可以只获取变量中返回的节点集并在其他地方使用它。

   <?xml version='1.0'?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <!-- Your first param -->
        <xsl:param name="Param1" select="4"/>
        <!-- Your second param -->
        <xsl:param name="Param2" select="9"/>
            <xsl:variable name="ResultNodeSet">
                <xsl:for-each select="/container/entry/item[number(.) >= $Param1 and number(.) <= $Param2]">
                  <!-- What ever else you want to do can go here-->
                  <xsl:copy-of select="."/>
                </xsl:for-each>
            </xsl:variable>  
        <xsl:value-of select="$ResultNodeSet"/>
    </xsl:template> 
    </xsl:stylesheet>

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.

   <?xml version='1.0'?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <!-- Your first param -->
        <xsl:param name="Param1" select="4"/>
        <!-- Your second param -->
        <xsl:param name="Param2" select="9"/>
            <xsl:variable name="ResultNodeSet">
                <xsl:for-each select="/container/entry/item[number(.) >= $Param1 and number(.) <= $Param2]">
                  <!-- What ever else you want to do can go here-->
                  <xsl:copy-of select="."/>
                </xsl:for-each>
            </xsl:variable>  
        <xsl:value-of select="$ResultNodeSet"/>
    </xsl:template> 
    </xsl:stylesheet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文