473,545 Members | 721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why does this xpath fail - starting over

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
to handle this. We can't just always require the fill
/order/product[.='software'].)

Program below sig.

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

Program.cs:
using System.Xml;
using System.Xml.XPat h;
using System;
using System.IO;

namespace XPathSample
{
class Program
{
static void Main(string[] args)
{

string xml =
"<order>" +
"<product>softw are</product>" +
"<product>softw are</product>" +
"</order>";
XPathDocument doc = new XPathDocument(n ew StringReader(xm l));

// this works
XPathNavigator nav = doc.CreateNavig ator();
XPathNavigator rtn = nav.SelectSingl eNode("/order/product[.='software']");
Console.Out.Wri teLine("nav(/) = " + nav.OuterXml);
Console.Out.Wri teLine("/order/product[.='software'] = " + rtn.OuterXml);

// this works although the nav.OuterXml appears wrong
nav = nav.SelectSingl eNode("/order");
rtn = nav.SelectSingl eNode("product[.='software']");
Console.Out.Wri teLine("nav(/order) = " + nav.OuterXml);
Console.Out.Wri teLine("product[.='software'] = " + rtn.OuterXml);

// this works (same as prev)
rtn = nav.SelectSingl eNode("./product[.='software']");
Console.Out.Wri teLine("./product[.='software'] = " + rtn.OuterXml);

nav = nav.SelectSingl eNode("product" );
Console.Out.Wri teLine("nav(/order/product) = " + nav.OuterXml);

// these all fail - WHY?
try
{
rtn = nav.SelectSingl eNode("[.='software']");
}
catch (XPathException ex)
{
Console.Out.Wri teLine("[.='software'] exception = " + ex.Message);
}
try
{
rtn = nav.SelectSingl eNode("./[.='software']");
}
catch (XPathException ex)
{
Console.Out.Wri teLine("./[.='software'] exception = " + ex.Message);
}

// yes this works - but I need the node as it does have a value.
object obj = nav.Evaluate(". ='software'");
Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);
Console.Out.Wri teLine("all done");
}
}
}
Feb 25 '06 #1
9 2133
> // these all fail - WHY?

because of a common feature -- they all have invalid XPath syntax.
try
{
rtn = nav.SelectSingl eNode("[.='software']");
}
catch (XPathException ex)
{
Console.Out.Wri teLine("[.='software'] exception = " + ex.Message);
}
try
{
rtn = nav.SelectSingl eNode("./[.='software']");
}
catch (XPathException ex)
{
Console.Out.Wri teLine("./[.='software'] exception = " + ex.Message);
}

// yes this works - but I need the node as it does have a value.
object obj = nav.Evaluate(". ='software'");
Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);

Try:

"self::*[.='software']"

or

"string(sel f::*[.='software'])"

depending on whether you need the node or its string value returned.
Cheers,
Dimitre Novatchev

"David Thielen" <da***@bogus.wi ndward.net> wrote in message
news:74******** *************** ***********@mic rosoft.com... 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
to handle this. We can't just always require the fill
/order/product[.='software'].)

Program below sig.

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

Program.cs:
using System.Xml;
using System.Xml.XPat h;
using System;
using System.IO;

namespace XPathSample
{
class Program
{
static void Main(string[] args)
{

string xml =
"<order>" +
"<product>softw are</product>" +
"<product>softw are</product>" +
"</order>";
XPathDocument doc = new XPathDocument(n ew StringReader(xm l));

// this works
XPathNavigator nav = doc.CreateNavig ator();
XPathNavigator rtn = nav.SelectSingl eNode("/order/product[.='software']");
Console.Out.Wri teLine("nav(/) = " + nav.OuterXml);
Console.Out.Wri teLine("/order/product[.='software'] = " + rtn.OuterXml);

// this works although the nav.OuterXml appears wrong
nav = nav.SelectSingl eNode("/order");
rtn = nav.SelectSingl eNode("product[.='software']");
Console.Out.Wri teLine("nav(/order) = " + nav.OuterXml);
Console.Out.Wri teLine("product[.='software'] = " + rtn.OuterXml);

// this works (same as prev)
rtn = nav.SelectSingl eNode("./product[.='software']");
Console.Out.Wri teLine("./product[.='software'] = " + rtn.OuterXml);

nav = nav.SelectSingl eNode("product" );
Console.Out.Wri teLine("nav(/order/product) = " + nav.OuterXml);

// these all fail - WHY?
try
{
rtn = nav.SelectSingl eNode("[.='software']");
}
catch (XPathException ex)
{
Console.Out.Wri teLine("[.='software'] exception = " + ex.Message);
}
try
{
rtn = nav.SelectSingl eNode("./[.='software']");
}
catch (XPathException ex)
{
Console.Out.Wri teLine("./[.='software'] exception = " + ex.Message);
}

// yes this works - but I need the node as it does have a value.
object obj = nav.Evaluate(". ='software'");
Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);
Console.Out.Wri teLine("all done");
}
}
}

Feb 26 '06 #2
Hello;

I assumed it is invalid syntax. My question is why? I don't understand why
"[.='software']" or some variation of that is invalid.

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

"Dimitre Novatchev" wrote:
// these all fail - WHY?


because of a common feature -- they all have invalid XPath syntax.
try
{
rtn = nav.SelectSingl eNode("[.='software']");
}
catch (XPathException ex)
{
Console.Out.Wri teLine("[.='software'] exception = " + ex.Message);
}
try
{
rtn = nav.SelectSingl eNode("./[.='software']");
}
catch (XPathException ex)
{
Console.Out.Wri teLine("./[.='software'] exception = " + ex.Message);
}

// yes this works - but I need the node as it does have a value.
object obj = nav.Evaluate(". ='software'");
Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);

Try:

"self::*[.='software']"

or

"string(sel f::*[.='software'])"

depending on whether you need the node or its string value returned.
Cheers,
Dimitre Novatchev

"David Thielen" <da***@bogus.wi ndward.net> wrote in message
news:74******** *************** ***********@mic rosoft.com...
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
to handle this. We can't just always require the fill
/order/product[.='software'].)

Program below sig.

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

Program.cs:
using System.Xml;
using System.Xml.XPat h;
using System;
using System.IO;

namespace XPathSample
{
class Program
{
static void Main(string[] args)
{

string xml =
"<order>" +
"<product>softw are</product>" +
"<product>softw are</product>" +
"</order>";
XPathDocument doc = new XPathDocument(n ew StringReader(xm l));

// this works
XPathNavigator nav = doc.CreateNavig ator();
XPathNavigator rtn = nav.SelectSingl eNode("/order/product[.='software']");
Console.Out.Wri teLine("nav(/) = " + nav.OuterXml);
Console.Out.Wri teLine("/order/product[.='software'] = " + rtn.OuterXml);

// this works although the nav.OuterXml appears wrong
nav = nav.SelectSingl eNode("/order");
rtn = nav.SelectSingl eNode("product[.='software']");
Console.Out.Wri teLine("nav(/order) = " + nav.OuterXml);
Console.Out.Wri teLine("product[.='software'] = " + rtn.OuterXml);

// this works (same as prev)
rtn = nav.SelectSingl eNode("./product[.='software']");
Console.Out.Wri teLine("./product[.='software'] = " + rtn.OuterXml);

nav = nav.SelectSingl eNode("product" );
Console.Out.Wri teLine("nav(/order/product) = " + nav.OuterXml);

// these all fail - WHY?
try
{
rtn = nav.SelectSingl eNode("[.='software']");
}
catch (XPathException ex)
{
Console.Out.Wri teLine("[.='software'] exception = " + ex.Message);
}
try
{
rtn = nav.SelectSingl eNode("./[.='software']");
}
catch (XPathException ex)
{
Console.Out.Wri teLine("./[.='software'] exception = " + ex.Message);
}

// yes this works - but I need the node as it does have a value.
object obj = nav.Evaluate(". ='software'");
Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);
Console.Out.Wri teLine("all done");
}
}
}


Feb 27 '06 #3

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

I assumed it is invalid syntax. My question is why? I don't understand why
"[.='software']" or some variation of that is invalid.
Read the XPath 1.0 Spec.

It says:

http://www.w3.org/TR/xpath#predicates

"A predicate filters a node-set with respect to an axis to produce a new
node-set. For each node in the node-set to be filtered, the PredicateExpr is
evaluated with that node as the context node, with the number of nodes in
the node-set as the context size, and with the proximity position of the
node in the node-set with respect to the axis as the context position; if
PredicateExpr evaluates to true for that node, the node is included in the
new node-set; otherwise, it is not included."

So, a predicate can only apear after some XPath expression and filters it. A
predicate, alone, is not a valid XPath expression.

This is also made clear if we look at the BNF grammar definition of XPath
1.0. Of all rules only rule [4] and rule [20] contain the nonterminal symbol
predicate and it is always not the leading symbol (of the RHS):

http://www.w3.org/TR/xpath#section-Location-Steps
[4] Step ::= AxisSpecifier NodeTest Predicate*
| AbbreviatedStep


http://www.w3.org/TR/xpath#node-sets

[20] FilterExpr ::= PrimaryExpr
| FilterExpr Predicate


I hope that this answers completely your question.
Cheers,
Dimitre Novatchev

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

"Dimitre Novatchev" wrote:
> // these all fail - WHY?


because of a common feature -- they all have invalid XPath syntax.
> try
> {
> rtn = nav.SelectSingl eNode("[.='software']");
> }
> catch (XPathException ex)
> {
> Console.Out.Wri teLine("[.='software'] exception = " + ex.Message);
> }
> try
> {
> rtn = nav.SelectSingl eNode("./[.='software']");
> }
> catch (XPathException ex)
> {
> Console.Out.Wri teLine("./[.='software'] exception = " + ex.Message);
> }
>
> // yes this works - but I need the node as it does have a value.
> object obj = nav.Evaluate(". ='software'");
> Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);

Try:

"self::*[.='software']"

or

"string(sel f::*[.='software'])"

depending on whether you need the node or its string value returned.
Cheers,
Dimitre Novatchev

"David Thielen" <da***@bogus.wi ndward.net> wrote in message
news:74******** *************** ***********@mic rosoft.com...
> 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
> to handle this. We can't just always require the fill
> /order/product[.='software'].)
>
> Program below sig.
>
> --
> thanks - dave
> david_at_windwa rd_dot_net
> http://www.windwardreports.com
>
> Program.cs:
> using System.Xml;
> using System.Xml.XPat h;
> using System;
> using System.IO;
>
> namespace XPathSample
> {
> class Program
> {
> static void Main(string[] args)
> {
>
> string xml =
> "<order>" +
> "<product>softw are</product>" +
> "<product>softw are</product>" +
> "</order>";
> XPathDocument doc = new XPathDocument(n ew StringReader(xm l));
>
> // this works
> XPathNavigator nav = doc.CreateNavig ator();
> XPathNavigator rtn =
> nav.SelectSingl eNode("/order/product[.='software']");
> Console.Out.Wri teLine("nav(/) = " + nav.OuterXml);
> Console.Out.Wri teLine("/order/product[.='software'] = " +
> rtn.OuterXml);
>
> // this works although the nav.OuterXml appears wrong
> nav = nav.SelectSingl eNode("/order");
> rtn = nav.SelectSingl eNode("product[.='software']");
> Console.Out.Wri teLine("nav(/order) = " + nav.OuterXml);
> Console.Out.Wri teLine("product[.='software'] = " + rtn.OuterXml);
>
> // this works (same as prev)
> rtn = nav.SelectSingl eNode("./product[.='software']");
> Console.Out.Wri teLine("./product[.='software'] = " + rtn.OuterXml);
>
> nav = nav.SelectSingl eNode("product" );
> Console.Out.Wri teLine("nav(/order/product) = " + nav.OuterXml);
>
> // these all fail - WHY?
> try
> {
> rtn = nav.SelectSingl eNode("[.='software']");
> }
> catch (XPathException ex)
> {
> Console.Out.Wri teLine("[.='software'] exception = " + ex.Message);
> }
> try
> {
> rtn = nav.SelectSingl eNode("./[.='software']");
> }
> catch (XPathException ex)
> {
> Console.Out.Wri teLine("./[.='software'] exception = " + ex.Message);
> }
>
> // yes this works - but I need the node as it does have a value.
> object obj = nav.Evaluate(". ='software'");
> Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);
>
>
> Console.Out.Wri teLine("all done");
> }
> }
> }
>
>



Feb 27 '06 #4
Hi;

First, thanks for helping on this I really appreciate it.

Ok, I see that the xpath must start with "." to get it to the node and the
"[...]" then modifies that - makes sense.

But then, shouldn't ".[.='software']" or "./.[.='software']" or
"./[.='software']" work.

In the case of ".[.='software']" isn't that saying start with the node "."
(which exists) and then return all where .='software'?

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

"Dimitre Novatchev" wrote:

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

I assumed it is invalid syntax. My question is why? I don't understand why
"[.='software']" or some variation of that is invalid.


Read the XPath 1.0 Spec.

It says:

http://www.w3.org/TR/xpath#predicates

"A predicate filters a node-set with respect to an axis to produce a new
node-set. For each node in the node-set to be filtered, the PredicateExpr is
evaluated with that node as the context node, with the number of nodes in
the node-set as the context size, and with the proximity position of the
node in the node-set with respect to the axis as the context position; if
PredicateExpr evaluates to true for that node, the node is included in the
new node-set; otherwise, it is not included."

So, a predicate can only apear after some XPath expression and filters it. A
predicate, alone, is not a valid XPath expression.

This is also made clear if we look at the BNF grammar definition of XPath
1.0. Of all rules only rule [4] and rule [20] contain the nonterminal symbol
predicate and it is always not the leading symbol (of the RHS):

http://www.w3.org/TR/xpath#section-Location-Steps
[4] Step ::= AxisSpecifier NodeTest Predicate*
| AbbreviatedStep


http://www.w3.org/TR/xpath#node-sets

[20] FilterExpr ::= PrimaryExpr
| FilterExpr Predicate


I hope that this answers completely your question.
Cheers,
Dimitre Novatchev

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

"Dimitre Novatchev" wrote:
> // these all fail - WHY?

because of a common feature -- they all have invalid XPath syntax.

> try
> {
> rtn = nav.SelectSingl eNode("[.='software']");
> }
> catch (XPathException ex)
> {
> Console.Out.Wri teLine("[.='software'] exception = " + ex.Message);
> }
> try
> {
> rtn = nav.SelectSingl eNode("./[.='software']");
> }
> catch (XPathException ex)
> {
> Console.Out.Wri teLine("./[.='software'] exception = " + ex.Message);
> }
>
> // yes this works - but I need the node as it does have a value.
> object obj = nav.Evaluate(". ='software'");
> Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);
Try:

"self::*[.='software']"

or

"string(sel f::*[.='software'])"

depending on whether you need the node or its string value returned.
Cheers,
Dimitre Novatchev

"David Thielen" <da***@bogus.wi ndward.net> wrote in message
news:74******** *************** ***********@mic rosoft.com...
> 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
> to handle this. We can't just always require the fill
> /order/product[.='software'].)
>
> Program below sig.
>
> --
> thanks - dave
> david_at_windwa rd_dot_net
> http://www.windwardreports.com
>
> Program.cs:
> using System.Xml;
> using System.Xml.XPat h;
> using System;
> using System.IO;
>
> namespace XPathSample
> {
> class Program
> {
> static void Main(string[] args)
> {
>
> string xml =
> "<order>" +
> "<product>softw are</product>" +
> "<product>softw are</product>" +
> "</order>";
> XPathDocument doc = new XPathDocument(n ew StringReader(xm l));
>
> // this works
> XPathNavigator nav = doc.CreateNavig ator();
> XPathNavigator rtn =
> nav.SelectSingl eNode("/order/product[.='software']");
> Console.Out.Wri teLine("nav(/) = " + nav.OuterXml);
> Console.Out.Wri teLine("/order/product[.='software'] = " +
> rtn.OuterXml);
>
> // this works although the nav.OuterXml appears wrong
> nav = nav.SelectSingl eNode("/order");
> rtn = nav.SelectSingl eNode("product[.='software']");
> Console.Out.Wri teLine("nav(/order) = " + nav.OuterXml);
> Console.Out.Wri teLine("product[.='software'] = " + rtn.OuterXml);
>
> // this works (same as prev)
> rtn = nav.SelectSingl eNode("./product[.='software']");
> Console.Out.Wri teLine("./product[.='software'] = " + rtn.OuterXml);
>
> nav = nav.SelectSingl eNode("product" );
> Console.Out.Wri teLine("nav(/order/product) = " + nav.OuterXml);
>
> // these all fail - WHY?
> try
> {
> rtn = nav.SelectSingl eNode("[.='software']");
> }
> catch (XPathException ex)
> {
> Console.Out.Wri teLine("[.='software'] exception = " + ex.Message);
> }
> try
> {
> rtn = nav.SelectSingl eNode("./[.='software']");
> }
> catch (XPathException ex)
> {
> Console.Out.Wri teLine("./[.='software'] exception = " + ex.Message);
> }
>
> // yes this works - but I need the node as it does have a value.
> object obj = nav.Evaluate(". ='software'");
> Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);
>
>
> Console.Out.Wri teLine("all done");
> }
> }
> }
>
>


Feb 27 '06 #5

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

First, thanks for helping on this I really appreciate it.

Ok, I see that the xpath must start with "." to get it to the node and the
"[...]" then modifies that - makes sense.

But then, shouldn't ".[.='software']" or "./.[.='software']" or
No, the syntax rules do not allow this:
[4] Step ::= AxisSpecifier NodeTest Predicate*
| AbbreviatedStep


as we can see, Predicatecan only occur after a NodeTest , not after an
AbbreviatedStep .
"./[.='software']" work.
Again, Predicate cannot be the leftmost symbol in a step

In the case of ".[.='software']" isn't that saying start with the node "."
(which exists) and then return all where .='software'?
This may seem a clear semantics, but the syntax is invalid according to the
XPath 1.0 grammar rules.

As I said, use:

self::node()[.='software']

Cheers,
Dimitre Novatchev

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

"Dimitre Novatchev" wrote:

"David Thielen" <da***@bogus.wi ndward.net> wrote in message
news:24******** *************** ***********@mic rosoft.com...
> Hello;
>
> I assumed it is invalid syntax. My question is why? I don't understand
> why
> "[.='software']" or some variation of that is invalid.


Read the XPath 1.0 Spec.

It says:

http://www.w3.org/TR/xpath#predicates

"A predicate filters a node-set with respect to an axis to produce a new
node-set. For each node in the node-set to be filtered, the PredicateExpr
is
evaluated with that node as the context node, with the number of nodes in
the node-set as the context size, and with the proximity position of the
node in the node-set with respect to the axis as the context position; if
PredicateExpr evaluates to true for that node, the node is included in
the
new node-set; otherwise, it is not included."

So, a predicate can only apear after some XPath expression and filters
it. A
predicate, alone, is not a valid XPath expression.

This is also made clear if we look at the BNF grammar definition of XPath
1.0. Of all rules only rule [4] and rule [20] contain the nonterminal
symbol
predicate and it is always not the leading symbol (of the RHS):

http://www.w3.org/TR/xpath#section-Location-Steps
[4] Step ::= AxisSpecifier NodeTest Predicate*
| AbbreviatedStep


http://www.w3.org/TR/xpath#node-sets

[20] FilterExpr ::= PrimaryExpr
| FilterExpr Predicate


I hope that this answers completely your question.
Cheers,
Dimitre Novatchev
>
> --
> thanks - dave
> david_at_windwa rd_dot_net
> http://www.windwardreports.com
>
>
>
> "Dimitre Novatchev" wrote:
>
>> > // these all fail - WHY?
>>
>> because of a common feature -- they all have invalid XPath syntax.
>>
>> > try
>> > {
>> > rtn = nav.SelectSingl eNode("[.='software']");
>> > }
>> > catch (XPathException ex)
>> > {
>> > Console.Out.Wri teLine("[.='software'] exception = " + ex.Message);
>> > }
>> > try
>> > {
>> > rtn = nav.SelectSingl eNode("./[.='software']");
>> > }
>> > catch (XPathException ex)
>> > {
>> > Console.Out.Wri teLine("./[.='software'] exception = " + ex.Message);
>> > }
>> >
>> > // yes this works - but I need the node as it does have a value.
>> > object obj = nav.Evaluate(". ='software'");
>> > Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);
>>
>>
>> Try:
>>
>> "self::*[.='software']"
>>
>> or
>>
>> "string(sel f::*[.='software'])"
>>
>> depending on whether you need the node or its string value returned.
>>
>>
>> Cheers,
>> Dimitre Novatchev
>>
>>
>>
>>
>>
>> "David Thielen" <da***@bogus.wi ndward.net> wrote in message
>> news:74******** *************** ***********@mic rosoft.com...
>> > 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
>> > to handle this. We can't just always require the fill
>> > /order/product[.='software'].)
>> >
>> > Program below sig.
>> >
>> > --
>> > thanks - dave
>> > david_at_windwa rd_dot_net
>> > http://www.windwardreports.com
>> >
>> > Program.cs:
>> > using System.Xml;
>> > using System.Xml.XPat h;
>> > using System;
>> > using System.IO;
>> >
>> > namespace XPathSample
>> > {
>> > class Program
>> > {
>> > static void Main(string[] args)
>> > {
>> >
>> > string xml =
>> > "<order>" +
>> > "<product>softw are</product>" +
>> > "<product>softw are</product>" +
>> > "</order>";
>> > XPathDocument doc = new XPathDocument(n ew StringReader(xm l));
>> >
>> > // this works
>> > XPathNavigator nav = doc.CreateNavig ator();
>> > XPathNavigator rtn =
>> > nav.SelectSingl eNode("/order/product[.='software']");
>> > Console.Out.Wri teLine("nav(/) = " + nav.OuterXml);
>> > Console.Out.Wri teLine("/order/product[.='software'] = " +
>> > rtn.OuterXml);
>> >
>> > // this works although the nav.OuterXml appears wrong
>> > nav = nav.SelectSingl eNode("/order");
>> > rtn = nav.SelectSingl eNode("product[.='software']");
>> > Console.Out.Wri teLine("nav(/order) = " + nav.OuterXml);
>> > Console.Out.Wri teLine("product[.='software'] = " + rtn.OuterXml);
>> >
>> > // this works (same as prev)
>> > rtn = nav.SelectSingl eNode("./product[.='software']");
>> > Console.Out.Wri teLine("./product[.='software'] = " + rtn.OuterXml);
>> >
>> > nav = nav.SelectSingl eNode("product" );
>> > Console.Out.Wri teLine("nav(/order/product) = " + nav.OuterXml);
>> >
>> > // these all fail - WHY?
>> > try
>> > {
>> > rtn = nav.SelectSingl eNode("[.='software']");
>> > }
>> > catch (XPathException ex)
>> > {
>> > Console.Out.Wri teLine("[.='software'] exception = " + ex.Message);
>> > }
>> > try
>> > {
>> > rtn = nav.SelectSingl eNode("./[.='software']");
>> > }
>> > catch (XPathException ex)
>> > {
>> > Console.Out.Wri teLine("./[.='software'] exception = " + ex.Message);
>> > }
>> >
>> > // yes this works - but I need the node as it does have a value.
>> > object obj = nav.Evaluate(". ='software'");
>> > Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);
>> >
>> >
>> > Console.Out.Wri teLine("all done");
>> > }
>> > }
>> > }
>> >
>> >
>>
>>
>>


Feb 27 '06 #6
Hello;

Is this correct to say:
"/order/software" or "software" in each case is an axis specifier while "."
is an abbreviated step?

I had thought both were a way of specifing a node and therefore were an
axisSpecifier.

Also, any good book(s) or website(s) that explain this, preferably by example?

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

"Dimitre Novatchev" wrote:

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

First, thanks for helping on this I really appreciate it.

Ok, I see that the xpath must start with "." to get it to the node and the
"[...]" then modifies that - makes sense.

But then, shouldn't ".[.='software']" or "./.[.='software']" or


No, the syntax rules do not allow this:
[4] Step ::= AxisSpecifier NodeTest Predicate*
| AbbreviatedStep


as we can see, Predicatecan only occur after a NodeTest , not after an
AbbreviatedStep .
"./[.='software']" work.


Again, Predicate cannot be the leftmost symbol in a step

In the case of ".[.='software']" isn't that saying start with the node "."
(which exists) and then return all where .='software'?


This may seem a clear semantics, but the syntax is invalid according to the
XPath 1.0 grammar rules.

As I said, use:

self::node()[.='software']

Cheers,
Dimitre Novatchev

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

"Dimitre Novatchev" wrote:

"David Thielen" <da***@bogus.wi ndward.net> wrote in message
news:24******** *************** ***********@mic rosoft.com...
> Hello;
>
> I assumed it is invalid syntax. My question is why? I don't understand
> why
> "[.='software']" or some variation of that is invalid.

Read the XPath 1.0 Spec.

It says:

http://www.w3.org/TR/xpath#predicates

"A predicate filters a node-set with respect to an axis to produce a new
node-set. For each node in the node-set to be filtered, the PredicateExpr
is
evaluated with that node as the context node, with the number of nodes in
the node-set as the context size, and with the proximity position of the
node in the node-set with respect to the axis as the context position; if
PredicateExpr evaluates to true for that node, the node is included in
the
new node-set; otherwise, it is not included."

So, a predicate can only apear after some XPath expression and filters
it. A
predicate, alone, is not a valid XPath expression.

This is also made clear if we look at the BNF grammar definition of XPath
1.0. Of all rules only rule [4] and rule [20] contain the nonterminal
symbol
predicate and it is always not the leading symbol (of the RHS):

http://www.w3.org/TR/xpath#section-Location-Steps
[4] Step ::= AxisSpecifier NodeTest Predicate*
| AbbreviatedStep


http://www.w3.org/TR/xpath#node-sets

[20] FilterExpr ::= PrimaryExpr
| FilterExpr Predicate


I hope that this answers completely your question.
Cheers,
Dimitre Novatchev

>
> --
> thanks - dave
> david_at_windwa rd_dot_net
> http://www.windwardreports.com
>
>
>
> "Dimitre Novatchev" wrote:
>
>> > // these all fail - WHY?
>>
>> because of a common feature -- they all have invalid XPath syntax.
>>
>> > try
>> > {
>> > rtn = nav.SelectSingl eNode("[.='software']");
>> > }
>> > catch (XPathException ex)
>> > {
>> > Console.Out.Wri teLine("[.='software'] exception = " + ex.Message);
>> > }
>> > try
>> > {
>> > rtn = nav.SelectSingl eNode("./[.='software']");
>> > }
>> > catch (XPathException ex)
>> > {
>> > Console.Out.Wri teLine("./[.='software'] exception = " + ex.Message);
>> > }
>> >
>> > // yes this works - but I need the node as it does have a value.
>> > object obj = nav.Evaluate(". ='software'");
>> > Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);
>>
>>
>> Try:
>>
>> "self::*[.='software']"
>>
>> or
>>
>> "string(sel f::*[.='software'])"
>>
>> depending on whether you need the node or its string value returned.
>>
>>
>> Cheers,
>> Dimitre Novatchev
>>
>>
>>
>>
>>
>> "David Thielen" <da***@bogus.wi ndward.net> wrote in message
>> news:74******** *************** ***********@mic rosoft.com...
>> > 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
>> > to handle this. We can't just always require the fill
>> > /order/product[.='software'].)
>> >
>> > Program below sig.
>> >
>> > --
>> > thanks - dave
>> > david_at_windwa rd_dot_net
>> > http://www.windwardreports.com
>> >
>> > Program.cs:
>> > using System.Xml;
>> > using System.Xml.XPat h;
>> > using System;
>> > using System.IO;
>> >
>> > namespace XPathSample
>> > {
>> > class Program
>> > {
>> > static void Main(string[] args)
>> > {
>> >
>> > string xml =
>> > "<order>" +
>> > "<product>softw are</product>" +
>> > "<product>softw are</product>" +
>> > "</order>";
>> > XPathDocument doc = new XPathDocument(n ew StringReader(xm l));
>> >
>> > // this works
>> > XPathNavigator nav = doc.CreateNavig ator();
>> > XPathNavigator rtn =
>> > nav.SelectSingl eNode("/order/product[.='software']");
>> > Console.Out.Wri teLine("nav(/) = " + nav.OuterXml);
>> > Console.Out.Wri teLine("/order/product[.='software'] = " +
>> > rtn.OuterXml);
>> >
>> > // this works although the nav.OuterXml appears wrong
>> > nav = nav.SelectSingl eNode("/order");
>> > rtn = nav.SelectSingl eNode("product[.='software']");
>> > Console.Out.Wri teLine("nav(/order) = " + nav.OuterXml);
>> > Console.Out.Wri teLine("product[.='software'] = " + rtn.OuterXml);
>> >
>> > // this works (same as prev)
>> > rtn = nav.SelectSingl eNode("./product[.='software']");
>> > Console.Out.Wri teLine("./product[.='software'] = " + rtn.OuterXml);
>> >
>> > nav = nav.SelectSingl eNode("product" );
>> > Console.Out.Wri teLine("nav(/order/product) = " + nav.OuterXml);
>> >
>> > // these all fail - WHY?
>> > try
>> > {
>> > rtn = nav.SelectSingl eNode("[.='software']");
>> > }
>> > catch (XPathException ex)
>> > {
>> > Console.Out.Wri teLine("[.='software'] exception = " + ex.Message);
>> > }
>> > try
>> > {
>> > rtn = nav.SelectSingl eNode("./[.='software']");
>> > }
>> > catch (XPathException ex)
>> > {
>> > Console.Out.Wri teLine("./[.='software'] exception = " + ex.Message);
>> > }
>> >
>> > // yes this works - but I need the node as it does have a value.
>> > object obj = nav.Evaluate(". ='software'");
>> > Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);
>> >
>> >
>> > Console.Out.Wri teLine("all done");
>> > }
>> > }
>> > }
>> >
>> >
>>
>>
>>


Feb 27 '06 #7

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

Is this correct to say:
"/order/software" or "software" in each case is an axis specifier
No, Axis specifier can be only a predefined (full) name out of 13 axis names
(see below) or an abbreviated axis specifier (just "@").
while "."
is an abbreviated step?

I had thought both were a way of specifing a node and therefore were an
axisSpecifier.
No.

The rules from the spec:

[5] AxisSpecifier ::= AxisName '::'
| AbbreviatedAxis Specifier
[6] AxisName ::= 'ancestor'
| 'ancestor-or-self'
| 'attribute'
| 'child'
| 'descendant'
| 'descendant-or-self'
| 'following'
| 'following-sibling'
| 'namespace'
| 'parent'
| 'preceding'
| 'preceding-sibling'
| 'self'
[13] AbbreviatedAxis Specifier ::= '@'?


Also, any good book(s) or website(s) that explain this, preferably by
example?
Michael Kay's books on XSLT 1.0, XSLT 2.0 and XPath 2.0
http://www.amazon.com/gp/product/186...lance&n=283155

http://www.amazon.com/gp/product/076...lance&n=283155

http://www.amazon.com/gp/product/076...lance&n=283155
Jeni Tennison's books on the same topics.
http://www.amazon.com/gp/product/159...lance&n=283155

http://www.amazon.com/gp/product/076...lance&n=283155

http://www.amazon.com/gp/product/159...lance&n=283155
Hope this helped.

Cheers,
Dimitre Novatchev.


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

"Dimitre Novatchev" wrote:

"David Thielen" <da***@bogus.wi ndward.net> wrote in message
news:DA******** *************** ***********@mic rosoft.com...
> Hi;
>
> First, thanks for helping on this I really appreciate it.
>
> Ok, I see that the xpath must start with "." to get it to the node and
> the
> "[...]" then modifies that - makes sense.
>
> But then, shouldn't ".[.='software']" or "./.[.='software']" or


No, the syntax rules do not allow this:
>> [4] Step ::= AxisSpecifier NodeTest Predicate*
>> | AbbreviatedStep


as we can see, Predicatecan only occur after a NodeTest , not after an
AbbreviatedStep .
> "./[.='software']" work.


Again, Predicate cannot be the leftmost symbol in a step
>
> In the case of ".[.='software']" isn't that saying start with the node
> "."
> (which exists) and then return all where .='software'?


This may seem a clear semantics, but the syntax is invalid according to
the
XPath 1.0 grammar rules.

As I said, use:

self::node()[.='software']

Cheers,
Dimitre Novatchev
>
> --
> thanks - dave
> david_at_windwa rd_dot_net
> http://www.windwardreports.com
>
>
>
> "Dimitre Novatchev" wrote:
>
>>
>> "David Thielen" <da***@bogus.wi ndward.net> wrote in message
>> news:24******** *************** ***********@mic rosoft.com...
>> > Hello;
>> >
>> > I assumed it is invalid syntax. My question is why? I don't
>> > understand
>> > why
>> > "[.='software']" or some variation of that is invalid.
>>
>> Read the XPath 1.0 Spec.
>>
>> It says:
>>
>> http://www.w3.org/TR/xpath#predicates
>>
>> "A predicate filters a node-set with respect to an axis to produce a
>> new
>> node-set. For each node in the node-set to be filtered, the
>> PredicateExpr
>> is
>> evaluated with that node as the context node, with the number of nodes
>> in
>> the node-set as the context size, and with the proximity position of
>> the
>> node in the node-set with respect to the axis as the context position;
>> if
>> PredicateExpr evaluates to true for that node, the node is included in
>> the
>> new node-set; otherwise, it is not included."
>>
>>
>>
>> So, a predicate can only apear after some XPath expression and filters
>> it. A
>> predicate, alone, is not a valid XPath expression.
>>
>> This is also made clear if we look at the BNF grammar definition of
>> XPath
>> 1.0. Of all rules only rule [4] and rule [20] contain the nonterminal
>> symbol
>> predicate and it is always not the leading symbol (of the RHS):
>>
>>
>>
>> http://www.w3.org/TR/xpath#section-Location-Steps
>>
>>
>> [4] Step ::= AxisSpecifier NodeTest Predicate*
>> | AbbreviatedStep
>>
>>
>>
>>
>> http://www.w3.org/TR/xpath#node-sets
>>
>>
>>
>> [20] FilterExpr ::= PrimaryExpr
>> | FilterExpr Predicate
>>
>>
>>
>>
>> I hope that this answers completely your question.
>>
>>
>> Cheers,
>> Dimitre Novatchev
>>
>>
>>
>> >
>> > --
>> > thanks - dave
>> > david_at_windwa rd_dot_net
>> > http://www.windwardreports.com
>> >
>> >
>> >
>> > "Dimitre Novatchev" wrote:
>> >
>> >> > // these all fail - WHY?
>> >>
>> >> because of a common feature -- they all have invalid XPath syntax.
>> >>
>> >> > try
>> >> > {
>> >> > rtn = nav.SelectSingl eNode("[.='software']");
>> >> > }
>> >> > catch (XPathException ex)
>> >> > {
>> >> > Console.Out.Wri teLine("[.='software'] exception = " +
>> >> > ex.Message);
>> >> > }
>> >> > try
>> >> > {
>> >> > rtn = nav.SelectSingl eNode("./[.='software']");
>> >> > }
>> >> > catch (XPathException ex)
>> >> > {
>> >> > Console.Out.Wri teLine("./[.='software'] exception = " +
>> >> > ex.Message);
>> >> > }
>> >> >
>> >> > // yes this works - but I need the node as it does have a value.
>> >> > object obj = nav.Evaluate(". ='software'");
>> >> > Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);
>> >>
>> >>
>> >> Try:
>> >>
>> >> "self::*[.='software']"
>> >>
>> >> or
>> >>
>> >> "string(sel f::*[.='software'])"
>> >>
>> >> depending on whether you need the node or its string value
>> >> returned.
>> >>
>> >>
>> >> Cheers,
>> >> Dimitre Novatchev
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> "David Thielen" <da***@bogus.wi ndward.net> wrote in message
>> >> news:74******** *************** ***********@mic rosoft.com...
>> >> > 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
>> >> > to handle this. We can't just always require the fill
>> >> > /order/product[.='software'].)
>> >> >
>> >> > Program below sig.
>> >> >
>> >> > --
>> >> > thanks - dave
>> >> > david_at_windwa rd_dot_net
>> >> > http://www.windwardreports.com
>> >> >
>> >> > Program.cs:
>> >> > using System.Xml;
>> >> > using System.Xml.XPat h;
>> >> > using System;
>> >> > using System.IO;
>> >> >
>> >> > namespace XPathSample
>> >> > {
>> >> > class Program
>> >> > {
>> >> > static void Main(string[] args)
>> >> > {
>> >> >
>> >> > string xml =
>> >> > "<order>" +
>> >> > "<product>softw are</product>" +
>> >> > "<product>softw are</product>" +
>> >> > "</order>";
>> >> > XPathDocument doc = new XPathDocument(n ew StringReader(xm l));
>> >> >
>> >> > // this works
>> >> > XPathNavigator nav = doc.CreateNavig ator();
>> >> > XPathNavigator rtn =
>> >> > nav.SelectSingl eNode("/order/product[.='software']");
>> >> > Console.Out.Wri teLine("nav(/) = " + nav.OuterXml);
>> >> > Console.Out.Wri teLine("/order/product[.='software'] = " +
>> >> > rtn.OuterXml);
>> >> >
>> >> > // this works although the nav.OuterXml appears wrong
>> >> > nav = nav.SelectSingl eNode("/order");
>> >> > rtn = nav.SelectSingl eNode("product[.='software']");
>> >> > Console.Out.Wri teLine("nav(/order) = " + nav.OuterXml);
>> >> > Console.Out.Wri teLine("product[.='software'] = " + rtn.OuterXml);
>> >> >
>> >> > // this works (same as prev)
>> >> > rtn = nav.SelectSingl eNode("./product[.='software']");
>> >> > Console.Out.Wri teLine("./product[.='software'] = " +
>> >> > rtn.OuterXml);
>> >> >
>> >> > nav = nav.SelectSingl eNode("product" );
>> >> > Console.Out.Wri teLine("nav(/order/product) = " + nav.OuterXml);
>> >> >
>> >> > // these all fail - WHY?
>> >> > try
>> >> > {
>> >> > rtn = nav.SelectSingl eNode("[.='software']");
>> >> > }
>> >> > catch (XPathException ex)
>> >> > {
>> >> > Console.Out.Wri teLine("[.='software'] exception = " +
>> >> > ex.Message);
>> >> > }
>> >> > try
>> >> > {
>> >> > rtn = nav.SelectSingl eNode("./[.='software']");
>> >> > }
>> >> > catch (XPathException ex)
>> >> > {
>> >> > Console.Out.Wri teLine("./[.='software'] exception = " +
>> >> > ex.Message);
>> >> > }
>> >> >
>> >> > // yes this works - but I need the node as it does have a value.
>> >> > object obj = nav.Evaluate(". ='software'");
>> >> > Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);
>> >> >
>> >> >
>> >> > Console.Out.Wri teLine("all done");
>> >> > }
>> >> > }
>> >> > }
>> >> >
>> >> >
>> >>
>> >>
>> >>
>>
>>
>>
>>


Feb 28 '06 #8
> Also, any good book(s) or website(s) that explain this, preferably by
example?
I would recommend the XPath Visualizer as a proven tool for playing with
XPath and learning it "the fun way".

Cheers,
Dimitre Novatchev

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

Is this correct to say:
"/order/software" or "software" in each case is an axis specifier while
"."
is an abbreviated step?

I had thought both were a way of specifing a node and therefore were an
axisSpecifier.

Also, any good book(s) or website(s) that explain this, preferably by
example?

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

"Dimitre Novatchev" wrote:

"David Thielen" <da***@bogus.wi ndward.net> wrote in message
news:DA******** *************** ***********@mic rosoft.com...
> Hi;
>
> First, thanks for helping on this I really appreciate it.
>
> Ok, I see that the xpath must start with "." to get it to the node and
> the
> "[...]" then modifies that - makes sense.
>
> But then, shouldn't ".[.='software']" or "./.[.='software']" or


No, the syntax rules do not allow this:
>> [4] Step ::= AxisSpecifier NodeTest Predicate*
>> | AbbreviatedStep


as we can see, Predicatecan only occur after a NodeTest , not after an
AbbreviatedStep .
> "./[.='software']" work.


Again, Predicate cannot be the leftmost symbol in a step
>
> In the case of ".[.='software']" isn't that saying start with the node
> "."
> (which exists) and then return all where .='software'?


This may seem a clear semantics, but the syntax is invalid according to
the
XPath 1.0 grammar rules.

As I said, use:

self::node()[.='software']

Cheers,
Dimitre Novatchev
>
> --
> thanks - dave
> david_at_windwa rd_dot_net
> http://www.windwardreports.com
>
>
>
> "Dimitre Novatchev" wrote:
>
>>
>> "David Thielen" <da***@bogus.wi ndward.net> wrote in message
>> news:24******** *************** ***********@mic rosoft.com...
>> > Hello;
>> >
>> > I assumed it is invalid syntax. My question is why? I don't
>> > understand
>> > why
>> > "[.='software']" or some variation of that is invalid.
>>
>> Read the XPath 1.0 Spec.
>>
>> It says:
>>
>> http://www.w3.org/TR/xpath#predicates
>>
>> "A predicate filters a node-set with respect to an axis to produce a
>> new
>> node-set. For each node in the node-set to be filtered, the
>> PredicateExpr
>> is
>> evaluated with that node as the context node, with the number of nodes
>> in
>> the node-set as the context size, and with the proximity position of
>> the
>> node in the node-set with respect to the axis as the context position;
>> if
>> PredicateExpr evaluates to true for that node, the node is included in
>> the
>> new node-set; otherwise, it is not included."
>>
>>
>>
>> So, a predicate can only apear after some XPath expression and filters
>> it. A
>> predicate, alone, is not a valid XPath expression.
>>
>> This is also made clear if we look at the BNF grammar definition of
>> XPath
>> 1.0. Of all rules only rule [4] and rule [20] contain the nonterminal
>> symbol
>> predicate and it is always not the leading symbol (of the RHS):
>>
>>
>>
>> http://www.w3.org/TR/xpath#section-Location-Steps
>>
>>
>> [4] Step ::= AxisSpecifier NodeTest Predicate*
>> | AbbreviatedStep
>>
>>
>>
>>
>> http://www.w3.org/TR/xpath#node-sets
>>
>>
>>
>> [20] FilterExpr ::= PrimaryExpr
>> | FilterExpr Predicate
>>
>>
>>
>>
>> I hope that this answers completely your question.
>>
>>
>> Cheers,
>> Dimitre Novatchev
>>
>>
>>
>> >
>> > --
>> > thanks - dave
>> > david_at_windwa rd_dot_net
>> > http://www.windwardreports.com
>> >
>> >
>> >
>> > "Dimitre Novatchev" wrote:
>> >
>> >> > // these all fail - WHY?
>> >>
>> >> because of a common feature -- they all have invalid XPath syntax.
>> >>
>> >> > try
>> >> > {
>> >> > rtn = nav.SelectSingl eNode("[.='software']");
>> >> > }
>> >> > catch (XPathException ex)
>> >> > {
>> >> > Console.Out.Wri teLine("[.='software'] exception = " +
>> >> > ex.Message);
>> >> > }
>> >> > try
>> >> > {
>> >> > rtn = nav.SelectSingl eNode("./[.='software']");
>> >> > }
>> >> > catch (XPathException ex)
>> >> > {
>> >> > Console.Out.Wri teLine("./[.='software'] exception = " +
>> >> > ex.Message);
>> >> > }
>> >> >
>> >> > // yes this works - but I need the node as it does have a value.
>> >> > object obj = nav.Evaluate(". ='software'");
>> >> > Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);
>> >>
>> >>
>> >> Try:
>> >>
>> >> "self::*[.='software']"
>> >>
>> >> or
>> >>
>> >> "string(sel f::*[.='software'])"
>> >>
>> >> depending on whether you need the node or its string value
>> >> returned.
>> >>
>> >>
>> >> Cheers,
>> >> Dimitre Novatchev
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> "David Thielen" <da***@bogus.wi ndward.net> wrote in message
>> >> news:74******** *************** ***********@mic rosoft.com...
>> >> > 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
>> >> > to handle this. We can't just always require the fill
>> >> > /order/product[.='software'].)
>> >> >
>> >> > Program below sig.
>> >> >
>> >> > --
>> >> > thanks - dave
>> >> > david_at_windwa rd_dot_net
>> >> > http://www.windwardreports.com
>> >> >
>> >> > Program.cs:
>> >> > using System.Xml;
>> >> > using System.Xml.XPat h;
>> >> > using System;
>> >> > using System.IO;
>> >> >
>> >> > namespace XPathSample
>> >> > {
>> >> > class Program
>> >> > {
>> >> > static void Main(string[] args)
>> >> > {
>> >> >
>> >> > string xml =
>> >> > "<order>" +
>> >> > "<product>softw are</product>" +
>> >> > "<product>softw are</product>" +
>> >> > "</order>";
>> >> > XPathDocument doc = new XPathDocument(n ew StringReader(xm l));
>> >> >
>> >> > // this works
>> >> > XPathNavigator nav = doc.CreateNavig ator();
>> >> > XPathNavigator rtn =
>> >> > nav.SelectSingl eNode("/order/product[.='software']");
>> >> > Console.Out.Wri teLine("nav(/) = " + nav.OuterXml);
>> >> > Console.Out.Wri teLine("/order/product[.='software'] = " +
>> >> > rtn.OuterXml);
>> >> >
>> >> > // this works although the nav.OuterXml appears wrong
>> >> > nav = nav.SelectSingl eNode("/order");
>> >> > rtn = nav.SelectSingl eNode("product[.='software']");
>> >> > Console.Out.Wri teLine("nav(/order) = " + nav.OuterXml);
>> >> > Console.Out.Wri teLine("product[.='software'] = " + rtn.OuterXml);
>> >> >
>> >> > // this works (same as prev)
>> >> > rtn = nav.SelectSingl eNode("./product[.='software']");
>> >> > Console.Out.Wri teLine("./product[.='software'] = " +
>> >> > rtn.OuterXml);
>> >> >
>> >> > nav = nav.SelectSingl eNode("product" );
>> >> > Console.Out.Wri teLine("nav(/order/product) = " + nav.OuterXml);
>> >> >
>> >> > // these all fail - WHY?
>> >> > try
>> >> > {
>> >> > rtn = nav.SelectSingl eNode("[.='software']");
>> >> > }
>> >> > catch (XPathException ex)
>> >> > {
>> >> > Console.Out.Wri teLine("[.='software'] exception = " +
>> >> > ex.Message);
>> >> > }
>> >> > try
>> >> > {
>> >> > rtn = nav.SelectSingl eNode("./[.='software']");
>> >> > }
>> >> > catch (XPathException ex)
>> >> > {
>> >> > Console.Out.Wri teLine("./[.='software'] exception = " +
>> >> > ex.Message);
>> >> > }
>> >> >
>> >> > // yes this works - but I need the node as it does have a value.
>> >> > object obj = nav.Evaluate(". ='software'");
>> >> > Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);
>> >> >
>> >> >
>> >> > Console.Out.Wri teLine("all done");
>> >> > }
>> >> > }
>> >> > }
>> >> >
>> >> >
>> >>
>> >>
>> >>
>>
>>
>>
>>


Feb 28 '06 #9
> Also, any good book(s) or website(s) that explain this, preferably by
example?
I would recommend the XPath Visualizer as a proven tool for playing with
XPath and learning it "the fun way".

Cheers,
Dimitre Novatchev

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

Is this correct to say:
"/order/software" or "software" in each case is an axis specifier while
"."
is an abbreviated step?

I had thought both were a way of specifing a node and therefore were an
axisSpecifier.

Also, any good book(s) or website(s) that explain this, preferably by
example?

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

"Dimitre Novatchev" wrote:

"David Thielen" <da***@bogus.wi ndward.net> wrote in message
news:DA******** *************** ***********@mic rosoft.com...
> Hi;
>
> First, thanks for helping on this I really appreciate it.
>
> Ok, I see that the xpath must start with "." to get it to the node and
> the
> "[...]" then modifies that - makes sense.
>
> But then, shouldn't ".[.='software']" or "./.[.='software']" or


No, the syntax rules do not allow this:
>> [4] Step ::= AxisSpecifier NodeTest Predicate*
>> | AbbreviatedStep


as we can see, Predicatecan only occur after a NodeTest , not after an
AbbreviatedStep .
> "./[.='software']" work.


Again, Predicate cannot be the leftmost symbol in a step
>
> In the case of ".[.='software']" isn't that saying start with the node
> "."
> (which exists) and then return all where .='software'?


This may seem a clear semantics, but the syntax is invalid according to
the
XPath 1.0 grammar rules.

As I said, use:

self::node()[.='software']

Cheers,
Dimitre Novatchev
>
> --
> thanks - dave
> david_at_windwa rd_dot_net
> http://www.windwardreports.com
>
>
>
> "Dimitre Novatchev" wrote:
>
>>
>> "David Thielen" <da***@bogus.wi ndward.net> wrote in message
>> news:24******** *************** ***********@mic rosoft.com...
>> > Hello;
>> >
>> > I assumed it is invalid syntax. My question is why? I don't
>> > understand
>> > why
>> > "[.='software']" or some variation of that is invalid.
>>
>> Read the XPath 1.0 Spec.
>>
>> It says:
>>
>> http://www.w3.org/TR/xpath#predicates
>>
>> "A predicate filters a node-set with respect to an axis to produce a
>> new
>> node-set. For each node in the node-set to be filtered, the
>> PredicateExpr
>> is
>> evaluated with that node as the context node, with the number of nodes
>> in
>> the node-set as the context size, and with the proximity position of
>> the
>> node in the node-set with respect to the axis as the context position;
>> if
>> PredicateExpr evaluates to true for that node, the node is included in
>> the
>> new node-set; otherwise, it is not included."
>>
>>
>>
>> So, a predicate can only apear after some XPath expression and filters
>> it. A
>> predicate, alone, is not a valid XPath expression.
>>
>> This is also made clear if we look at the BNF grammar definition of
>> XPath
>> 1.0. Of all rules only rule [4] and rule [20] contain the nonterminal
>> symbol
>> predicate and it is always not the leading symbol (of the RHS):
>>
>>
>>
>> http://www.w3.org/TR/xpath#section-Location-Steps
>>
>>
>> [4] Step ::= AxisSpecifier NodeTest Predicate*
>> | AbbreviatedStep
>>
>>
>>
>>
>> http://www.w3.org/TR/xpath#node-sets
>>
>>
>>
>> [20] FilterExpr ::= PrimaryExpr
>> | FilterExpr Predicate
>>
>>
>>
>>
>> I hope that this answers completely your question.
>>
>>
>> Cheers,
>> Dimitre Novatchev
>>
>>
>>
>> >
>> > --
>> > thanks - dave
>> > david_at_windwa rd_dot_net
>> > http://www.windwardreports.com
>> >
>> >
>> >
>> > "Dimitre Novatchev" wrote:
>> >
>> >> > // these all fail - WHY?
>> >>
>> >> because of a common feature -- they all have invalid XPath syntax.
>> >>
>> >> > try
>> >> > {
>> >> > rtn = nav.SelectSingl eNode("[.='software']");
>> >> > }
>> >> > catch (XPathException ex)
>> >> > {
>> >> > Console.Out.Wri teLine("[.='software'] exception = " +
>> >> > ex.Message);
>> >> > }
>> >> > try
>> >> > {
>> >> > rtn = nav.SelectSingl eNode("./[.='software']");
>> >> > }
>> >> > catch (XPathException ex)
>> >> > {
>> >> > Console.Out.Wri teLine("./[.='software'] exception = " +
>> >> > ex.Message);
>> >> > }
>> >> >
>> >> > // yes this works - but I need the node as it does have a value.
>> >> > object obj = nav.Evaluate(". ='software'");
>> >> > Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);
>> >>
>> >>
>> >> Try:
>> >>
>> >> "self::*[.='software']"
>> >>
>> >> or
>> >>
>> >> "string(sel f::*[.='software'])"
>> >>
>> >> depending on whether you need the node or its string value
>> >> returned.
>> >>
>> >>
>> >> Cheers,
>> >> Dimitre Novatchev
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> "David Thielen" <da***@bogus.wi ndward.net> wrote in message
>> >> news:74******** *************** ***********@mic rosoft.com...
>> >> > 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
>> >> > to handle this. We can't just always require the fill
>> >> > /order/product[.='software'].)
>> >> >
>> >> > Program below sig.
>> >> >
>> >> > --
>> >> > thanks - dave
>> >> > david_at_windwa rd_dot_net
>> >> > http://www.windwardreports.com
>> >> >
>> >> > Program.cs:
>> >> > using System.Xml;
>> >> > using System.Xml.XPat h;
>> >> > using System;
>> >> > using System.IO;
>> >> >
>> >> > namespace XPathSample
>> >> > {
>> >> > class Program
>> >> > {
>> >> > static void Main(string[] args)
>> >> > {
>> >> >
>> >> > string xml =
>> >> > "<order>" +
>> >> > "<product>softw are</product>" +
>> >> > "<product>softw are</product>" +
>> >> > "</order>";
>> >> > XPathDocument doc = new XPathDocument(n ew StringReader(xm l));
>> >> >
>> >> > // this works
>> >> > XPathNavigator nav = doc.CreateNavig ator();
>> >> > XPathNavigator rtn =
>> >> > nav.SelectSingl eNode("/order/product[.='software']");
>> >> > Console.Out.Wri teLine("nav(/) = " + nav.OuterXml);
>> >> > Console.Out.Wri teLine("/order/product[.='software'] = " +
>> >> > rtn.OuterXml);
>> >> >
>> >> > // this works although the nav.OuterXml appears wrong
>> >> > nav = nav.SelectSingl eNode("/order");
>> >> > rtn = nav.SelectSingl eNode("product[.='software']");
>> >> > Console.Out.Wri teLine("nav(/order) = " + nav.OuterXml);
>> >> > Console.Out.Wri teLine("product[.='software'] = " + rtn.OuterXml);
>> >> >
>> >> > // this works (same as prev)
>> >> > rtn = nav.SelectSingl eNode("./product[.='software']");
>> >> > Console.Out.Wri teLine("./product[.='software'] = " +
>> >> > rtn.OuterXml);
>> >> >
>> >> > nav = nav.SelectSingl eNode("product" );
>> >> > Console.Out.Wri teLine("nav(/order/product) = " + nav.OuterXml);
>> >> >
>> >> > // these all fail - WHY?
>> >> > try
>> >> > {
>> >> > rtn = nav.SelectSingl eNode("[.='software']");
>> >> > }
>> >> > catch (XPathException ex)
>> >> > {
>> >> > Console.Out.Wri teLine("[.='software'] exception = " +
>> >> > ex.Message);
>> >> > }
>> >> > try
>> >> > {
>> >> > rtn = nav.SelectSingl eNode("./[.='software']");
>> >> > }
>> >> > catch (XPathException ex)
>> >> > {
>> >> > Console.Out.Wri teLine("./[.='software'] exception = " +
>> >> > ex.Message);
>> >> > }
>> >> >
>> >> > // yes this works - but I need the node as it does have a value.
>> >> > object obj = nav.Evaluate(". ='software'");
>> >> > Console.Out.Wri teLine(".='soft ware' evaluate = " + obj);
>> >> >
>> >> >
>> >> > Console.Out.Wri teLine("all done");
>> >> > }
>> >> > }
>> >> > }
>> >> >
>> >> >
>> >>
>> >>
>> >>
>>
>>
>>
>>



Feb 28 '06 #10

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

Similar topics

14
3013
by: Dave Murray | last post by:
New to Python question, why does this fail? Thanks, Dave ---testcase.py--- import sys, urllib, htmllib def Checkit(URL): try: print "Opening", URL
2
1941
by: Curantil | last post by:
When I look at all the examples every selection like select="child::item" has a constant value to test against ("3" in this case). Can't this be done with a variable? eg: select="child::item" Because I'm trying to create a structure like this... usecase ->item(s) (kind of ordered list) ->alt (with @number parameter)
1
1336
by: Keith Davies | last post by:
Hi All, I've got a document containing a internal lookups (I want to show a relationship hierarchy, starting at an arbitrary point, or series of points). It's easy if I know what item IDs I want to refer to during processing, I can put in a @refid, @refids, or child elements listing the IDs to process. No big deal. I'd *rather*,...
3
1730
by: gimme_this_gimme_that | last post by:
I once downloaded a shareware program that allowed you to open an xml file, click on a text or an attribute, an then see the xpath expression that would fetch that data. The program didn't require that you enter the xpath expression. It may have been something that worked from opening a page in IE. That would be OK. It could have been
13
3220
by: David Thielen | last post by:
XPathNavigator nav = MyCreateNav(); // InnerXml == "software" nav.SelectSingleNode"."); The select returns an exception: + $exception {"'.' has an invalid token."} System.Exception {System.Xml.XPath.XPathException} Any idea why? --
3
3455
by: marfi95 | last post by:
I'm just starting to learn xpath, so if I have the following xml <Root> <Data 1> <Child 1></Child 1> <Child 2></Child 2> </Data 1> <Data 2> <Child 1></Child 1> <Child 2></Child 2>
6
7281
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! ...
14
16872
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
7465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7416
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5969
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...
1
5325
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
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...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.