0

我有这个 XML:

<events>
  <event name="Christmas" attendees="1"/>
  <event name="Halloween" attendees="3"/>
  <event name="Easter" attendees="2"/>
  <event name="Easter" attendees="1"/>
</events>

感谢hr_117的帮助,我设法做到了:

<xsl:template match="data">
  <xsl:apply-templates select="events"/>
</xsl:template>

<xsl:key name="events-by-name" match="events/event" use="@name" />

<xsl:template match="events">
<xsl:for-each select="event[count(. | key('events-by-name', @name)[1]) = 1]">
    <p>
        <xsl:value-of select="concat(@name,': ')" />
        <xsl:value-of select="count(key('events-by-name', @name))" />
        <xsl:text> booking(s)</xsl:text>        
    </p>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

这给了我这个输出:

Christmas: 1 booking(s)
Halloween: 1 booking(s)
Easter: 2 booking(s)

但是我怎么能算attendees,所以我得到这个:

Christmas: 1 booking(s), 1 attendee(s)
Halloween: 1 booking(s), 3 attendee(s)
Easter: 2 booking(s), 1 attendee(s)

有人可以帮忙吗?

谢谢!

4

1 回答 1

3

这个怎么样?我用 sum 方法试了一下,我可以看到累计值,我还添加了一些额外的元素进行测试。

<xsl:value-of select="concat(@name,': ')"/>
<xsl:value-of select="count(key('events-by-name', @name))"/>
<xsl:text> booking(s), </xsl:text>
<xsl:value-of select="sum(key('events-by-name', @name)/@attendees)"/>
<xsl:text> attendee(s)</xsl:text>
于 2013-05-15T18:32:09.937 回答