Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 18th, 2007, 11:14 AM
Authorized User
 
Join Date: May 2007
Location: , , .
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default Looping within a string

Considering the following node:

<cell>Project\Application $id_number$ $description$\$folder$ $dates$</cell>

I am trying to expand each $expression$ which are defined later in the XML input and then capture the full path.

<expression>
      <cell name="naming convention">id_number</cell>
      <cell name="format">[some regex expression]</cell>
</expression>

I know I can use

<xsl:value-of select="substring-before(substring-after($path, '$'), '$')" />

to isolate one expression, but is there any way to loop through a string so for each $expression$ perform a lookup and substitution?

Any help is greatly appreciated.
Reply With Quote
  #2 (permalink)  
Old May 18th, 2007, 11:21 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This kind of thing becomes very easy in XSLT 2.0 with xsl:analyze-string.

If you're really stuck with 1.0, take a look at the string replacement templates in www.exslt.org which show how to iterate over a string using recursive named templates.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old May 18th, 2007, 11:33 AM
Authorized User
 
Join Date: May 2007
Location: , , .
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Was just reading through your book and have the page opened to xsl:analyze-string, which has been tremendously helpful.

Thankfully, I am able to use 2.0. My plan was to use xsl:analyze-string once I had expanded the $expressions$. But you are stating that I can use it to actually identify and expand each $expression$ as well? If you have any examples implementing this technique, I would appreciate the context.

Reply With Quote
  #4 (permalink)  
Old May 18th, 2007, 12:02 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm not exactly sure what you want to do, but you can start by

<xsl:key name="exp" match="expression" use="cell[1]/name"/>

then
  <xsl:variable name="cell" select="cell"/>
  <xsl:analyze-string select="$cell" regex="\$(.*?)\$">
    <xsl:matching-substring>
      <xsl:variable name="exp" select="key('exp', regex-group(1), $cell)"/>
       ... $exp is now the selected <expression> element ...
    </xsl:matching-substring>

Basically the <matching-substring> will get executed once for each $xyz$ variable in your string.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #5 (permalink)  
Old May 18th, 2007, 01:16 PM
Authorized User
 
Join Date: May 2007
Location: , , .
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you, that was very helpful.

Reply With Quote
  #6 (permalink)  
Old May 18th, 2007, 03:20 PM
Authorized User
 
Join Date: May 2007
Location: , , .
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One follow up question, using the following code produces an error. I might be missing something.

XML:
<root>
<cell>Project\Application $id_number$ $description$\$folder$</cell>
<expression>
      <cell name="naming convention">id_number</cell>
      <cell name="format">[some regex expression]</cell>
</expression>
<expression>
      <cell name="naming convention">description</cell>
      <cell name="format">[some regex expression]</cell>
</expression>
<expression>
      <cell name="naming convention">folder</cell>
      <cell name="format">[some regex expression]</cell>
</expression>
</root>

The XSLT (right now just trying to get some sore of result:

<xsl:key name="exp" match="/root/expression" use="cell[1]/@name"/>

<xsl:template match="/root">
  <xsl:variable name="cell" select="cell"/>
  <xsl:analyze-string select="$cell" regex="\$(.*?)\$">
    <xsl:matching-substring>
      <xsl:variable name="exp" select="key('exp', regex-group(1), $cell)"/>
      <xsl:value-of select="$exp" />
    </xsl:matching-substring>
  </xsl:analyze-string>
</xsl:template>



Reply With Quote
  #7 (permalink)  
Old May 18th, 2007, 04:19 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I don't get an error, I just get no output.

There were a couple of small errors in defining and using the key. The key definition should be

<xsl:key name="exp" match="/root/expression" use="cell[1]"/>

because it's the contents of the cell we're searching on, not its name. And the call on key() should be

<xsl:variable name="exp" select="key('exp', regex-group(1), root($cell))"/>

because we want to search the whole document, not just the subtree rooted at $cell.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
looping thru string havey C# 1 April 26th, 2008 05:13 PM
Looping ssaranam SQL Server 2005 2 April 17th, 2008 01:40 AM
looping mrjoka Classic ASP Basics 1 September 26th, 2007 12:21 AM
Looping deepsea007 XSLT 1 June 14th, 2007 12:13 PM
RegExp looping for multiple strings in string interrupt Javascript How-To 2 June 20th, 2005 05:37 AM



All times are GMT -4. The time now is 01:26 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.