XSLT:如何找到节点的唯一子节点的数量? [英] XSLT: How to find the count of unique children of a node?

查看:27
本文介绍了XSLT:如何找到节点的唯一子节点的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 XML 如下所示:

My XML looks like this:

<foo>
    <bar name="a">
        <baz name="xyz">
            <time>2</time>
            <date>3</date>
        </baz>
    </bar>
    <bar name="b">
        <baz name="xyz">
            <time>2</time>
            <date>3</date>
        </baz>
    </bar>
    <bar name="c">
        <baz name="xyz">
            <time>2</time>
            <date>3</date>
        </baz>
    </bar>
</foo>

我正在编写一个 XSL,它需要像这样运行:如果所有 baz 子项都相同,则 doSomething else doSomethingElse.我当前的节点是 foo.

I am writing an XSL that needs to function like this: If all the baz children are same then doSomething else doSomethingElse. My current node is foo.

我是 XSLT 的新手,我知道 XSL 中的条件.现在看起来像这样:

I am new to XSLT and I am aware of the conditionals in XSL. It looks something like this as of now:

<xsl:template match="foo">   
<xsl:choose>
    <xsl:when test="[My condition]"> 
        doSomething()
    </xsl:when>
    <xsl:otherwise>
        doSomethingElse()
    </xsl:otherwise>
</xsl:choose>
</xsl:template>

在当前示例中,它应该 doSomething() 因为所有 baz 元素都相同.

In the current example, it should doSomething() as all the baz elements are the same.

如果我找出唯一的 baz 元素的数量,我可以测试它是否等于 1.如果是,那么我会doSomething() else doSomethingElse()

If I find out the number of unique baz elements, I can test whether it is equal to one. If it is, then I will doSomething() else doSomethingElse()

我应该如何实现这一点?MyCondition 应该是什么?

How should I implement this? What should MyCondition be?

PS:我的 XSL 版本是 1.0

PS: My XSL version is 1.0

推荐答案

如果所有 baz 子节点都相同,则 doSomething elsedoSomethingElse.我当前的节点是 foo.

If all the baz children are same then doSomething else doSomethingElse. My current node is foo.

这令人困惑,因为:

  • baz 不是 foo 的孩子;
  • 您的标题说找到唯一孩子的数量" - 但事实并非如此必须找到它才能知道它们是否相同.
  • baz are not children of foo;
  • your title says "find the count of unique children" - but it is not necessary to find it in order to know if they are same.

尝试类似:

<xsl:template match="foo">
    <xsl:variable name="first-baz" select="(bar/baz)[1]" />
    <xsl:choose>
        <xsl:when test="bar/baz[date!=$first-baz/date or time!=$first-baz/time]">
            <!-- THEY ARE NOT ALL SAME -->
        </xsl:when>
        <xsl:otherwise>
            <!-- THEY ARE ALL SAME -->
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

请注意,这里假设每个 baz 都有一个 date 和一个 time.否则你需要测试 not(date=$first-baz/date)

Note that this assumes that every baz has a date and a time. Otherwise you need to test for not(date=$first-baz/date) etc.

另见:http://www.jenitennison.com/xslt/grouping/muenchian.html

现在,假设所有 bar/baz 元素都有相同的标签(不是必须 datetime 但说 a, bc),什么会是那个案例的测试属性?

Now, assuming all the bar/baz elements have the same tags (not necessarily date and time but say a, b and c), what would be the test attribute for that case?

这使得它变得更加复杂.你仍然可以构造一个密钥:

This makes it significantly more complex. Still you could construct a key:

<xsl:key name="first-baz" match="foo/bar[1]/baz[1]/*" use="name()" />

然后进行测试:

<xsl:when test="bar/baz/*[. != key('first-baz', name())]">

如果存在 baz 的任何子节点,其字符串值与作为第一个 baz 子节点的同名节点不同,则返回 true.

This returns true if any child of baz exists whose string-value is different from an equally named node that is child of the first baz.

请注意,此处定义的键是文档范围的.如果要将测试限制为当前 foo 祖先,则必须在键中包含其 id.

Note that the key, as defined here, is document-wide. If you want to restrict the test to the current foo ancestor, then you must include its id in the key.

这篇关于XSLT:如何找到节点的唯一子节点的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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