2

我希望能够从与属性值匹配的集合中选择第一个 XML 元素。为了解释更多,假设我们有以下 XML:

<events>
    <event date="2013-06-17">
        <person first="Joe" last="Bloggs" />
        <person first="John" last="Smith" />
    </event>
    <event date="2013-01-29">
        <person first="Jane" last="Smith" />
        <person first="John" last="Smith" />
    </event>
    <event date="2012-09-03">
        <person first="Joe" last="Bloggs" />
        <person first="John" last="Doe" />
    </event>
    <event date="2012-04-05">
        <person first="Jane" last="Smith" />
        <person first="John" last="Smith" />
        <person first="John" last="Doe" />
    </event>
<event>

我想在第一个和最后一个属性上选择一组唯一的人元素匹配,即结果集如下所示:

<person first="Joe" last="Bloggs" />
<person first="John" last="Doe" />
<person first="Jane" last="Smith" />
<person first="John" last="Smith" />

有许多解决方案都是这个主题的变体:

<xsl:for-each select="//person">
    <xsl:if test="not( preceding::person[ @first = current()/@first and @last = current()/@last ] )">
        <xsl:apply-templates select="." />
    </xsl:if>
</xsl:for-each>

但是,在我看来,我应该能够将 中的测试作为从xsl:if中选择的谓词xsl:for-each,例如

<xsl:apply-templates select="//person[ not( preceding::person[ @first = current()/@first and @last = current()/@last ] ) ]" />

当然,虽然该current()函数不喜欢这样,但我只是想知道是否有人知道如何在单个 XPath 语句中完成此操作?

4

1 回答 1

4

这是使用for-each-groupwith的 XSLT 2.0 方法group-by

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

<xsl:template match="events">
  <xsl:for-each-group select="event/person" group-by="concat(@first, '|', @last)">
    <xsl:copy-of select="."/>
  </xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>

它转变

<events>
    <event date="2013-06-17">
        <person first="Joe" last="Bloggs" />
        <person first="John" last="Smith" />
    </event>
    <event date="2013-01-29">
        <person first="Jane" last="Smith" />
        <person first="John" last="Smith" />
    </event>
    <event date="2012-09-03">
        <person first="Joe" last="Bloggs" />
        <person first="John" last="Doe" />
    </event>
    <event date="2012-04-05">
        <person first="Jane" last="Smith" />
        <person first="John" last="Smith" />
        <person first="John" last="Doe" />
    </event>
</events>

进入

<person first="Joe" last="Bloggs"/>
<person first="John" last="Smith"/>
<person first="Jane" last="Smith"/>
<person first="John" last="Doe"/>

使用 XSLT 1.0,您可以使用

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

<xsl:key name="by-name" match="event/person" use="concat(@first, '|', @last)"/>

<xsl:template match="events">
  <xsl:for-each select="event/person[generate-id() = generate-id(key('by-name', concat(@first, '|', @last))[1])]">
    <xsl:copy-of select="."/>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

有关说明,请参见http://www.jenitennison.com/xslt/grouping/muenchian.xml

Muenchian 分组可以简化为

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

<xsl:key name="by-name" match="event/person" use="concat(@first, '|', @last)"/>

<xsl:template match="events">
  <xsl:copy-of select="event/person[generate-id() = generate-id(key('by-name', concat(@first, '|', @last))[1])]"/>
</xsl:template>

</xsl:stylesheet>

当然,除了复制到结果树之外,您还可以这样做apply-templates并以这种方式转换每个组中的第一项:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

<xsl:key name="by-name" match="event/person" use="concat(@first, '|', @last)"/>

<xsl:template match="events">
  <xsl:apply-templates select="event/person[generate-id() = generate-id(key('by-name', concat(@first, '|', @last))[1])]"/>
</xsl:template>

<xsl:template match="person">
  <foo>...</foo>
</xsl:template>

</xsl:stylesheet>

使用 XSLT 2.0,for-each-group您将需要使用存储项目的变量,例如

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

<xsl:template match="events">
  <xsl:variable name="first-person-in-groups" as="element(person)*">
    <xsl:for-each-group select="event/person" group-by="concat(@first, '|', @last)">
      <xsl:copy-of select="."/>
    </xsl:for-each-group>
  </xsl:variable>
  <xsl:apply-templates select="$first-person-in-groups"/>
</xsl:template>

<xsl:template match="person">
  <foo>...</foo>
</xsl:template>

</xsl:stylesheet>

这样你就可以使用一系列person元素apply-templates

于 2013-06-21T09:43:26.757 回答