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

Re: XSL transformation


xsl substring
On 19 Nov 2002 at 16:53, Nischal Muthana wrote:

> I have an XSL file say NameList.xsl which picks
> transforms an XML file say Names.xml. 
> 
> The Names.xml is like,
> 
> <Name>
>   <Names>aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii
> jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt
> uuuu vvvv wwww xxxx yyyy zzzz</FirstNames>
> </Name>

This non-well-formed XML isn't your real code, is it?
(Names != FirstNames)

> My Xsl file has to pick up the Names node and do a
> substring and pick each value in the <Names> tag. Each
> value is of 4 characters and each value is seperated
> by a space. 
> 
> I am tryin to write a <xsl:foreach
> select='Name/Names'> 
> and then do a substring on the element value. Can
> someone guide me through how to go about with this. 

Generally the best way to handle problems like this is a functional 
programming technique known as tail-recursion. The basic idea is that
your function does some operation on the first item in the input and
then calls itself with the remaining (unprocessed) input as an 
argument.

Let's say you want each of those four-character names to end up as an
HTML list item. Here's a pseudo-code example of how to do that with 
tail-recursion.

  function namesToListItems(names) {
      # assume splitString() is a function that returns a pair
      # of the substring before the first space and the substring
      # after the first space
      firstName = firstOf(splitString(names));
      remainingNames = restOf(splitString(names));
      result = makeListItem(firstName) + \
          namesToListItems(remainingNames);
      }

And here's how it looks in XSLT:

  <xsl:template name="namesToListItems">
    <xsl:param name="names"/>
    <LI>
      <xsl:value-of select="substring-before($names,' ')"/>
    </LI>
    <xsl:call-template name="namesToListItems">
      <xsl:with-param name="names"
        select="substring-after($names,' ')"/>
    </xsl:call-template>
  </xsl:template>

> <xsl:value-of select="substring(Name/Names,0,4)"/>
> <xsl:value-of select="substring(Name/Names,4,4)"/>

Well, you can do that. But of course the problem is that you have to 
know exactly how many names there are in every instance.

By the way, if you find yourself having a lot of XSLT questions, XSL-
List is the best place to ask. Visit www.mulberrytech.com for more 
info.

Hope this helps.
 
--
Matt Gushee
Englewood, CO USA

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
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

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.