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 August 29th, 2006, 01:53 PM
Authorized User
 
Join Date: Jun 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Trouble with preceding sibling to a parent

Given xml like the following:

<myXML>
  <ACTUAL_1_VALUE>
      <myTAG />
  </ACTUAL_1_VALUE>
  <ACTUAL_2_VALUE>
      <myTAG />
  </ACTUAL_2_VALUE>
etc...
</myXML>

What I would like to do is when I'm processing the myTAG in ACTUAL_2_VALUE, pull the name of the previous ACTUAL_# tag.

I've done some research online, and it looks like I probably want to use the preceding-sibling, but I'm not really sure how to do that since I'm actually wanting to pull the NAME of the PARENT's preceding-sibling, not the current tag's preceding sibling.

Any help is greatly appreciated. Thanks.

Here's what I'm tried, and got the following error:

<xsl:variable name="lastParent" select="preceding-sibling::name(..)" />

NodeTest expected here. preceding-sibling::-->name<--(..)


Thanks!
__________________
Thanks!
 
Old August 29th, 2006, 02:00 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You want name(../preceding-sibling::*[1])

that is: go to the parent, then go to the first preceding sibling element, then get its name.

In XPath 2.0 you could also do

../preceding-sibling::*[1]/name()

if that seems more logical.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 29th, 2006, 02:12 PM
Authorized User
 
Join Date: Jun 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael, as always, you're a wealth of knowledge (and quick to respond)!

Thanks!
 
Old August 30th, 2006, 02:21 PM
Authorized User
 
Join Date: Jun 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One follow up on this, what if I didn't want the first preceding sibling element, but instead wanted to select the closest preceding sibling that ended in a specific "VALUE"?

Thanks!
 
Old August 31st, 2006, 03:40 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>the closest preceding sibling that ended in a specific "VALUE"

I read that as:

the closest preceding sibling whose name ends in a specific "VALUE"

It's not a good idea to build structural information into your element names - that's what attributes are for. But if you must, you can do

preceding-sibling::*[ends-with(local-name(), 'abcd')][1]

ends-with() is XPath 2.0.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 31st, 2006, 08:06 AM
Authorized User
 
Join Date: Jun 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:I read that as: the closest preceding sibling whose name ends in a specific "VALUE"
Correct.

Quote:
quote:It's not a good idea to build structural information into your element names - that's what attributes are for.
Agreed. Unfortunately, I don't create the XML, just process what I'm given, so I don't have the ability to make that change.

Quote:
quote:ends-with() is XPath 2.0.
With my limited knowledge of 2.0, it sounds like in order to use it will involve some kind of software install/update, both of which can't be done for this project (besides, it would require every PC that wanted to view the XML correctly to perform the same upgrade/install, something that again may or may not be able to be done depending on each of their company's policies, so most likely not).

If it can't be done in 1.0 then I'll just have to report back that I'm unable to get it done with the current limitations, and see if they'll cough up the money to get the XML corrected so that the structural information is in the attributes instead of the element name, since that would be the less intrusive alternative.

Thanks again Michael, I appreciate the response, and feedback.

Thanks!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using preceding-sibling mcanne98 Infopath 0 September 11th, 2008 11:09 PM
preceding-sibling descendant xaun XSLT 3 February 28th, 2006 02:48 PM
preceding-sibling jonesyp XSLT 2 November 22nd, 2005 12:29 PM
preceding-sibling after sort rushman XSLT 3 January 18th, 2005 11:08 AM
preceding-sibling behaviour andrewcday XSLT 6 December 3rd, 2004 03:24 PM





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