473,745 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why does this xpath fail?

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

The select returns an exception:
+ $exception {"'.[.='software']' has an invalid token."} System.Exceptio n
{System.Xml.XPa th.XPathExcepti on}

Any idea why?

--
thanks - dave
david_at_windwa rd_dot_net
http://www.windwardreports.com

Feb 16 '06 #1
13 3243


David Thielen wrote:
XPathNavigator nav = MyCreateNav(); // InnerXml == "software"
nav.SelectSingl eNode".[.='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.SelectSingl eNode(".[.='software']");

--
thanks - dave
david_at_windwa rd_dot_net
http://www.windwardreports.com

"Martin Honnen" wrote:


David Thielen wrote:
XPathNavigator nav = MyCreateNav(); // InnerXml == "software"
nav.SelectSingl eNode".[.='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.SelectSingl eNode(".[.='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_windwa rd_dot_net
http://www.windwardreports.com

"Martin Honnen" wrote:


David Thielen wrote:

XPathNavigator nav = MyCreateNav(); // InnerXml == "software"
nav.SelectSingl eNode(".[.='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 SelectSingleNod e 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 SelectSingleNod e/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.Sel ectSingleNode(" 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 "SelectSingleNo de")

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

If you want to check for a value, use
myReturnValue = CType(nav.Evalu ate("XpathExpre ssion"), 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*******@yaho o.de> wrote in message
news:uN******** ******@TK2MSFTN GP09.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 SelectSingleNod e 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 SelectSingleNod e/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 "SelectSingleNo de")


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>softwa re</product>
</order>

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

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

Why? It seems to me this should return the node <product>softwa re</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_windwa rd_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 SelectSingleNod e 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.SelectSingl eNode("order/product[.='software']")

Regards,

Cerebrus.


"David Thielen" <da***@bogus.wi ndward.net> wrote in message
news:F0******** *************** ***********@mic rosoft.com...
Hi;

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

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

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

Why? It seems to me this should return the node <product>softwa re</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_windwa rd_dot_net
http://www.windwardreports.com


Feb 24 '06 #10

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

Similar topics

1
1311
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
2861
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" encoding="utf-8"?> <xs:schema id="module" targetNamespace="http://tempuri.org/module.xsd"
2
10049
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 value="Y" errMsg="required Y" /> <requiredChar value="X" errMsg="required X" /> <bannedChar value="Z" errMsg="banned Z" /> <bannedChar value="A" errMsg="banned A" /> </field>
18
7735
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); System.out.println(xp.evaluate(new InputSource(new FileInputStream("a.xml"))));
9
2155
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 should work. Also the second nav.OuterXml appears to also be wrong to me. Can someone explain to me why this does not work? (This is an example from a program we have where xpath can be entered in two parts so we have to be able
4
3377
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 is a concatenation of many reports; my objective is to find the first page of each report, which I've discovered can be found in this particular instance by looking for an xml element with a particular attribute "left" equal to 277. So I want...
1
1783
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 </inner> </top>
6
7299
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 figure out what is going on here. I have taken it all the way back to just selecting the configuration node (top level node), and it fails! How can this line fail (returns null)??
14
17153
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
8795
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9486
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9344
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9214
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8218
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6057
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4827
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2195
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.