How to improve my Muenchian grouping XSLT?

让人想犯罪 __ 提交于 2019-12-13 20:05:38

问题


Thanks to the help of Niraj and hr_117 I was able to arrive at an XSL template that turns this XML...

<bookings>
  <entry>
    <event>Christmas</event>
    <attendees>2</attendees>
  </entry>
  <entry>
    <event>Halloween</event>
    <attendees>2</attendees>
  </entry>
  <entry>
    <event>Easter</event>
    <attendees>1</attendees>
  </entry>
  <entry>
    <event>Easter</event>
    <attendees>1</attendees>
  </entry>
</bookings>

...into this output:

Christmas: 1 booking(s)

Halloween: 1 booking(s)

Easter: 2 booking(s)

This is my XSL:

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

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

<xsl:key name="bookings-within-period" match="bookings/entry" use="event" />

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

</xsl:stylesheet>

How can I get this function to calculate the sum of attendees as well?

This is the output I need:

Christmas: 1 booking(s), 2 attendee(s)

Halloween: 1 booking(s), 2 attendee(s)

Easter: 2 booking(s), 2 attendee(s)

Thanks to anybody who can help!


回答1:


Enhanced XSLT is as under:

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

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

  <xsl:key name="bookings-within-period" match="bookings/entry" use="event" />

  <xsl:template match="bookings">
    <xsl:for-each select="entry[count(. | key('bookings-within-period', event)[1]) = 1]">
      <p>
        <xsl:value-of select="concat(event,': ')" />
        <xsl:value-of select="count(key('bookings-within-period', event))" />
        <xsl:text> booking(s), </xsl:text>
        <xsl:value-of select="sum(key('bookings-within-period', event)/attendees)" />
        <xsl:text> attendee(s)</xsl:text>
      </p>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

where we have <xsl:value-of select="sum(key('bookings-within-period', event)/attendees)" /> to sum all the attendees




回答2:


Using key('bookings-within-period', event) gives you the entry element in each group and then sum(key('bookings-within-period', event)/attendees) gives you the sum of the attendees in each group.

And you need to do that inside the for-each where the first entry element in each group is the context node, in case that is not clear.



来源:https://stackoverflow.com/questions/16584221/how-to-improve-my-muenchian-grouping-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!