Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Whats Wrong with this XSLT ??

Status
Not open for further replies.

MrT2005

Programmer
Mar 8, 2005
22
CA
Im trying to display results based on a keyword which is working just fine.

The problem is that it shows the results found 3 times (the current total number of records).

I want it to just show the records I choose and then sort them.

heres the XSL I'm using:

<xsl:template match="fiches/fiche">
<xsl:for-each select="//fiche">
<xsl:sort order="descending" select="texte/fichetitre"/>
<xsl:if test="texte/*= $keyword">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="*" class="pad">
<xsl:value-of select="texte/fichetitre" />
</td>
<td width="150" align="center" class="pad"><xsl:value-of select="texte/typeheberg" /></td>
<td width="150" align="center" class="pad"><xsl:text>73</xsl:text></td>
<td width="100" align="center" class="pad"><xsl:value-of select="region/regionnseo" /></td>
</tr>
</table>
</xsl:if>
</xsl:for-each>
</xsl:template>

Anyone know how to fix this?
I think I have the code in the wrong order?

thanks!
 
Code:
<xsl:for-each select="//fiche">
This XPath says "Give me all the fiche elements in the document", which is 3, but your template is being called 3 times also, so you'll end up with 9 tables.

To correct it depends on where you are calling the template from. If it's the only template, just put match="/" instead of match="fiches/fiche". If not, you'll have to get rid template and just use for-each or get rid of the for-each and when you apply-templates, use this:
Code:
<xsl:apply-templates select="fiches/fiche">
  <xsl:sort order="descending" select="texte/fichetitre"/>
</xsl:apply-templates>
 
This is what im using:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:eek:utput method="html" encoding="ISO-8859-1" indent="yes"/>

<xsl:param name="keyword">none</xsl:param>

<xsl:template match="/">

<xsl:apply-templates/>

</xsl:template>

<xsl:template match="fiches/fiche">
<xsl:for-each select="//fiche">
<xsl:sort order="descending" select="texte/fichetitre"/>
<xsl:if test="texte/*= $keyword">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="*" class="pad">
<xsl:value-of select="texte/fichetitre" />
</td>
<td width="150" align="center" class="pad"><xsl:value-of select="texte/typeheberg" /></td>
<td width="150" align="center" class="pad"><xsl:text>73</xsl:text></td>
<td width="100" align="center" class="pad"><xsl:value-of select="region/regionnseo" /></td>
</tr>
</table>
</xsl:if>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>


if I remove the for-each, then it says
XSLT processing error: XSL element 'template' cannot contain element 'sort' at this point
 
ok here's a revised version:

with the (working) code below
how can I sort the data based on the "fichetitre" ??


<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:eek:utput method="html" encoding="ISO-8859-1" indent="yes"/>

<xsl:param name="keyword">none</xsl:param>

<xsl:template match="fiches/fiche">
<xsl:if test="texte/*= $keyword">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<xsl:attribute name="bgcolor">
<xsl:choose>
<xsl:when test="position() mod 2 = 1">#E6E4DE</xsl:when>
<xsl:eek:therwise>#FFFFFF</xsl:eek:therwise>
</xsl:choose>
</xsl:attribute>
<td width="*" class="pad">
<xsl:value-of select="texte/fichetitre" />
</td>
<td width="150" align="center" class="pad"><xsl:value-of select="texte/typeheberg" /></td>
<td width="150" align="center" class="pad"><xsl:text>73</xsl:text></td>
<td width="100" align="center" class="pad"><xsl:value-of select="region/regionnseo" /></td>
</tr>
</table>
</xsl:if>
</xsl:template>

</xsl:stylesheet>
 
In that case, you can use either of the following:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:output method="html" encoding="ISO-8859-1" indent="yes"/>
  <xsl:param name="keyword">none</xsl:param>
  <xsl:template match="/">
    <xsl:apply-templates select="//fiche">
      <xsl:sort order="descending" select="texte/fichetitre"/>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match="fiche">
    <xsl:if test="texte/*= $keyword">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="*" class="pad">
            <xsl:value-of select="texte/fichetitre"/>
          </td>
          <td width="150" align="center" class="pad">
            <xsl:value-of select="texte/typeheberg"/>
          </td>
          <td width="150" align="center" class="pad">
            <xsl:text>73</xsl:text>
          </td>
          <td width="100" align="center" class="pad">
            <xsl:value-of select="region/regionnseo"/>
          </td>
        </tr>
      </table>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
Or:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:output method="html" encoding="ISO-8859-1" indent="yes"/>
  <xsl:param name="keyword">none</xsl:param>
  <xsl:template match="/">
    <xsl:for-each select="//fiche">
      <xsl:sort order="descending" select="texte/fichetitre"/>
      <xsl:if test="texte/*= $keyword">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="*" class="pad">
              <xsl:value-of select="texte/fichetitre"/>
            </td>
            <td width="150" align="center" class="pad">
              <xsl:value-of select="texte/typeheberg"/>
            </td>
            <td width="150" align="center" class="pad">
              <xsl:text>73</xsl:text>
            </td>
            <td width="100" align="center" class="pad">
              <xsl:value-of select="region/regionnseo"/>
            </td>
          </tr>
        </table>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Bear in mind that it is bad practice to use the double backslash (//) as this will search the entire tree. Specify the nodes directly where possible (ie apply-templates select="fiches/fiche").
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top