| Subcribe via RSS

Navigating to a Sibling Node via XPath

January 4th, 2010 Posted in XSL

So I’m in an XSLT stylesheet, and I’ve navigated my way down to the node level. Now I’m trying to traverse my way down to the next sibling node and get an attribute out of it. Something like the following:

Not the best XML structure in the world, but what do you do when you have inherited code? After a long time of searching and trying out about 20 different possible iterations, I found the best solution to be:

For a quick translation, we are currently at the question node from the XML sample above, looking to step down into the answer node to get the text value. The second XSLT code block’s select statement tells the routine to get the following sibling node ‘answer’ even more specifically the first answer node following the question node. From there, we just reference the text attribute within the node.