Note that there are some explanatory texts on larger screens.

plurals
  1. POXSL apply more than one template
    text
    copied!<p>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.</p> <p><strong>Paging</strong></p> <pre><code>... &lt;xsl:if test="position() &amp;gt; $start and position() &amp;lt; $end"&gt; ... </code></pre> <p><strong>Search.xsl</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output omit-xml-declaration="yes" method="xml" version="1.0" encoding="UTF-8"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:template match="/*"&gt; &lt;items&gt; &lt;xsl:attribute name="count"&gt;&lt;xsl:value-of select="count(//item)"/&gt;&lt;/xsl:attribute&gt; &lt;xsl:apply-templates select="//item"&gt; &lt;xsl:sort select="*[name()=$sortBy]" order="{$order}" data-type="{$type}" /&gt; &lt;/xsl:apply-templates&gt; &lt;/items&gt; &lt;/xsl:template&gt; &lt;xsl:template match="//item"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="contains( translate(title, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), $keyword) or contains(translate(content, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), $keyword)"&gt; &lt;item&gt; &lt;title&gt;&lt;xsl:value-of select="title"/&gt;&lt;/title&gt; &lt;content&gt;&lt;xsl:value-of select="content"/&gt;&lt;/content&gt; &lt;date&gt;&lt;xsl:value-of select="date"/&gt;&lt;/date&gt; &lt;author&gt;&lt;xsl:value-of select="author"/&gt;&lt;/author&gt; &lt;uri&gt;&lt;xsl:value-of select="uri"/&gt;&lt;/uri&gt; &lt;division&gt;&lt;xsl:value-of select="division"/&gt;&lt;/division&gt; &lt;/item&gt; &lt;/xsl:when&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>Final Solution using xsl-variable and node-set()</strong></p> <p>Need to do some more checks, but i'm pretty sure this works ok.</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common"&gt; &lt;xsl:output omit-xml-declaration="yes" method="xml" version="1.0" encoding="UTF-8"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:variable name="searchResults"&gt; &lt;xsl:apply-templates select="//item"&gt; &lt;xsl:sort select="*[name()=$sortBy]" order="{$order}" data-type="{$type}" /&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:variable&gt; &lt;xsl:template match="//item"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="contains(translate(title, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), $keyword) or contains(translate(content, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), $keyword)"&gt; &lt;item&gt; &lt;title&gt;&lt;xsl:value-of select="title"/&gt;&lt;/title&gt; &lt;content&gt;&lt;xsl:value-of select="content"/&gt;&lt;/content&gt; &lt;date&gt;&lt;xsl:value-of select="date"/&gt;&lt;/date&gt; &lt;author&gt;&lt;xsl:value-of select="author"/&gt;&lt;/author&gt; &lt;uri&gt;&lt;xsl:value-of select="uri"/&gt;&lt;/uri&gt; &lt;division&gt;&lt;xsl:value-of select="division"/&gt;&lt;/division&gt; &lt;/item&gt; &lt;/xsl:when&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; &lt;xsl:template match="//item" mode="paging"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="position() &amp;gt; $start and position() &amp;lt; $end"&gt; &lt;item&gt; &lt;title&gt;&lt;xsl:value-of select="title"/&gt;&lt;/title&gt; &lt;content&gt;&lt;xsl:value-of select="content"/&gt;&lt;/content&gt; &lt;date&gt;&lt;xsl:value-of select="date"/&gt;&lt;/date&gt; &lt;author&gt;&lt;xsl:value-of select="author"/&gt;&lt;/author&gt; &lt;uri&gt;&lt;xsl:value-of select="uri"/&gt;&lt;/uri&gt; &lt;division&gt;&lt;xsl:value-of select="division"/&gt;&lt;/division&gt; &lt;/item&gt; &lt;/xsl:when&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/*"&gt; &lt;items&gt; &lt;xsl:attribute name="count"&gt;&lt;xsl:value-of select="count(//item)"/&gt;&lt;/xsl:attribute&gt; &lt;xsl:apply-templates select="exslt:node-set($searchResults)/*" mode="paging" /&gt; &lt;/items&gt; &lt;/xsl:template&gt; </code></pre> <p></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload