Dymond
Dymond

Reputation: 2277

How can i NOT print a value in a node using not[arg] with xslt?

I have a table with four columns, of which one is <type>. I want to sort my values and NOT print the rows wher type is 1. This is my table:

<table border="1">
<tr bgcolor="#ccc">
<th>Namn</th>
<th>Modell</th>
<th>Pris</th>
<th>Typ</th>

</tr>

These are my commands for sorting:

<xsl:for-each select="//ad">

<xsl:sort select="type" order="descending" />
<xsl:sort select="name"/>
<xsl:sort select="model"/>

I tried to use this value-of select, and was able to print only type 2, but I am not sure where to add the line. -<xsl:apply-templates select="type[not(. = '1')]"> SO, my question is where to add it inside the for each-loop, and if it should be open or closed. Any pointers are very welcome!

Upvotes: 1

Views: 51

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243579

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
  <table border="1">
    <tr bgcolor="#ccc">
     <th>Namn</th>
     <th>Modell</th>
     <th>Pris</th>
     <th>Typ</th>
    </tr>
   <xsl:apply-templates select="ad[not(type=1)]">
    <xsl:sort select="type" data-type="number"
              order="descending" />
    <xsl:sort select="name"/>
    <xsl:sort select="model"/>
   </xsl:apply-templates>
  </table>
 </xsl:template>

 <xsl:template match="ad">
  <tr><xsl:apply-templates/></tr>
 </xsl:template>

 <xsl:template match="ad/*">
  <td><xsl:value-of select="."/></td>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<t>
 <ad>
   <name>N1</name>
   <model>M1</model>
   <price>10</price>
   <type>2</type>
 </ad>
 <ad>
   <name>N2</name>
   <model>M2</model>
   <price>20</price>
   <type>1</type>
 </ad>
 <ad>
   <name>N3</name>
   <model>M3</model>
   <price>30</price>
   <type>1</type>
 </ad>
 <ad>
   <name>N4</name>
   <model>M4</model>
   <price>40</price>
   <type>3</type>
 </ad>
</t>

produces the wanted, correct result:

<table border="1">
   <tr bgcolor="#ccc">
      <td>Namn</th>
      <th>Modell</th>
      <th>Pris</th>
      <th>Typ</th>
   </tr>
   <tr>
      <td>N4</td>
      <td>M4</td>
      <td>40</td>
      <td>3</td>
   </tr>
   <tr>
      <td>N1</td>
      <td>M1</td>
      <td>10</td>
      <td>2</td>
   </tr>
</table>

Upvotes: 2

lavinio
lavinio

Reputation: 24319

Without seeing your input and more of your program, it's hard to tell, but have you tried this?

<xsl:for-each select="//ad[type != '1']">
  <xsl:sort select="type" order="descending" />
  <xsl:sort select="name"/>
  <xsl:sort select="model"/>

Upvotes: 2

Related Questions