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 January 5th, 2015, 03:15 PM
Authorized User
 
Join Date: Mar 2007
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Default Test within for-each

Hi,

I use XSLT for a lot of time, but mostly of time I use it wrongly because I am basically a programmer and then I have this habit of think proceduraly.

Anyway, recently I learn this nice way of make tests within a for-each loop:

Code:
<xsl:variable name="AlphaExact">
  <xsl:for-each select="//root/Inventory/InventoryRow[control = 1][1]">
      <xsl:value-of select="substring(InventoryNumber,10,1)" />
  </xsl:for-each>
</xsl:variable>
In this case, it sets my variable 'AlphaExact' with a proper vaue when the 'control' field in a row matches the value 1.

OK, It works fine for me but I really don't know exactly what the [1] does.

Is this the number of times that my test should match then if I replace it with [2] it should have at least two rows matching 1 before to load the var 'AlphaExact' with the given value?
 
Old January 5th, 2015, 04:41 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The [1] is a filter, or predicate in XSLT language, just like the [control = 1] part (which filters the set so that only elements with a sub-element called control having a value equal to 1 are returned).

When a number appears on its own in a predicate then it is equivalent to writing [position() = X], where X is the number.

So [1] returns the first element in the set preceding it, whereas [2] would return the second one. Both would only return 1 element.

You XSLT could be rewritten as this, as you are only selecting 1 element:

Code:
<xsl:variable name="AlphaExact">
   <xsl:value-of select="substring(//root/Inventory/InventoryRow[control=1][1]/InventoryNumber,10,1)" />
</xsl:variable>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old January 5th, 2015, 06:36 PM
Authorized User
 
Join Date: Mar 2007
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hey Samjudson, thank you for replying!

I don't want to abuse, but... could you point me a good place for learning XSLT language within this nice and clean non-procedural approach? I think I am tired to struggle with it due to my wrong way of looking at Xpath.

Thank you!

:)
 
Old January 5th, 2015, 06:37 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well, Wrox have a book....
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old January 5th, 2015, 06:55 PM
Authorized User
 
Join Date: Mar 2007
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Cool, thanks, I didn't have noticed! I will take a look!

:)





Similar Threads
Thread Thread Starter Forum Replies Last Post
transition between Test, Bug, Test drodriguez BOOK: Professional Scrum with Team Foundation Server 2010 1 November 11th, 2011 05:35 AM
test test Crystal Reports 0 August 21st, 2007 01:35 PM
test flyfish SQL Server DTS 0 January 13th, 2006 08:35 PM
test jdtaylor BOOK: Access 2003 VBA Programmer's Reference 0 December 30th, 2005 12:36 PM
test pettyqiang BOOK: ASP.NET Website Programming Problem-Design-Solution 1 June 10th, 2004 02:44 AM





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