XSL apply more than one template

2019-07-11 02:53发布

问题:

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() &gt; $start and position() &lt; $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() &gt; $start and position() &lt; $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>

回答1:

Read about modes in XSLT.

Then use in these two cases:

<xsl:apply-templates mode="search" select="someExpression">
 <!-- <xsl:with-param> children if necessary -->
 <!-- <xsl:sort> children if necessary -->
</xsl:apply-templates>

and also:

<xsl:apply-templates mode="paging" select="someExpression">
 <!-- <xsl:with-param> children if necessary -->
 <!-- <xsl:sort> children if necessary -->
</xsl:apply-templates>

Of course, you must have tempaltes in each of the above modes.



回答2:

I think this is no ways to apply different templates consequentially in single transformation. So, try to add paging xpath to search xpath:

<xsl:when test="contains(
             translate(title, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
                'abcdefghijklmnopqrstuvwxyz'), $keyword) 
                or contains(translate(content, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
                'abcdefghijklmnopqrstuvwxyz'), $keyword)
                and
                position() &gt; $start and position() &lt; $end">


回答3:

This feels to me like a case where you should split the transformation into two phases, one to do searching and one to do paging.

You can either writing a pipeline that executes two transformations written as separate stylesheets, or (with a bit of help from exslt:node-set()) you can do it in a single stylesheet saving the results in a temporary variable. I'd recommend two stylesheets - it makes the code more readable and reusable.



标签: xslt