473,320 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

can't get the xpath working

Hi,

this is my first xpath try and after reading many web-pages that xpath is
still not working :-(
(btw, it's my own project, not a homework)

I have something like that:

<root>
<one/>
<two>
<two-one/>
<two-two/>
</two>
<three>
<three-one>
<three-one-one/>
</three-one>
<three-two/>
</three>
</four>
</root>

I want all tags expect /root/two and root/three/three-one:

<root>
<one/>
<three>
<three-two/>
</three>
</four>
</root>
I have tried the following path, but it does not work:
getXPath("/root/*[name()!='two' and not(descendant::three-one/*)]

thanks for any enlightenment,
Michael
Jul 20 '05 #1
8 1162


Michael Wohlwend wrote:
I have something like that:

<root>
<one/>
<two>
<two-one/>
<two-two/>
</two>
<three>
<three-one>
<three-one-one/>
</three-one>
<three-two/>
</three>
</four> ^^^^^^
not well-formed
I want all tags expect /root/two and root/three/three-one:

<root>
<one/>
<three>
<three-two/>
</three>
</four>

^^^^^^
not well-formed either
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #2

Michael Wohlwend <mi*****@fantasymail.de> writes:
Hi,

this is my first xpath try and after reading many web-pages that xpath is
still not working :-(
(btw, it's my own project, not a homework)

I have something like that:

<root>
<one/>
<two>
<two-one/>
<two-two/>
</two>
<three>
<three-one>
<three-one-one/>
</three-one>
<three-two/>
</three>
</four>
</root>

I want all tags expect /root/two and root/three/three-one:
Xpath doesn't deal with tags but with a node tree, this isn't just a
matter of naming, it explains your problem.

Xpath returns _unaltered_ nodes from a source tree and if you return the
element node with name "root" that will have all the same children and
grandchildren that it has in the source. So even if you only select the
single node, /root, you will get back the whole document, effectively.

the XPaths /root/two and root/three/three-one select the nodes that you
don't want to copy and you could use those in xslt or similar program to
specify the transformation that you want, but you can not select
the result below using XPath as that is not a subset of the nodes in the
original node set.
<root>
<one/>
<three>
<three-two/>
</three>
</four>
</root>
I have tried the following path, but it does not work:
getXPath("/root/*[name()!='two' and not(descendant::three-one/*)]

thanks for any enlightenment,
Michael

David
Jul 20 '05 #3
Your XML is not valid. I would look here for how to write XML;

http://www.w3schools.com/xml/default.asp

Once you've had a look at that, stick your XML through a validator to
make sure it is correct. Unless your XML is correct, you won't get
anywhere with XPath.

Jul 20 '05 #4
Martin Honnen wrote:
</four>

^^^^^^
not well-formed


yes I know, that should be </one>
but my problem stays the same
I want all tags expect /root/two and root/three/three-one:


Jul 20 '05 #5
reclusive monkey wrote:
Your XML is not valid. I would look here for how to write XML;


yes I have messed up a name for the example, the right closing tag is </one>
but the xpath is my problem

thanks
Michael

Jul 20 '05 #6
David Carlisle wrote:
Xpath doesn't deal with tags but with a node tree, this isn't just a
matter of naming, it explains your problem.

Xpath returns _unaltered_ nodes from a source tree and if you return the
element node with name "root" that will have all the same children and
grandchildren that it has in the source. So even if you only select the
single node, /root, you will get back the whole document, effectively.

the XPaths /root/two and root/three/three-one select the nodes that you
don't want to copy and you could use those in xslt or similar program to
specify the transformation that you want, but you can not select
the result below using XPath as that is not a subset of the nodes in the
original node set.


but the following works for getting rid of node two:

"/root/*[name()!='two']

is gives:
<root>
<one/>
<three>
Â*Â*Â*<three-one>Â*
Â*Â*Â*Â*Â*Â*<three-one-one/>
Â*Â*</three-one>
Â*Â*Â*<three-two/>
</three>
</root>

just the node <three-one> should go away too. I thought this is easy, but it
seems, I cannot handle it with xpath

Michael

ps: I thought it's easier with number-nodes as an example, but no, normal
names would have been better :-)

Jul 20 '05 #7
but the following works for getting rid of node two:

"/root/*[name()!='two']

is gives:
<root>
<one/>


No. /root/*[name()!='two'] selects all the children of root except two
In particular it doesn't select the root element.
If you did select root then it would be returned unchanged (and would
have all its children).

David
Jul 20 '05 #8
Michael Wohlwend <mi*****@fantasymail.de> writes:
but the following works for getting rid of node two:

"/root/*[name()!='two'] is gives: <root>
<one/>
<three>
* <three-one>
<three-one-one/>
* </three-one>
*<three-two/>
</three>
</root>


Ah, this is not what is denoted by "/root/*[name() != 'two']".

What's denoted by that XPath expression would be

<one/>
<three>
* <three-one>
<three-one-one/>
* </three-one>
*<three-two/>
</three>

I.e. two nodes, the set of children of /root whose generic
identifier is not 'two'.

Either a tool you are using is giving you a horribly wrong and
misleading result (it's returning a modified /root element
instead of the children of /root) or you may be
misinterpreting the correct result it's giving you.

Without knowing more about what tools you're using in what
context, it's hard to say more than that you should carefully
examine the evidence that led you to the belief quoted above.

I hope this helps.

-C. M. Sperberg-McQueen
World Wide Web Consortium
Jul 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bnaya Eshet | last post by:
I do like XPath, I really do. But I'm working on the compact framework which XPath is not included. So I come to understanding that if XPath do not come to the mountain,
3
by: ziggyware | last post by:
Hi All, I have updated my XPath Generator software: XPath Studio .NET. ( http://www.ziggyware.com/downloads.php?cat_id=2 ) Easily select nodes from an xml file to generate XPath statements ...
6
by: Hoss | last post by:
Hello. Because IE and Mozilla have such completely different XML implementations, I have created a class to handle general XML tasks, such as iterating over nodes given an xpath, evaluating an...
7
by: Tim Hallwyl | last post by:
Hi, there! As I understand the XPaht recommendation, the context node is a node; not a node-list, not XPath object -- but a single node. Now, the WS-BPEL 2.0 specification allows an XML simple...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.