Subject:getting all nodes leading up to a given node but no descendants... Author:jake feeder Date:06 Mar 2007 03:23 PM
i feel like this should be easier than i'm making it... i have a document with recursively nested "tag" nodes as listed below.
what i want to do is get the tag with id=3, and all of its ancestors, but no other nodes. (would return <tag id="1" name="a"><tag id="2" name="b"><tag id="3" name="c"></tag></tag></tag>)
i've got:
//tag[@id="3"]/ancestor-or-self::node()
which gives me the proper nodes, and i could use a loop to recreate the tag structure, but i feel like i'm missing something trivial, probably a simple xpath solution with no need for loops at all.
does anyone else see the obvious solution that is eluding me, or do i really need to loop through and recreate the tags?
Subject:getting all nodes leading up to a given node but no descendants... Author:(Deleted User) Date:07 Mar 2007 03:17 AM
Hi Jake,
your solution is the correct one; every XPath expression will simply select one or more existing nodes from the input tree, while you want to create a new structure that only contains some of the them. So you have to extract the <tag> nodes you are interested into, and build copies of them excluding the children and attributes you are not interested into.
Subject:getting all nodes leading up to a given node but no descendants... Author:jake feeder Date:08 Mar 2007 01:29 PM
Thanks for the confirmation Alberto.
In case anyone else needs this someday, here is the recursive function I ended up with for this..
define function LimitPathToChildren($nTags, $iID)
{
for $nTag in $nTags
return
if ($nTag//@tagid=$iID) then
element { node-name($nTag)}
{ $nTag/@*, LimitPathToChildren($nTag/tag,$iID) }
else ()
}
for $nTag in $nTags
return LimitPathToChildren($nTag, (271,133,134))