Grouping XML elements based on attributes with XSL

2019-05-31 23:14发布

问题:

I am working on a webpage that publishes a schedule of presentations based on an XML feed that I don't have access to change.

The feed looks like this:

  <track name="Track 1">
    <session name="Session 1" starttime="2012-06-06 10:45" endtime="2012-06-06 12:45">
      <presentation name="Presentation 1">
        ...presentation info
      </presentation>
      <presentation name="Presentation 2">
        ...presentation info
      </presentation>
    </session>
    <session name="Session 2" starttime="2012-06-06 10:45" endtime="2012-06-06 12:45">
      <presentation name="Presentation 3">
        ...presentation info
      </presentation>
      <presentation name="Presentation 4">
        ...presentation info
      </presentation>
    </session>
    <session name="Session 3" starttime="2012-06-07 08:45" endtime="2012-06-07 10:45">
      <presentation name="Presentation 5">
        ...presentation info
      </presentation>
      <presentation name="Presentation 6">
        ...presentation info
      </presentation>
    </session>
  </track>

At present, I am doing an <xsl:for-each select="session"> loop to pull out information, however that ends with me outputting duplicate dates and times.

I have no problem doing the actual date and time parsing, so I am currently outputting June 6, 2012; 10:45 with no issue, but it is being duplicated each time due to the for-each, as follows:

June 6, 2012

10:45-12:45

Session 1: Presentation 1, Presentation 2

June 6, 2012

10:45-12:45

Session 2: Presentation 3, Presentation 4

June 7, 2012:

8:45-10:45

Session 3: Presentation 5, Presentation 6

What I would like is to somehow pull out all common datetimes, for instance, get output like:

June 6, 2012:

10:45-12:45

Session 1: Presentation 1, Presentation 2

Session 2: Presentation 3, Presentation 4

June 7, 2012:

8:45-10:45

Session 3: Presentation 5, Presentation 6

For reference, here is my current implementation:

<xsl:for-each select="session">
  <h4>
    <!-- output to Month, DD, YYYY -->
    <xsl:call-template name="formatDate">
      <xsl:with-param name="dateTime" select="@starttime" />
    </xsl:call-template>
  </h4>

  <h5>
    <!-- output time -->
    <xsl:call-template name="formatTime">
      <xsl:with-param name="dateTime" select="@starttime" />
    </xsl:call-template> - 
    <xsl:call-template name="formatTime">
      <xsl:with-param name="dateTime" select="@endtime" />
    </xsl:call-template>
  </h5>

  <!-- session title -->
  <h5><xsl:value-of select="@name"/></h5>

  <!-- presentation title -->          
  <xsl:for-each select="presentation">
    <xsl:value-of select="@name"/><xsl:element name="br"/>
  </xsl:for-each>

</xsl:for-each>

And the date-time formatter:

<!-- formatting dateTime -->
<xsl:template name="formatDate">
  <xsl:param name="dateTime" />
  <xsl:variable name="date" select="substring-before($dateTime, ' ')" />
  <xsl:variable name="year" select="substring-before($date, '-')" />
  <xsl:variable name="month" select="number(substring-before(substring-after($date, '-'), '-'))" />
  <xsl:variable name="day" select="number(substring-after(substring-after($date, '-'), '-'))" />

  <!-- output -->
  <xsl:choose>
    <xsl:when test="$month = '1'">January</xsl:when>
    <xsl:when test="$month = '2'">February</xsl:when>
    <xsl:when test="$month = '3'">March</xsl:when>
    <xsl:when test="$month = '4'">April</xsl:when>
    <xsl:when test="$month = '5'">May</xsl:when>
    <xsl:when test="$month = '6'">June</xsl:when>
    <xsl:when test="$month = '7'">July</xsl:when>
    <xsl:when test="$month = '8'">August</xsl:when>
    <xsl:when test="$month = '9'">September</xsl:when>
    <xsl:when test="$month = '10'">October</xsl:when>
    <xsl:when test="$month = '11'">November</xsl:when>
    <xsl:when test="$month = '12'">December</xsl:when>
  </xsl:choose>
  <xsl:value-of select="' '" />
  <xsl:value-of select="$day" />
  <xsl:value-of select="', '" />
  <xsl:value-of select="$year" />
</xsl:template>

<!-- formatting dateTime -->
<xsl:template name="formatTime">
  <xsl:param name="dateTime" />
  <xsl:value-of select="substring-after($dateTime, ' ')" />
</xsl:template>

回答1:

You want to group using the Muenchian method. Add this immediately inside the root element of your stylesheet:

<xsl:key name="sessions-by-track-name-starttime-and-endtime" match="/track/session" use="concat(parent::track/@name, ' ', @starttime, ' ', @endtime)"/>

Then update your XSLT as shown:

<xsl:for-each select="session[generate-id() = generate-id(key('sessions-by-track-name-starttime-and-endtime', concat(parent::track/@name, ' ', @starttime, ' ', @endtime))[1])]">
  <h4>
    <!-- output to Month, DD, YYYY -->
    <xsl:call-template name="formatDate">
      <xsl:with-param name="dateTime" select="@starttime" />
    </xsl:call-template>
  </h4>

  <h5>
    <!-- output time -->
    <xsl:call-template name="formatTime">
      <xsl:with-param name="dateTime" select="@starttime" />
    </xsl:call-template> - 
    <xsl:call-template name="formatTime">
      <xsl:with-param name="dateTime" select="@endtime" />
    </xsl:call-template>
  </h5>

  <xsl:for-each select="key('sessions-by-track-name-starttime-and-endtime', concat(parent::track/@name, ' ', @starttime, ' ', @endtime))">

    <!-- session title -->
    <h5><xsl:value-of select="@name"/></h5>

    <!-- presentation title -->          
    <xsl:for-each select="presentation">
      <xsl:value-of select="@name"/><xsl:element name="br"/>
    </xsl:for-each>

  </xsl:for-each>

</xsl:for-each>


回答2:

Firstly, avoid for-each in XSLT and apply your nodes to templates instead.

Try this (condensed, and without the calls to call-template as you didn't post that bit), which you can run at this XMLPlayground.

<!-- sessions -->
<xsl:template match='track/session'>
    <xsl:if test='not(preceding-sibling::session[@starttime = current()/@starttime])'>
        <h4><xsl:value-of select="@starttime" /></h4>
        <h5><xsl:value-of select="concat(@starttime,' - ',@endtime)" /></h5>
        <p><xsl:apply-templates select="presentation" /></p>
        <xsl:variable name='other_pres' select="following-sibling::session[@starttime = current()/@starttime]/presentation" />
        <xsl:if test='count($other_pres)'>
            <p><xsl:apply-templates select="$other_pres" /></p>
        </xsl:if>
    </xsl:if>
</xsl:template>

<!-- presentations -->
<xsl:template match='presentation'>
    <xsl:if test='position() = 1'>
        <strong><xsl:value-of select='../@name' />: </strong>
    </xsl:if>
    <xsl:value-of select="@name"/>
    <xsl:if test='position() != last()'>, </xsl:if>
</xsl:template>

The concept here is, inside the session template, for each session, we first see if we've previously already processed a session with the same @starttime (since I assume this is the attribute you meant was responsible for the duplication). If so, we skip it.

Then, at the point of outputting a session's presentations (which are handled by their own template, you'll notice), we also process any presentations of sibling nodes of the current session.

Output: (without access to your date formatting template)

<h4>2012-06-06 10:45</h4>
<h5>2012-06-06 10:45 - 2012-06-06 12:45</h5>
<p><strong>Session 1: </strong>Presentation 1, Presentation 2</p><p><strong>Session 2: </strong>Presentation 3, Presentation 4</p>

<h4>2012-06-07 08:45</h4>
<h5>2012-06-07 08:45 - 2012-06-07 10:45</h5>
<p><strong>Session 3: </strong>Presentation 5, Presentation 6</p>