Solved

Please help me to get substrings from this string and assign them to variables

Posted on 2008-10-30
5
242 Views
Last Modified: 2013-11-18
I need to get substrings from this string and assign them to variables

Here is an example of the string i will be getting from the xml:
John Smith Hrs. 50 Billing Rate 100.00 For Weekly Billing Report Dated 08/20/2008

So the format is always:
[name] Hrs. [amount of hours] Billing Rate [rate] For Weekly Billing Report Dated [date]

The text in quotes will always be consistent.  The text in brackets will be different names/dates/etc.. each time.

I need the text that will be in the brackets to be assigned to variables so I can display them seperately.


0
Comment
Question by:CarltonBanks
[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
  • 3
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 22846300
I wrapped the string in a test element
<xsl:template match="test">
    <xsl:variable name="name" select="normalize-space(substring-before(., 'Hrs.'))"></xsl:variable>
    <xsl:variable name="hours" select="normalize-space(substring-before(substring-after(., 'Hrs.'), 'Billing Rate'))"></xsl:variable>
    <xsl:variable name="rate" select="normalize-space(substring-before(substring-after(., 'Billing Rate'), 'For Weekly'))"></xsl:variable>
    <xsl:variable name="date" select="normalize-space(substring-after(., 'Dated'))"></xsl:variable>
</xsl:template>

Open in new window

0
 

Author Comment

by:CarltonBanks
ID: 22846478
This looks really good but how can I call these variables from a seperate template?
0
 

Author Comment

by:CarltonBanks
ID: 22846737
nvm I figured it out.  Thanks!
0
 

Author Closing Comment

by:CarltonBanks
ID: 31511896
you rock!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22848153
you are welcome

sorry, I went to bed before you posted your follow-up.

I know you figured it out yourself, but just in order to have the information in this reference...

There are basically two options

1. make the variables global (at the top level of your stylesheet). This way, they are accessible everywhere in the stylesheet
at the top level you would have something like this
    <xsl:variable name="name" select="normalize-space(substring-before(//test, 'Hrs.'))"></xsl:variable>
You would use this option if you only have on set of variables from your source XML

2. have the variables locally to the template and pass them to deeper level parameters
<xsl:template match="test">
    <xsl:variable name="name" select="normalize-space(substring-before(., 'Hrs.'))"></xsl:variable>
    <xsl:apply-templates select="myChild">
       <xsl:with-param name="name" select="$name"/>
    </xsl:apply-templates>
</xsl:template>

And pick it up and use it like this
<xsl:template match="myChild">
    <xsl:param name="name"/>
    <xsl:value-of select="$name"/>
</xsl:template>
You would use this option if your source XML contains more than one set of these variables and you only use them in the underlying tree

If you found another option working for yourself, it would be nice that you would post it here
After all, your follow question was an interesting one

cheers

Geert
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

623 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