如何使用 xslt 选择第一次出现的节点 [英] How to select the first occurrence of a node using xslt

查看:25
本文介绍了如何使用 xslt 选择第一次出现的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于:

<bookstores>
    <bookstore>
        <book id="1">
            <author>ABC</author> 
        </book>
        <book id="2">
            <title>YYY</title> 
        </book>
    </bookstore>
    <bookstore>
        <book id="3">
            <author>ABC</author> 
        </book>
        <book id="4">
            <author>DEF</author> 
        </book>
    </bookstore>
    <bookstore>
        <book id="5">
            <price>50</price>
        </book>
        <book id="6">
            <title>ZZZ</title> 
        </book>
    </bookstore>
</bookstores>

我想选择book"节点的第一个子节点,或者换句话说,book"节点的所有唯一子节点.

I would like to select the first occurrence of the child of 'book' node, or in other words, all unique child node of the 'book' node.

所以输出应该是这样的:

So the output should be like:

author
title
price

我写了一个 xslt:

I wrote a xslt as:

<xsl:for-each select="bookstores/bookstore/book"> 
    <xsl:if test="count(preceding-sibling::*[1]) = 0">
        <xsl:value-of select="local-name(*[1])"/>
    </xsl:if>
</xsl:for-each>

它没有给我任何回报......有人可以帮我解决这个问题吗?谢谢!!

It returned me nothing...Could anyone give me some help on this? Thanks!!

更新:

如果我的 xml 中有几个 'bookstores' 元素,并且我只想限制每个 'bookstores' 上下文中的唯一性,以便即使 'author' 出现在一个 'bookstores' 中,它仍然可能是如果它出现在另一个书店"中,会显示吗?

What if I have several 'bookstores' elements in my xml, and I just would like to limit the uniqueness within the context of each 'bookstores' so that even 'author' appears in one 'bookstores', it could still be displayed if it appears in another 'bookstores'?

推荐答案

如果您使用的是 XSLT1.0,则获得不同元素的方法是通过一种称为 Muenchian Grouping 的技术.在您的情况下,您希望按 book 子元素分组",因此首先,您定义一个键以按元素名称查找 book 的子元素

If you are using XSLT1.0, the way to get distinct elements is by a technique called Muenchian Grouping. In your case you want to 'group' by book child elements, so to start with, you define a key to look up the child elements of books by the element name

 <xsl:key name="child" match="book/*" use="local-name()" />

要获得不同的名称,您可以查看所有 book 子元素,但只输出组中第一个为其给定名称出现的元素.你使用这个可怕的声明来做到这一点:

To get the distinct names, you then look at all book child elements, but only output the elements that occur first in the group for their given name. You do this using this scary statement:

<xsl:apply-templates 
   select="//book/*[generate-id() = generate-id(key('child', local-name())[1])]" />

这是完整的 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="text"/>
   <xsl:key name="child" match="book/*" use="local-name()" />

   <xsl:template match="/">
      <xsl:apply-templates select="//book/*[generate-id() = generate-id(key('child', local-name())[1])]" />
   </xsl:template>

   <xsl:template match="//book/*">
      <xsl:value-of select="concat(local-name(), '&#10;')" />
   </xsl:template>
</xsl:stylesheet>

应用于您的 XML 时,输出如下

When applied to your XML, the following is output

author
title
price

这篇关于如何使用 xslt 选择第一次出现的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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