Search

<xsl:for-each select="more-diary-dates/entry[event/@handle = 'county-weekend']">

If I have a bunch of XML that has loads of entries, some of which have that event/@handle as ‘county-weekend’ - surely it should display all those entries that have county-weekend in their event handle.

Or am I wrong?

Could you post your XML and XSL?

<more-diary-dates>
<entry id="145">
            <event handle="county-weekend">County Weekend</event>
            <info word-count="3" mode="formatted"><p>Clwyd v Worcestershire</p></info>
            <date>
                <date timeline="1" type="range">
                    <start iso="2009-10-03T08:24:00+01:00" time="00:24" weekday="6" offset="-0700">2009-10- </start>
                    <end iso="2009-10-04T08:24:00+01:00" time="00:24" weekday="0" offset="-0700">2009-10-</end>
                </date>
            </date>
        </entry>
    </more-diary-dates>


<xsl:for-each select="more-diary-dates/entry[event/@handle = 'county-weekend']">
<dl class="diary-date">
<xsl:choose>
<xsl:when test="date/date/@type = 'range'">
<dt class="date">
    <xsl:call-template name="format-date">
        <xsl:with-param name="date" select="date/date/start"/>
        <xsl:with-param name="format" select="'D M'"/>
    </xsl:call-template>
    <xsl:text> - </xsl:text>
    <xsl:call-template name="format-date">
        <xsl:with-param name="date" select="date/date/end"/>
        <xsl:with-param name="format" select="'D M Y'"/>
    </xsl:call-template>
</dt>
</xsl:when>


<xsl:otherwise>
<dt class="date">
    <xsl:call-template name="format-date">
        <xsl:with-param name="date" select="date/date/start"/>
        <xsl:with-param name="format" select="'D M Y'"/>
    </xsl:call-template>
</dt>
</xsl:otherwise>
</xsl:choose>
<dd class="info"><xsl:copy-of select="info/*"/></dd>
<xsl:if test="link != ''">
<dd class="link"><a href="{link}" target="_blank">Click here for more information</a></dd>
</xsl:if>
</dl>
</xsl:for-each>

The XML I showed you has multiple entries with the same event handle. I am basically getting 1 result come back with this XSL.

Would you mind posting more of your XML, also, include the parent elements of more-diary-dates? As well as showing me where you are calling the for-each in your XSL so I can get a better idea of what’s going on.

If I have a bunch of XML that has loads of entries, some of which have that event/@handle as ‘county-weekend’ - surely it should display all those entries that have county-weekend in their event handle.

Or am I wrong?

Telling us what you are seeing displayed will also be useful. Anything? All entries? Nothing at all?

Before you posted your code above, I put together a basic example to show that your code works with the predicate either in a for-each or an apply-templates.

Example XML based off your original post…

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <more-diary-dates>
        <entry id="1">
            <title handle="event-one">Event One</title>
            <event handle="county-weekend">County Weekend</event>
        </entry>
        <entry id="2">
            <title handle="event-two">Event Two</title>
            <event handle="city-weekend">City Weekend</event>
        </entry>
        <entry id="3">
            <title handle="event-three">Event Three</title>
            <event handle="county-weekend">County Weekend</event>
        </entry>
        <entry id="4">
            <title handle="event-four">Event Four</title>
            <event handle="county-weekend">County Weekend</event>
        </entry>
        <entry id="5">
            <title handle="event-five">Event Five</title>
            <event handle="city-weekend">City Weekend</event>
        </entry>
        <entry id="6">
            <title handle="event-six">Event Six</title>
            <event handle="state-weekend">State Weekend</event>
        </entry>
        <entry id="7">
            <title handle="event-seven">Event Seven</title>
            <event handle="county-weekend">County Weekend</event>
        </entry>
    </more-diary-dates>
</data>

Here’s the XSL with apply-templates first and your for-each listed second.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" encoding="UTF-8" />
    <xsl:template match="/">
        <h2>Using Apply-Templates</h2>
        <xsl:apply-templates select="data/more-diary-dates/entry[event/@handle='county-weekend']"/>

        <hr />

        <h2>Using For Each</h2>
        <xsl:for-each select="data/more-diary-dates/entry[event/@handle = 'county-weekend']">
            <dl>
                <dt style="color:red;"><xsl:value-of select="title"/></dt>
                <dd>Event: <xsl:value-of select="event"/></dd>
            </dl>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="entry">
        <dl>
            <dt><xsl:value-of select="title"/></dt>
            <dd>Event: <xsl:value-of select="event"/></dd>
        </dl>
    </xsl:template>

</xsl:stylesheet>

If you run this example code above, your XML or HTML, should look like this…

<h2>Using Apply-Templates</h2><dl>
  <dt>Event One</dt>
  <dd>Event: County Weekend</dd>
</dl><dl>
  <dt>Event Three</dt>
  <dd>Event: County Weekend</dd>
</dl><dl>
  <dt>Event Four</dt>
  <dd>Event: County Weekend</dd>
</dl><dl>
  <dt>Event Seven</dt>
  <dd>Event: County Weekend</dd>
</dl><hr/><h2>Using For Each</h2><dl>
  <dt style="color:red;">Event One</dt>
  <dd>Event: County Weekend</dd>
</dl><dl>
  <dt style="color:red;">Event Three</dt>
  <dd>Event: County Weekend</dd>
</dl><dl>
  <dt style="color:red;">Event Four</dt>
  <dd>Event: County Weekend</dd>
</dl><dl>
  <dt style="color:red;">Event Seven</dt>
  <dd>Event: County Weekend</dd>
</dl>

I’m actually retired for the evening as I have to get up early.

But this is what I am getting returned:

http://www.clwydcountydarts.co.uk/county/

Just 1 record.

I may not reply till the morning, but I appreciate the help guys, this seems so simple to me, which is why I can’t fathom it.

But then I generally attract oddities when it comes to stuff like this.

XML Utility Main page

@NickToye -

I think I found your problem… it’s in your XML. It looks like you have a space at end of “county-weekend “ in your other entries.

On line 38 of your XML… <event handle="county-weekend">County Weekend</event>

On line 96 of your XML… <event handle="county-weekend ">County Weekend </event>

Notice the extra space that you have in your handle and your entry of County Weekend. All the other entries are like line 96. That’s why line 38 of your XML is the only one that is being output.

see I knew it was simple.

Copy and Paste has done that.

Thanks mate.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details