Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Delmitting / Splitting Attribute Values in XSLT

Posted on 2003-03-14
Medium Priority
Last Modified: 2012-08-13
I need to split an attribute value and I need to do this in a loop.

For example

my xml file:

<att1 val="8/10/12"> </att1>

I need to iterate over val and split into 8,10,12 or say 8,10,12,14 or 10,12,14,16. I cannot change the xml to something more sensible like child nodes for each value.

I can think of an algorithmn with

string = substring-before(string, substring)
string = substring-after(string, substring)

I ask for the string before and after, so the first time I get 8 & 10/12. Then I do it again and I get 10 & 12.
Voila!, but I need to do it in a loop (so I can deal with 8/10/12/16/18, and the only looping I can find is for-each! does any one clever have any ideas?


Question by:kentusha

Accepted Solution

aaronchan earned 500 total points
ID: 8137780
Hi Kentusha,

You can accomplish this using recursion:

To demonstrate it, I use your XML source:
  <att1 val="8/10/12"/>

Here is the XSL:
1. Main template:
<xsl:template match="/">
  <xsl:call-template name="returnStringBefore">
    <xsl:with-param name="myString" select="//att1/@val"/>

2. Template you call recursively:
<xsl:template name="returnStringBefore">
  <xsl:param name="myString"/>
      <xsl:when test="substring-after($myString, '/') != ''">
        <xsl:value-of select="substring-before($myString, '/')"/>    
        <xsl:call-template name="returnStringBefore">
          <xsl:with-param name="myString" select="substring-after($myString, '/')"/>
        <xsl:value-of select="$myString" />


The output would look like this:

Hope this helps.  Good luck!

Author Comment

ID: 8150705
Thanks! That worked a treat.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Integration Management Part 2
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question