Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
| FAQ | Members List | Calendar | 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
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 16th, 2006, 05:18 AM
Authorized User
Join Date: Oct 2005
Location: , , Philippines.
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default traversing a tree based on an array of strings

i want to traverse an xml tree using a text file which has values equivalent to the nodes found in the tree. but when i place the this code:
<xsl:param name="file"/>
<xsl:output method="text" omit-xml-declaration="yes" use-character-maps="charmap" encoding="ascii" indent="no"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="elements">
<xsl:analyze-string select="unparsed-text($file,'utf-8')" regex="[\r\n]">
<xsl:value-of select="."/>
<xsl:template match="livre">
<xsl:apply-templates select="ident"/>

<xsl:template match="ident">
<xsl:variable name="node" select="."/>
<xsl:for-each select="$elements">
<xsl:apply-templates select="root($node)/ident/."/>
it outputs the value of the strings. i know that the node-set to be traversed will change upon the use if the xsl:for-each, but is there a way i can do it? thanks. =D

Reply With Quote
  #2 (permalink)  
Old February 16th, 2006, 09:54 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: 100%
Activity: 100% Activity: 100% Activity: 100%
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts

Firstly, this structure:

<xsl:variable name="elements">
  <xsl:analyze-string select="unparsed-text($file,'utf-8')" regex="[\r\n]">
      <xsl:value-of select="."/>

Goves you a variable whose value is a document node containing the concatenated values of the string. You want to keep the strings separate, so use:

<xsl:variable name="elements" as="xs:string*">
  <xsl:analyze-string select="unparsed-text($file,'utf-8')" regex="[\r\n]">
      <xsl:sequence select="."/>

or more simply

<xsl:variable name="elements" select="tokenize(unparsed-text($file,'utf-8'), '[\r\n]')"/>

Secondly in this,

<xsl:apply-templates select="root($node)/ident/."/>

you're think that if "." is then string "fred", then the expression is equivalent to

<xsl:apply-templates select="root($node)/ident/fred"/>

which isn't the case for two reasons: firstly, the value of "." changes on the rhs of "/", and secondly, you can't just substitute a value into an expression and have it treated as part of the expression before evaluating the expression - XSLT is an expression language, not a macro language. You want

<xsl:apply-templates select="root($node)/ident/*[name()=current()]"/>

Michael Kay
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote

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
strings as array indexes cwalton Excel VBA 3 July 30th, 2007 02:50 AM
Tree Menu Based on Directories red_fiesta Classic ASP Professional 4 November 1st, 2006 08:42 AM
Array of Strings jjd Visual C++ 1 June 23rd, 2005 02:56 PM
looping tree based on identical value of a child Kabe Classic ASP XML 0 March 17th, 2004 05:57 AM
array to tree Baldo XSLT 2 November 7th, 2003 01:49 PM

All times are GMT -4. The time now is 12:02 PM.

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