XSLT:选择独特但与其他示例稍有不同的XSLT: [英] XSLT: select distinct but slightly different to other examples

查看:78
本文介绍了XSLT:选择独特但与其他示例稍有不同的XSLT:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XML:

<a>
    <b>
        <d>D1 content (can include child nodes)</d>
    </b>
    <b>
        <c>C1 content (can include child nodes)</c>
    </b>
    <b>
        <e>E1 content (can include child nodes)</e>
    </b>
    <b>
        <c>C2 content (can include child nodes)</c>
    </b>
</a>

使用XSLT 1.0,我需要从中简单生成: cde;即按节点名称排序的/ a / b /的直接子节点名称的不同列表。每个b都有一个任意名字的孩子。

Using XSLT 1.0, I need to produce from this simply: "cde"; i.e. a distinct list of the names of the immediate children of /a/b/ ordered by the node name. Each b has exactly one child of arbitrary name.

我可以产生 ccde:

I can produce "ccde":

<xsl:for-each select="/a/b/*">
    <xsl:sort select="name(.)"/>
    <xsl:value-of select="name(.)" />
</xsl:for-each>

我尝试使用通常的before-sibling ::比较,但由于每个b只有一个

I've tried using the usual preceding-sibling:: comparison, but as each b only has one child, the preceding sibling is always nothing.

推荐答案

首先将以下关键元素添加到XSL的顶部:-

First add this key element to the top of your XSL:-

<xsl:key name="tagNames" match="/a/b/*" use="name()" /> 

现在,您的每个循环看起来像这样:-

Now your for each loop can look like this:-

<xsl:template match="/*">
    <xsl:for-each select="/a/b/*[count(. | key('tagNames', name())[1]) = 1]">
        <xsl:sort select="name()" />
        <xsl:value-of select="name()" />
    </xsl:for-each>
</xsl:template>

这篇关于XSLT:选择独特但与其他示例稍有不同的XSLT:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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