Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 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 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

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: 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

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

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

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