I'm transforming an XML document with PHP/XSL. I'm searching for a keyword value. I want to add paging, so I'm not returning all the search results. I can do this with separate xsl files, but I'd like to join them if I can. How can I return the search results and then apply the paging? E.g.
Paging
...
<xsl:if test="position() > $start and position() < $end">
...
Search.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<items>
<xsl:attribute name="count"><xsl:value-of select="count(//item)"/></xsl:attribute>
<xsl:apply-templates select="//item">
<xsl:sort select="*[name()=$sortBy]" order="{$order}" data-type="{$type}" />
</xsl:apply-templates>
</items>
</xsl:template>
<xsl:template match="//item">
<xsl:choose>
<xsl:when test="contains(
translate(title, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz'), $keyword)
or contains(translate(content, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz'), $keyword)">
<item>
<title><xsl:value-of select="title"/></title>
<content><xsl:value-of select="content"/></content>
<date><xsl:value-of select="date"/></date>
<author><xsl:value-of select="author"/></author>
<uri><xsl:value-of select="uri"/></uri>
<division><xsl:value-of select="division"/></division>
</item>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Final Solution using xsl-variable and node-set()
Need to do some more checks, but i'm pretty sure this works ok.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="searchResults">
<xsl:apply-templates select="//item">
<xsl:sort select="*[name()=$sortBy]" order="{$order}" data-type="{$type}" />
</xsl:apply-templates>
</xsl:variable>
<xsl:template match="//item">
<xsl:choose>
<xsl:when test="contains(translate(title, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), $keyword) or contains(translate(content, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), $keyword)">
<item>
<title><xsl:value-of select="title"/></title>
<content><xsl:value-of select="content"/></content>
<date><xsl:value-of select="date"/></date>
<author><xsl:value-of select="author"/></author>
<uri><xsl:value-of select="uri"/></uri>
<division><xsl:value-of select="division"/></division>
</item>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="//item" mode="paging">
<xsl:choose>
<xsl:when test="position() > $start and position() < $end">
<item>
<title><xsl:value-of select="title"/></title>
<content><xsl:value-of select="content"/></content>
<date><xsl:value-of select="date"/></date>
<author><xsl:value-of select="author"/></author>
<uri><xsl:value-of select="uri"/></uri>
<division><xsl:value-of select="division"/></division>
</item>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="/*">
<items>
<xsl:attribute name="count"><xsl:value-of select="count(//item)"/></xsl:attribute>
<xsl:apply-templates select="exslt:node-set($searchResults)/*" mode="paging" />
</items>
</xsl:template>