Solved

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

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
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 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)

809 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