My website makes heavy use of external SQL databases, so I need a way to limit the records returned from the various tables.
Is there a simple way to limit SQL records with XSLT? I have successfully connected to the databases, but when I access the information, I get everything in the specified columns. I am trying to limit the records to 16.
I have accomplished this in classic ASP when originally constructing the website, but I need to access it with XSLT instead for this Umbraco project. The classic ASP example is at http://briercrest.ca/redesign2/college/ (the news slider below the banner images).
I was attempting to limit the results with <xsl:iftest="position() <= 16"/>, but I don't think I'm on the right track.
That's actually the way you'd do it - if your data is sorted the way it should be, you can ask for just the first 16 by adding a predicate to the selection:
<!-- Grab the movies -->
<xsl:variable name="data" select="document('http://mydata.com/movies/')" />
<!-- Process the first 16 -->
<xsl:apply-templates select="$data/movies/movie[position() <= 16]" />
<!-- Template for a single movie -->
<xsl:template match="movie">
<p>
<xsl:value-of select="title" />
</p>
</xsl:template>
But if you need to sort them first, you'll need to grab everything and use a for-each with the nested sort(s) and then check the position() value:
<!-- Grab the movies -->
<xsl:variable name="data" select="document('http://mydata.com/movies/')" />
<!-- Sort by releaseDate DESC and take the top 16 -->
<xsl:for-each select="$data/movies/movie">
<xsl:sort select="releaseDate" data-type="text" order="descending" />
<xsl:if test="position() <= 16">
<xsl:apply-templates select="." />
</xsl:if>
</xsl:for-each>
<!-- Template for a single movie -->
<xsl:template match="movie">
<p>
<xsl:value-of select="title" />
<xsl:apply-templates select="releaseDate" mode="date" />
</p>
</xsl:template>
Limit SQL records with XSLT
My website makes heavy use of external SQL databases, so I need a way to limit the records returned from the various tables.
Is there a simple way to limit SQL records with XSLT? I have successfully connected to the databases, but when I access the information, I get everything in the specified columns. I am trying to limit the records to 16.
I have accomplished this in classic ASP when originally constructing the website, but I need to access it with XSLT instead for this Umbraco project. The classic ASP example is at http://briercrest.ca/redesign2/college/ (the news slider below the banner images).
I was attempting to limit the results with <xsl:if test="position() <= 16" />, but I don't think I'm on the right track.
Thanks,
Luke
I found the solution! For anyone interested, here is the entire XSLT file:
Hi Luke,
That's actually the way you'd do it - if your data is sorted the way it should be, you can ask for just the first 16 by adding a predicate to the selection:
But if you need to sort them first, you'll need to grab everything and use a for-each with the nested sort(s) and then check the position() value:
/Chriztian
OK, if you're actually querying a SQL database, I'd say it makes much more sense to just do the SELECT TOP 16 ... ORDER BY stuff in there...
/Chriztian
Where would I make a regular SQL query (SELECT TOP 16 ... ORDER BY)? In the XSLT file? I didn't think the two could mix (but I'm no expert...)
Hi Luke,
As far as I can see, you're using an extension function - SQL:GetDataSet() - which seems to take an SQL statement, right?
(The line that sets the $newsItem variable)
/Chriztian
is working on a reply...