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 April 4th, 2007, 12:07 AM
Registered User
 
Join Date: Apr 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Working on Doc Array inside another Doc Array?

Hi gurus, I am a beginner on XSLT. Appreciate if you can shed some light regarding the behavior of for-each & if dealing with a document array which contains another document array. See below

Example Doc Structure:
-EventMatters[1]: XML Document Array
   /EventMattersResult[1] : a XML document Array inside EventMatters
   /EventMattersResult[2]
-EventMatters[2]
   /EventMattersResult[1]

The code :
1) <xsl:for-each select="EventMatters/EventMattersResult">
2) <xsl:if test="EventMatters/EventMattersResult/Result = $VARIABLE">

My Question :
1) Hows does the xslt for-each works in this case? Does it 1st loop EventMatters, then loop on EvenMattersResult and so on? or does it only takes the 1st index of EventMatters and only loop the EventMattersResult?

2) Does the same doc rule for-each applies to the if condition?

Thank you
 
Old April 4th, 2007, 01:53 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

xsl:for-each does not loop, it applies its instructions to each node in the selected set. I'd find it easier to answer if you showed some real XML.

--

Joe (Microsoft MVP - XML)
 
Old April 4th, 2007, 02:37 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're inventing your own terminology "document array" to explain your data model - it would make for better communication if your learnt the standard vocabulary. XML does not have arrays.

The code

<xsl:for-each select="EventMatters/EventMattersResult">
   ...
</xsl:for-each>

is equivalent to a nested loop:

<xsl:for-each select="EventMatters">
  <xsl:for-each select="EventMattersResult">

The expression EventMatters/EventMattersResult returns a node-set (or in 2.0, a node sequence) containing all the EventMattersResult children of all the EventMatters children of the context node.

In the expression

<xsl:if test="EventMatters/EventMattersResult/Result = $VARIABLE">

once again EventMatters/EventMattersResult/Result selects a node-set. The semantics of "=" in XPath, when one of the operands is a node-set, are that the operator returns true if any item on the lhs is equal to any item on the rhs; so (assuming that $VARIABLE holds a string) you are testing whether any of the nodes in the node-set is equal to the variable.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 4th, 2007, 04:14 AM
Registered User
 
Join Date: Apr 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much joe & michael for your feedback.
I do have one more question assuming the following code :-

<xsl:variable name="LocnCode" select="substring(EventMatters/EventMatterResults/Order,1,2)"/>

I understand that this code performs a substring of variable Order and assigns it to variable name = LocnCode, however I am confused that Order is part of EventMatter/EventMatterResults which is a node set, so does this means that LocnCode will contain data of substring in each node in EventMatter/EventMatterResults?
 
Old April 4th, 2007, 04:57 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XPath 2.0 it is an error to pass a node-set containing more than one node as an argument to functions like substring().

In XPath 1.0 the node-set is converted to a string by taking the string value of the first node in the node-set and ignoring the other nodes. This is probably not what you want, which is why it has been outlawed in 2.0 (unless you run in backwards compability mode).

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 4th, 2007, 05:56 AM
Registered User
 
Join Date: Apr 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much Michael, your info really help me. I undertstand the concept now. :D





Similar Threads
Thread Thread Starter Forum Replies Last Post
Go from 2d Array to 1d array without defining type OneQuestion General .NET 1 January 10th, 2008 11:13 AM
Working with array-type elements tclotworthy XSLT 2 October 11th, 2007 09:27 AM
Copy text from 1 doc to other doc thru vba in word itchock Word VBA 2 December 29th, 2006 02:47 AM
Pass variables from array inside a loop dustygn PHP How-To 2 May 3rd, 2006 05:35 PM
Working with an Array stu9820 ASP.NET 1.0 and 1.1 Basics 14 October 20th, 2003 01:22 PM





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