如何输出这个XML作为排名?

Tin*_*n81 0 xml xslt xslt-1.0

我有这个XML:

<bookings>

  <booking>
    <event>Christmas Party</event>
    <source>Internet</source>
  </booking>

  <booking>
    <event>Church day</event>
    <source>Television</source>
  </booking>

  <booking>
    <event>Port Anniversary</event>
    <source>Television</source>
  </booking>

  <booking>
    <event>Disco Fever</event>
    <source>Internet</source>
  </booking>

  <booking>
    <event>Party Night</event>
    <source>Internet</source>
  </booking>

  <booking>
    <event>Christmas Party</event>
    <source>Flyer</source>
  </booking>    

</bookings>
Run Code Online (Sandbox Code Playgroud)

我需要像这样输出:

预订的主要营销来源:

  1. 互联网:3个预订
  2. 电视:2个预订
  3. 传单:1个预订

XSLT甚至可以实现吗?

谢谢你的帮助!

Ian*_*rts 5

在XSLT 1.0中,这种事情的常见答案是Muenchian分组.首先定义一个键

<xsl:key name="bookingsBySource" match="booking" use="source" />
Run Code Online (Sandbox Code Playgroud)

然后使用以下技巧迭代唯一的源值

<xsl:template match="/bookings">
  <ol>
    <xsl:for-each select="booking[generate-id() =
            generate-id(key('bookingsBySource', source)[1])]">
      <!-- sort by number of bookings, largest first -->
      <xsl:sort select="count(key('bookingsBySource', source))"
                data-type="number"
                order="descending" />
      <li>
        <xsl:value-of select="source"/>
        <xsl:text>: </xsl:text>
        <xsl:value-of select="count(key('bookingsBySource', source))" />
        <xsl:text> booking(s)</xsl:text>
      </li>
    </xsl:for-each>
  </ol>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

for-each选择仅拉出booking每个source值的第一个元素.

  • @ hr_117我的意思是使用`<ol>`(自动编号)而不是`<ul>`.我现在已经修好了. (2认同)