[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] XSLT recursion problem


Subject: Re: [xsl] XSLT recursion problem
From: Eric van der Vlist <vdv@xxxxxxxxxxxx>
Date: Wed, 28 Sep 2005 10:22:28 +0200

Hi Paolo,

Le mercredi 28 septembre 2005 ` 09:46 +0200, Paolo Vaccari a icrit :

> The real problem is that I don't want to have to know anything about 
> <sometag>. That is, I would be free to use (and nest!) whatever 
> non-proprietary tag inside my <repeat>, without modifing it.
> Probably there's no way in XSLT 1.0 (if it is, why creating tunnel in 
> 2.0 ;) , but if you see another solution...

You can do that in XSLT 1.0 using modes and a node-set extension:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:ax="http://dyomedea.com" xmlns:exsl="http://exslt.org/common"
    extension-element-prefixes="exsl">
    <xsl:template match="*|@*">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="ax:repeat" name="ax:repeat">
        <xsl:param name="index">1</xsl:param>
        <xsl:variable name="rtf">
            <xsl:apply-templates mode="ax:repeat">
                <xsl:with-param name="index" select="$index"/>
            </xsl:apply-templates>
        </xsl:variable>
        <xsl:apply-templates select="exsl:node-set($rtf)/*"/>
        <xsl:if test="$index &lt; count(*/ax:items[1]/ax:item)">
            <xsl:call-template name="ax:repeat">
                <xsl:with-param name="index" select="$index + 1"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    <xsl:template match="ax:items">
        <xsl:element name="{@element}">
            <xsl:apply-templates select="ax:item[1]"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="ax:item">
        <xsl:attribute name="src">
            <xsl:value-of select="@src"/>
        </xsl:attribute>
    </xsl:template>
    <xsl:template match="*|@*" mode="ax:repeat">
        <xsl:param name="index"/>
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" mode="ax:repeat">
                <xsl:with-param name="index" select="$index"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="ax:items" mode="ax:repeat">
        <xsl:param name="index"/>
        <xsl:copy>
            <xsl:apply-templates select="@*|ax:item[$index]" mode="ax:repeat">
                <xsl:with-param name="index" select="$index"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Eric

-- 
Le premier annuaire des apiculteurs 100% XML!
                                                http://apiculteurs.info/
------------------------------------------------------------------------
Eric van der Vlist       http://xmlfr.org            http://dyomedea.com
(ISO) RELAX NG   ISBN:0-596-00421-4 http://oreilly.com/catalog/relax
(W3C) XML Schema ISBN:0-596-00252-1 http://oreilly.com/catalog/xmlschema
------------------------------------------------------------------------


Current Thread
Keywords