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
Jeff MichaelsSubject: Need XPath to return just immediate child nodes
Author: Jeff Michaels
Date: 17 Apr 2007 04:18 PM
Hello,

Even though this is an XQuery forum, I believe XPath 2.o is a subset of XQuery, and so I will go ahead and ask this question...

I have been working feverishly to come up with an XPath expression that returns just 1-level deep of child nodes, and *no* grandchildren, along with those child nodes' attributes.

Assuming an XML fragment of:

<Products>
<Hardware location="" arrival="">
<Home location="" arrival="">
</Home>
<Business location="" arrival="">
</Business>
</Hardware>
<Software location="" arrival="">
<Home location="" arrival="">
</Home>
<Business location="" arrival="">
</Business>
</Software>
</Products>

I want to return those Product categories just under <Products> and to exclude <Products> element itself. Also, the child element names will not be known up front.

I only want to return something like:

<Hardware location="" arrival="">
</Hardware>
<Software location="" arrival="">
</Software>

The requirement is to not have any grandchild nodes and to include all child attributes...

If I execute an XPath expression of: //Products/* or //Products/child::node() or //Products/child::element(), I get child, grandchild, etc {a subtree}.

How do I get just the immediate children of a given node/element, along with it's attributes...?

Thanks,

Doogey



Postnext
Minollo I.Subject: Need XPath to return just immediate child nodes
Author: Minollo I.
Date: 17 Apr 2007 04:28 PM
I don't think you can do that using XPath; XPath will return nodes matching your expression; and those nodes will still contain the sub-elements defined in the queried fragment.

On the other hand, I'm not sure why that's a problem; //Products/* won't return two elements, say <software>, <home>; it will return one element, <software><home>...</home></software>, where <home> is a child of the <software> element. What you do with that result downstream should take care of ignoring children elements of the elements returned by //Products/*

If you really need to have a "clean" sequence of only the top level children of Products, you would have to switch to XQuery, that will help you building the XML result the way you want.

Postnext
Jeff MichaelsSubject: Need XPath to return just immediate child nodes
Author: Jeff Michaels
Date: 17 Apr 2007 06:49 PM
Originally Posted: 17 Apr 2007 04:38 PM
That is what I am thinking, so what would the XQuery for this look like..?

Here is what I am currently working on {but it does not work}

* * * * * * * * *

for $n in doc("http://localhost:8080/exist/servlet/db/config.xml")//Product/*/*

let $name := fn:node-name($n)
let $attrs := $n/@*

return
<output>
name={$name}
attrs=
{
for $a in $attrs
let $attrName := fn:local-name($a)
let $attrVal := fn:string($a)
return <attr>$attrName $attrVal</attr>
}
</output>


...In this example i am not yet trying to create the final output, but just trying to grab each attribute node's name and value.

...but this is not working, as the attributes that I grab, don't seem to list out; the return value of the nested FLWOR statement is empty. My other issue is that I cannot seem to create the node name like:

<{$name}>
</{$name}>

I get nasty syntax error stating: XQuery syntax error in #...ttrVal</attr> } </output> <{$#: Unexpected token "{" in path expression MCC/xml test.xq line 19 1176849281368 202154


It apparently doesn't like using a variable to create an element name...

Any ideas on both counts?

Doogey

Posttop
Minollo I.Subject: Need XPath to return just immediate child nodes
Author: Minollo I.
Date: 17 Apr 2007 11:00 PM
What processor and development environment are you using?

 
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.