Solved

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

Posted on 2008-10-30
5
203 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
XSLT 5 37
How to make similar contact form? 8 59
ColdFusion Rereplace 3 62
jQuery Date picker not working 16 78
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…
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 …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now