473,414 Members | 1,621 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,414 software developers and data experts.

Why does this xpath fail?

XPathNavigator nav = MyCreateNav(); // InnerXml == "software"
nav.SelectSingleNode".[.='software']");

The select returns an exception:
+ $exception {"'.[.='software']' has an invalid token."} System.Exception
{System.Xml.XPath.XPathException}

Any idea why?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Feb 16 '06 #1
13 3207


David Thielen wrote:
XPathNavigator nav = MyCreateNav(); // InnerXml == "software"
nav.SelectSingleNode".[.='software']");

^^^^^^
That is not even syntactically correct C# so first post the real code
then we can discuss the XPath expression.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 17 '06 #2
Sorry - dropped a (

XPathNavigator nav = MyCreateNav(); // InnerXml == "software"
nav.SelectSingleNode(".[.='software']");

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

"Martin Honnen" wrote:


David Thielen wrote:
XPathNavigator nav = MyCreateNav(); // InnerXml == "software"
nav.SelectSingleNode".[.='software']");

^^^^^^
That is not even syntactically correct C# so first post the real code
then we can discuss the XPath expression.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Feb 17 '06 #3


David Thielen wrote:

XPathNavigator nav = MyCreateNav(); // InnerXml == "software"
nav.SelectSingleNode(".[.='software']");


It is not a syntatically correct XPath expression, see
<http://www.w3.org/TR/xpath#section-Location-Steps>
which defines

Step ::= AxisSpecifier NodeTest Predicate*
| AbbreviatedStep

what you have with . is an abbreviated step, there is no predicate
allowed after that.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 17 '06 #4
If I want to test if the node == "software", what is the syntax to do that?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

"Martin Honnen" wrote:


David Thielen wrote:

XPathNavigator nav = MyCreateNav(); // InnerXml == "software"
nav.SelectSingleNode(".[.='software']");


It is not a syntatically correct XPath expression, see
<http://www.w3.org/TR/xpath#section-Location-Steps>
which defines

Step ::= AxisSpecifier NodeTest Predicate*
| AbbreviatedStep

what you have with . is an abbreviated step, there is no predicate
allowed after that.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Feb 17 '06 #5


David Thielen wrote:
If I want to test if the node == "software", what is the syntax to do that?


self::node()[.='software']
would work with SelectSingleNode or Select as it finds a node set.
But of course
. = 'software'
is an allowed expression returning a boolean value, so with the XPath
API you would need the Evaluate method and not SelectSingleNode/Select.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 17 '06 #6
Hi,

In other words, David, first of all, you must decide if you want to *select*
the node or do you just want to *check* if that particular node has this
value. The implementation would differ in both cases.

Assuming you want to select only 1 node, use an
XmlDocument.SelectSingleNode("XpathExpression"). You can also use an XmlNode
in place of a XmlDocument. (To correct your code, the XPathNavigator class
does not have any method called "SelectSingleNode")

If you want to select multiple nodes and must use a XPathNavigator, then use
nav.Select("XpathExpression").

If you want to check for a value, use
myReturnValue = CType(nav.Evaluate("XpathExpression"), Type)

As far as the XPath expression to select a particular node is concerned,
that depends on your Xml. However, I have read that the more exactly you
define your expression, the better the performance.

For instance,
/ROOTNODE/CHILDNODE/GRANDCHILDNODE[.='software']

If you share your Xml format, it might be easier to define the exact Xpath
to use.

Hope this helps,

Regards,

Cerebrus.
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:uN**************@TK2MSFTNGP09.phx.gbl...


David Thielen wrote:
If I want to test if the node == "software", what is the syntax to do
that?
self::node()[.='software']
would work with SelectSingleNode or Select as it finds a node set.
But of course
. = 'software'
is an allowed expression returning a boolean value, so with the XPath
API you would need the Evaluate method and not SelectSingleNode/Select.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Feb 17 '06 #7

Cerebrus99 wrote:
(To correct your code, the XPathNavigator class
does not have any method called "SelectSingleNode")


It has in .NET 2.0.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 17 '06 #8
Hi;

Here is what I am doing. The xml is:
<order>
<product>software</product>
</order>

If I create an
XPathNavigator nav = XPathNavigator("/order"); // actually created using
XPathDocument and selecting down to that node
I can call:
nav.SelectSingleNode("product[.='software']");

But these fail (the selects):
XPathNavigator nav = XPathNavigator("/order/product");
nav.SelectSingleNode("[.='software']");
nav.SelectSingleNode(".[.='software']");
nav.SelectSingleNode("./[.='software']");

Why? It seems to me this should return the node <product>software</product>.
The only thing that works for this nav is:
nav.Evaluate(".='software'");

I am probably not understanding something here but I have no idea what and I
have spent a couple of hours trying to figure this out. Help please.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Feb 24 '06 #9
Hi David,

Wow ! You took almost a week to reply back !

I'm not using VS 2005, so not familiar with the SelectSingleNode method of
the XPathNavigator class, so I will assume, that all it requires is a string
containing the XPathExpression, and in that case, the problem is not with
the method, but it is with the XPathExpression you're using.

By using square brackets in an XPath expression you specify an element
further. So you cannot simply use a condition in square brackets, and expect
something to be selected. It has to be in the form :

/Node[condition]

(Select a node where this condition holds true)

While nav.Evaluate(".='software'") checks if the current node is "software".
Since the current node is at the start of the document by default, it
returns a Boolean False value. It works, but returns False, and doesn't do
what you intend to do.

So, my final suggestion is just use the following Xpath expression (looking
at your 3 line Xml sample)

nav.SelectSingleNode("order/product[.='software']")

Regards,

Cerebrus.


"David Thielen" <da***@bogus.windward.net> wrote in message
news:F0**********************************@microsof t.com...
Hi;

Here is what I am doing. The xml is:
<order>
<product>software</product>
</order>

If I create an
XPathNavigator nav = XPathNavigator("/order"); // actually created using
XPathDocument and selecting down to that node
I can call:
nav.SelectSingleNode("product[.='software']");

But these fail (the selects):
XPathNavigator nav = XPathNavigator("/order/product");
nav.SelectSingleNode("[.='software']");
nav.SelectSingleNode(".[.='software']");
nav.SelectSingleNode("./[.='software']");

Why? It seems to me this should return the node <product>software</product>. The only thing that works for this nav is:
nav.Evaluate(".='software'");

I am probably not understanding something here but I have no idea what and I have spent a couple of hours trying to figure this out. Help please.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com


Feb 24 '06 #10
Hello;

Unfortunately I cannot do that. This is for a product where it is passed an
xpath to get a list of nodes ("/order/product") and then when iterating
through that list I have to see if nodes exist for an iteration of the list
(.='software').

And the inner check to see if a node exists could be a node, a list of
nodes, nodes with children, or just ".='software'". This works fine using
dom4j/jaxen in the java world - it returns a single node in this case.

And it seems to me there should be a way to do this...

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

"Cerebrus" wrote:
Hi David,

Wow ! You took almost a week to reply back !

I'm not using VS 2005, so not familiar with the SelectSingleNode method of
the XPathNavigator class, so I will assume, that all it requires is a string
containing the XPathExpression, and in that case, the problem is not with
the method, but it is with the XPathExpression you're using.

By using square brackets in an XPath expression you specify an element
further. So you cannot simply use a condition in square brackets, and expect
something to be selected. It has to be in the form :

/Node[condition]

(Select a node where this condition holds true)

While nav.Evaluate(".='software'") checks if the current node is "software".
Since the current node is at the start of the document by default, it
returns a Boolean False value. It works, but returns False, and doesn't do
what you intend to do.

So, my final suggestion is just use the following Xpath expression (looking
at your 3 line Xml sample)

nav.SelectSingleNode("order/product[.='software']")

Regards,

Cerebrus.


"David Thielen" <da***@bogus.windward.net> wrote in message
news:F0**********************************@microsof t.com...
Hi;

Here is what I am doing. The xml is:
<order>
<product>software</product>
</order>

If I create an
XPathNavigator nav = XPathNavigator("/order"); // actually created using
XPathDocument and selecting down to that node
I can call:
nav.SelectSingleNode("product[.='software']");

But these fail (the selects):
XPathNavigator nav = XPathNavigator("/order/product");
nav.SelectSingleNode("[.='software']");
nav.SelectSingleNode(".[.='software']");
nav.SelectSingleNode("./[.='software']");

Why? It seems to me this should return the node

<product>software</product>.
The only thing that works for this nav is:
nav.Evaluate(".='software'");

I am probably not understanding something here but I have no idea what and

I
have spent a couple of hours trying to figure this out. Help please.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com


Feb 24 '06 #11
Hi David,
Unfortunately I cannot do that. This is for a product where it is passed an
xpath to get a list of nodes ("/order/product") and then when iterating
through that list I have to see if nodes exist for an iteration of the list
(.='software')
I don't see the problem. If you mean, you cannot fully define the
XPathExpression,
you did mention that nav.SelectSingleNode("product[.='software']");
works for you.
Use Wildcards if necessary.

//product[.='software'] will select all Product Nodes which match the
condition (even if at different levels)
And the inner check to see if a node exists could be a node, a list of
nodes, nodes with children, or just ".='software'". This works fine using
dom4j/jaxen in the java world - it returns a single node in this case.


Use checks using the XmlNodeType enumeration for each of the
possibilities.

HTH,

Regards,

Cerebrus.

Feb 25 '06 #12
Hi;

The problem we have is our customers enter the xpath in pieces and we have
no control over it. I know what is happening in this specific case because I
have a set of xpath a customer uses that breaks here.

But I can't change what they enter and I can't control it. In this case the
problem I am facing is "product[.='software']" works but ".[.='software']" is
illegal and I am trying to figure out how to handle this.

And the root problem I have is I don't understand why ".[.='software']" is
illegal - it seems to me that it should return that one node.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

"Cerebrus" wrote:
Hi David,
Unfortunately I cannot do that. This is for a product where it is passed an
xpath to get a list of nodes ("/order/product") and then when iterating
through that list I have to see if nodes exist for an iteration of the list
(.='software')


I don't see the problem. If you mean, you cannot fully define the
XPathExpression,
you did mention that nav.SelectSingleNode("product[.='software']");
works for you.
Use Wildcards if necessary.

//product[.='software'] will select all Product Nodes which match the
condition (even if at different levels)
And the inner check to see if a node exists could be a node, a list of
nodes, nodes with children, or just ".='software'". This works fine using
dom4j/jaxen in the java world - it returns a single node in this case.


Use checks using the XmlNodeType enumeration for each of the
possibilities.

HTH,

Regards,

Cerebrus.

Feb 25 '06 #13
Hi;

I reposted this as a new question with a sample program to give a real clear
example.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com
Feb 25 '06 #14

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

Similar topics

1
by: Flyzone | last post by:
i think i don't anderstand the use of xpath :-( Someone could help me? XML file: <result> <delete> <ok> <tittle>Deleted ok</title> <msg>The id was deleted</msg> </ok>
1
by: Dmitry Martynov | last post by:
Hi I have a question whether XmlValidatingReader doesn't check keyref constrain (the same with key constraint) or I do smth wrong. I have the following schema <?xml version="1.0"...
2
by: dc | last post by:
i have a xml file like this: <?xml version="1.0" encoding="utf-8"?> <validate xmlns="http://tempuri.org/fieldValidate.xsd"> <field name="Short Name" type="SN" length="10"> <requiredChar...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
9
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
4
by: Ross Presser | last post by:
I'm feeling very stupid about this ... pdf2html (http://pdf2html.sourceforge.net) is an app that reads a PDF and can generate HTML or XML; in my case I'm using the XML. The PDF I'm working with...
1
by: woodworthjames | last post by:
Hello, not sure i am using xpath correctly. have the following trivial xml. <?:xml version="1.0" standalone="yes" ?> <top attr="1"> content1 content2 <inner id="first" day="friday"> content3...
6
by: J.Marsch | last post by:
I must be completely losing my mind. I have some code that writes to config files. It works great with app.config files, but fails miserably with web.config files. For the life of me, I cannot...
14
by: Mikhail Teterin | last post by:
Hello! What's would be the syntax for a query, which would allow me to get only the elements with non-empty text-nodes? For example, from: <a><b></b></a> <c/> <d><e>meow</e></d>
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.