?
Solved

Delmitting / Splitting Attribute Values in XSLT

Posted on 2003-03-14
2
Medium Priority
?
194 Views
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:

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

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?

thanks



 
0
Comment
Question by:kentusha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
aaronchan earned 500 total points
ID: 8137780
Hi Kentusha,

You can accomplish this using recursion:

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

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

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

</xsl:stylesheet>


The output would look like this:
8
10
12

Hope this helps.  Good luck!
0
 

Author Comment

by:kentusha
ID: 8150705
Thanks! That worked a treat.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

762 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