Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 18th, 2007, 11:14 AM
Authorized User
 
Join Date: May 2007
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.
 
Old May 18th, 2007, 11:21 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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
 
Old May 18th, 2007, 11:33 AM
Authorized User
 
Join Date: May 2007
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.

 
Old May 18th, 2007, 12:02 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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
 
Old May 18th, 2007, 01:16 PM
Authorized User
 
Join Date: May 2007
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you, that was very helpful.

 
Old May 18th, 2007, 03:20 PM
Authorized User
 
Join Date: May 2007
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>



 
Old May 18th, 2007, 04:19 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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





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





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