Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 16th, 2004, 08:42 AM
Authorized User
 
Join Date: Oct 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Recursive template to split up string and extract

As part of my solution to the problem I posted a few weeks ago (http://p2p.wrox.com/topic.asp?TOPIC_ID=22952) about compressing XML, I am condensing all the attribute values in a node into one long delimeted string.

e.g.

<node att1="23" att2="567" att3="7778" att4="999"/>

becomes

<node ats="23|567|7778|999|"/>

(if you have a large amount of attributes with long descriptive names then this does become worthwhile!)

Anyway, after the XML has been posted, I need to get it back into its original form. I need to be able to extract a particular value from the string.

I need to have a template which would insert the required value, in this case item 0 which has a value of 23

<xsl:attribute name="att1"><xsl:call-template name="Extractvalue"><xsl:with-param name="ItemNo" select="0"/><xsl:with-param name="Ats" select="@ats"/></xsl:call-template></xsl:attribute>

I've got this far, which does return the value of the first item

<xsl:template name="ExtractValue">
  <xsl:param name="ItemNo"/>
  <xsl:param name="Ats"/>
  <xsl:variable name="Item" select="substring-before($Ats,'|')"/>
  <xsl:value-of select="$Item"/>
</xsl:template>

I know that to get subsequent values I need to use a recursive template which accpets the remainder of the string and returns the first value, repeated x number of times till you get to the value that you want.

e.g. to get the thrid value

remove first value, pass in rest of string
remove second value, pass in rest of string
remove third value and return it

I just can't get my head around recursive templates enough to be able to do this! Any help will be appreciated.

Thanks
 
Old December 16th, 2004, 08:58 AM
Authorized User
 
Join Date: Oct 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually just writing this post has made the idea clearer...

It looks likr this now

<xsl:template name="ExtractValue">
        <xsl:param name="ItemNo"/>
        <xsl:param name="Ats"/>

        <xsl:choose>
        <xsl:when test="$ItemNo=0">
            <xsl:value-of select="substring-before($Ats,'|')"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:call-template name="">
                <xsl:with-param name="ItemNo" select="$ItemNo-1"/>
                <xsl:with-param name="Ats" select="substring-after($Ats,'|')"/>
            </xsl:call-template>
        </xsl:otherwise>
        </xsl:choose>

    </xsl:template>

but it doesn't work...
 
Old December 16th, 2004, 09:04 AM
Authorized User
 
Join Date: Oct 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

a few syntax errors corrected and it works fine!

<xsl:template name="ExtractValue">
        <xsl:param name="ItemNo"/>
        <xsl:param name="Ats"/>

        <xsl:choose>
        <xsl:when test="$ItemNo = 0">
            <xsl:value-of select="substring-before($Ats,'|')"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:call-template name="ExtractValue">
                <xsl:with-param name="ItemNo" select="$ItemNo - 1"/>
                <xsl:with-param name="Ats" select="substring-after($Ats,'|')"/>
            </xsl:call-template>
        </xsl:otherwise>
        </xsl:choose>

    </xsl:template>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in Recursive applying template ismomo XSLT 3 December 29th, 2007 11:35 PM
Recursive Template Help bucky483 XSLT 1 July 20th, 2007 12:21 PM
Recursive template - Please Help! Tre XSLT 5 March 20th, 2007 11:23 AM
transform in a loop vs recursive template ramarc XSLT 3 April 10th, 2006 04:40 PM
Using a recursive template for various nodes spencer.clark XSLT 5 August 2nd, 2005 03:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.