XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
jake feederSubject: 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?

thanks,

--jake

<tag id="1" name="a">
<tag id="2" name="b">
<tag id="3" name="c">
<tag id="4" name="d">
</tag>
</tag>
<tag id="5" name="x">
</tag>
</tag>
</tag>
<tag id="6" name="y">
</tag>
<tag id="7" name="z">
</tag>


Postnext
Alberto MassariSubject: getting all nodes leading up to a given node but no descendants...
Author: Alberto Massari
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.

Alberto

Posttop
jake feederSubject: 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))

--jake

 
Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.