我有这个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>
我需要像这样输出:
预订的主要营销来源:
- 互联网:3个预订
- 电视:2个预订
- 传单:1个预订
醇>
XSLT甚至可以实现吗?
感谢您的帮助!
答案 0 :(得分:5)
XSLT 1.0中此类事情的常见答案是 Muenchian分组。首先定义一个键
<xsl:key name="bookingsBySource" match="booking" use="source" />
然后使用以下技巧迭代唯一的源值
<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>
for-each select仅为每个booking
值提取第一个source
元素。