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 December 20th, 2005, 09:40 AM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default XPath problem

I have the following XPath problem: How to select the first preceding node,
whether it is an ancestor, a preceding-sibling or a "preceding-siblings child"?
As I understand, the axis 'preceding' selects everyhting except any ancestors.
Let's say we have the following document:

<a>
  <b/>
  <c/>
  <d/>
</a>
<e>
  <f/>
  <g/>
</e>

If the context node is <f/>, preceding::*[position()=1] does
not select <e> but <d/>. And if I use the ancestor axis, it
always selects an ancestor (as it is supposed to) even if it weren't the first
preceding node (e.g. the context were <g/>). I guess the problem boils down
to figuring out whether the first ancestor is also the first preceding node,
or is there some other nodes in between.

When I was travelling "downwards" in the document, the problem was
easy to solve. So what's the catch?

 
Old December 20th, 2005, 08:18 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try

(preceding::* | ancestor::*)[last()]

Note it's [last()] rather than [1] because the result of a union is always in document order.

This expression may be expensive but it should give the right answer.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 21st, 2005, 06:14 AM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yep, that did he trick. Thank you. I'm still on the learning curve on XSLT, and a union didn't even come to mind. Thanks again...






Similar Threads
Thread Thread Starter Forum Replies Last Post
Xpath problem deean XSLT 10 June 18th, 2008 11:23 AM
Problem with xpath function Phanikumar Biztalk 1 May 22nd, 2008 02:32 AM
xpath syntax problem nrane26 XSLT 1 January 22nd, 2005 01:25 PM
XPath problem alexiev_nikolay C# 0 February 6th, 2004 03:40 AM
XPath Problem babloo81 Beginning VB 6 4 November 14th, 2003 01:28 AM





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