Jeffrey Roosendaal
Jeffrey Roosendaal

Reputation: 7157

XSL sorting, but only when a certain attribute is false

I'm currently sorting a Magento-generated XML file, which looks like:

<products>
  <product>
    <productnaam>Example item 1</productnaam>
    <populariteit>27845</populariteit>
    <imagelink>http://www.example.com/image1.jpg</imagelink>
  </product>
  <product>
    <productnaam>Example item 2</productnaam>
    <populariteit>12687</populariteit>
    <imagelink>http://www.example.com/image1.jpg</imagelink>
  </product>
  <product>
    <productnaam>Example item 3</productnaam>
    <populariteit>32574</populariteit>
    <imagelink>http://www.example.com/media/catalog/productno_selection</imagelink>
  </product>
<products>

using the following block of XSL:

<xsl:template match="/">
  <xsl:apply-templates select="/products/product">
    <xsl:sort select="populariteit" order="ascending" data-type="number"/>  
  </xsl:apply-templates>
</xsl:template>

It sorts the items by popularity ("populariteit" in my XML), and with the following block of code I take the first one out of the list, so it will display the most popular item.

<xsl:template match="product">
  <xsl:if test="position()=1">
    <xsl:value-of select="productnaam"/>
    <img>
      <xsl:attribute name="src">
        <xsl:value-of select='imagelink'/>
      </xsl:attribute>
    </img>
  </xsl:if>
</xsl:template>

The problem is, however, that sometimes there is no valid picture, in that case, the <imagelink>-attribute is always:

<imagelink>http://www.example.com/media/catalog/productno_selection</imagelink>

What I want is to sort the list, the way I do now, but it should skip all items where the <imagelink> is equal to what is shown above.

I've tried things like:

<xsl:sort select="populariteit" order="ascending" data-type="number" test="not(imagelink = 'http://www.fietspunt.nl/media/catalog/productno_selection')">

But that doesn't seem to work.

In the example above, 'Example item 3' is the most popular, but, since it has a faulty <imagelink>-attribute, 'Example item 1' is the one that needs to be shown.

What changes to my block of sort-code do I need to make, to make it work?

Upvotes: 1

Views: 107

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243479

Use:

  <xsl:apply-templates select=
    "/products/product
      [not(imagelink 
          = 
          'http://www.fietspunt.nl/media/catalog/productno_selection'
           )
      ]">
    <xsl:sort select="populariteit" order="ascending" data-type="number"/>  
  </xsl:apply-templates>

Upvotes: 2

Related Questions