[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Random indexing into a comma seperated list

Subject: RE: Random indexing into a comma seperated list
From: "Diamond, Jason" <Jason.Diamond@xxxxxxx>
Date: Thu, 3 May 2001 17:42:38 -0500
random indexing
Doesn't embedding comma-separated lists into your elements defeat the
purpose of XML?

If I had to do it, I would define a template that could "return" a specified
element in the list and call that template each time I needed to "randomly"
access an element. This is hardly efficient. If that mattered, though, you
might consider an extension function. Using MSXML3 you can define functions
that accept node-sets as parameters in which you can manipulate them using
the standard NodeList interface.

The following stylesheet does it the hard (but portable) way:

<xsl:transform
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:output method="text" encoding="UTF-8"/>

  <!-- root -->
  <xsl:template match="/">

    <xsl:call-template name="process-list">
      <xsl:with-param name="list" select="tag"/>
    </xsl:call-template>

  </xsl:template>

  <!-- recursive process-list helper -->
  <xsl:template name="process-list">
    <xsl:param name="list"/>
    <xsl:param name="current" select="1"/>
    <xsl:param name="tail" select="$list"/>

    <xsl:if test="$tail">

      <!-- process the current element -->
      <xsl:text>current = </xsl:text>
      <xsl:call-template name="get-element">
        <xsl:with-param name="list" select="$list"/>
        <xsl:with-param name="index" select="$current"/>
      </xsl:call-template>

      <!-- current - 1 -->
      <xsl:if test="$current > 1">
        <xsl:text>, current - 1 = </xsl:text>
        <xsl:call-template name="get-element">
          <xsl:with-param name="list" select="$list"/>
          <xsl:with-param name="index" select="$current - 1"/>
        </xsl:call-template>
      </xsl:if>

      <!-- current + 1 -->
      <xsl:if test="contains($tail, ',')">
        <xsl:text>, current + 1 = </xsl:text>
        <xsl:call-template name="get-element">
          <xsl:with-param name="list" select="$list"/>
          <xsl:with-param name="index" select="$current + 1"/>
        </xsl:call-template>
      </xsl:if>

      <xsl:text>&#xA;</xsl:text>

      <!-- process the next element -->
      <xsl:call-template name="process-list">
        <xsl:with-param name="list" select="$list"/>
        <xsl:with-param name="current" select="$current + 1"/>
        <xsl:with-param name="tail" select="substring-after($tail, ',')"/>
      </xsl:call-template>

    </xsl:if>

  </xsl:template>

  <!-- recursive get-element helper -->
  <xsl:template name="get-element">
    <xsl:param name="list"/>
    <xsl:param name="index"/>
    <xsl:param name="current" select="1"/>

    <xsl:choose>
      <xsl:when test="$current &lt; $index">
        <xsl:call-template name="get-element">
          <xsl:with-param name="list" select="substring-after($list, ',')"/>
          <xsl:with-param name="index" select="$index"/>
          <xsl:with-param name="current" select="$current + 1"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="contains($list, ',')">
            <xsl:value-of select="substring-before($list, ',')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$list"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>

  </xsl:template>

</xsl:transform>

Hope this helps,
Jason.

> -----Original Message-----
> From: Dan Diebolt [mailto:dandiebolt@xxxxxxxxx]
> Sent: Thursday, May 03, 2001 2:30 PM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  Random indexing into a comma seperated list
> 
> 
> I have a tag in deep in my XML that holds a comma (or space or
> semicolon ...) separated list of tokens:
> 
> <tag>1,2,$19.95,4,five</tag>
> 
> I have pulled this tag into a variable Tag:
> 
> <xsl:variable name="List" select="?path to Tag?"/>
> 
> With another node elsewhere in the hierarchy as the context
> node, I need to iterate n times indexing into $List. However
> on each iteration I may need, in various places, multiple references 
> to $List[$i], $List[$i-1] or $List[$i+1]. This is different than
> making one pass through each token in $List. How would you
> orchestrate this so the loop logic does not overwhelm the body
> logic? I am looking for general techniques which make as few 
> assumptions as possible as even this description is a simplification
> of my task. 
> 
> Regards,
> 
> Dan
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Auctions - buy the things you want at great prices
> http://auctions.yahoo.com/
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.