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 November 24th, 2008, 01:15 PM
Authorized User
 
Join Date: May 2007
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default XPath 2.0 every expression

Hi,

I am working with the following XML input:

<blocks>
<block>
<start>2008-11-21T00:00:00-06:00</start>
<end>2008-11-21T05:00:00-06:00</end>
<flag>true</flag>
</block>
<block>
<start>2008-11-21T05:00:00-06:00</start>
<end>2008-11-21T010:00:00-06:00</end>
<flag>false</flag>
</block>
<block>
<start>2008-11-21T14:00:00-06:00</start>
<end>2008-11-21T20:00:00-06:00</end>
<flag>true</flag>
</block>
</blocks>

I am trying to write an XPATH expression that would build a comma delimited list of start,end values for every block that contains <flag>true</flag>. For example, the above XML would return:

outputvalue=0,5;14,20

To do this, I thought I could use the 2.0 every expression

every $block in descendant::block satisfies $block/MultiHourBlock/text() eq 'true'

with the hour-from-dateTime(start) function to generate this comma delimited list, but I am getting stuck on how to put those two expressions together to produce the output.

I am using saxon8.

Any help is greatly appreciated.
 
Old November 24th, 2008, 02:11 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You don't want "every" here - that tests whether every item in a sequence satisfies some condition, returning true if they do.

You want the string-join function:

string-join(block[flag='true']/string-join((start,end)/hours-from-dateTime(xs:dateTime(.)),','),';')

or if you find that too cryptic

string-join(
  for $b in block[flag='true']
  return string-join(
     for $t in (start,end)
     return hours-from-dateTime(xs:dateTime(.))
     , ',')
  , ';')


Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old November 24th, 2008, 02:12 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry the expanded one should be

string-join(
  for $b in block[flag='true']
  return string-join(
     for $t in $b/(start,end)
     return hours-from-dateTime(xs:dateTime($t))
     , ',')
  , ';')


Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old November 24th, 2008, 03:21 PM
Authorized User
 
Join Date: May 2007
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the direction. I implemented the code, but am receiving an error message:

"Required item type of first argument of string-join() is xs:string; supplied value has item type xs:integer."

Is the hours-from-dateTime() returning an integer? I thought it returned a string.

I am using saxon8 in Oxygen.
 
Old November 24th, 2008, 03:28 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You need to convert the result of hours-from-dateTime to a string before you can use string-join(). concat() automatically converts to a string, but string-join() doesn't. Sorry!

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old November 24th, 2008, 03:32 PM
Authorized User
 
Join Date: May 2007
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks! Works like a charm. And as always, much more elegant than my solution.





Similar Threads
Thread Thread Starter Forum Replies Last Post
xpath-nested expression vinod.kumbar RSS and Atom 0 July 8th, 2008 11:36 AM
XPath Expression NEO1976 XSLT 11 August 2nd, 2006 02:28 AM
Need help with XPATH Expression benomathew XSLT 1 May 12th, 2006 04:41 AM
Need help with XPATH Expression benomathew XML 1 May 9th, 2006 03:38 PM
Logical XPath Expression rohit72 XML 0 June 23rd, 2004 11:29 AM





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