Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

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

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
CarltonBanks
Asked:
CarltonBanks
  • 3
  • 2
1 Solution
 
Geert BormansCommented:
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
 
CarltonBanksAuthor Commented:
This looks really good but how can I call these variables from a seperate template?
0
 
CarltonBanksAuthor Commented:
nvm I figured it out.  Thanks!
0
 
CarltonBanksAuthor Commented:
you rock!
0
 
Geert BormansCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now