Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-30
5
Medium Priority
?
250 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 2000 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

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

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

715 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