Subject: Trouble with preceding sibling to a parent
Posted By: steelrose Post Date: 8/29/2006 1:53:22 PM
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!
Reply By: mhkay Reply Date: 8/29/2006 2:00:52 PM
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
Reply By: steelrose Reply Date: 8/29/2006 2:12:20 PM
Thanks Michael, as always, you're a wealth of knowledge (and quick to respond)!

Thanks!
Reply By: steelrose Reply Date: 8/30/2006 2:21:16 PM
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!
Reply By: mhkay Reply Date: 8/31/2006 3:40:11 AM
>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
Reply By: steelrose Reply Date: 8/31/2006 8:06:51 AM
quote:
I read that as: the closest preceding sibling whose name ends in a specific "VALUE"


Correct.

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:
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!

Go to topic 49091

Return to index page 188
Return to index page 187
Return to index page 186
Return to index page 185
Return to index page 184
Return to index page 183
Return to index page 182
Return to index page 181
Return to index page 180
Return to index page 179