XPath Question

M

Michael C#

OK, here's the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far so good.
I run an XPath query against it to retrieve all the field names. Everything
there works fine.

Here's my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I'm trying to come up with three XPath queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
3) All tblItem ndoes that have the word Remote in the Description (i.e., all
tblItem where Category contains the word 'Remote' in any position)

Coming from a SQL background, I'm having a hard time implementing XPath
expressions. I was hoping someone here could point me in the right
direction. I've tried several combinations, like "tblItem/ID[.='1']",
"tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of them seem to
be working though...

TIA
 
N

Nick Malik [Microsoft]

your first two queries are not difficult:
1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem where ID = 1),
//tblItem[ID=1]

2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
//tblItem[Category='Miscellaneous']

3) All tblItem ndoes that have the word Remote in the Description (i.e., all
tblItem where Category contains the word 'Remote' in any position)
I don't know this one.


One thing that you have to keep in mind, if you want to do this in code, is
that you have to set the namespace manager for the xpath query to include
the defined namespaces. In your example, you have a default namespace. If
you don't provide the namespace manager, then your query will ALWAYS return
zero results.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Michael C# said:
OK, here's the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far so good.
I run an XPath query against it to retrieve all the field names. Everything
there works fine.

Here's my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I'm trying to come up with three XPath queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
3) All tblItem ndoes that have the word Remote in the Description (i.e., all
tblItem where Category contains the word 'Remote' in any position)

Coming from a SQL background, I'm having a hard time implementing XPath
expressions. I was hoping someone here could point me in the right
direction. I've tried several combinations, like "tblItem/ID[.='1']",
"tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of them seem to
be working though...

TIA
 
N

Nick Malik [Microsoft]

one more thing: a good link with a tutorial on XPath queries is:
http://www.w3schools.com/xpath/xpath_syntax.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Nick Malik said:
your first two queries are not difficult:
1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem where ID = 1),
//tblItem[ID=1]

2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
//tblItem[Category='Miscellaneous']

3) All tblItem ndoes that have the word Remote in the Description (i.e., all
tblItem where Category contains the word 'Remote' in any position)
I don't know this one.


One thing that you have to keep in mind, if you want to do this in code, is
that you have to set the namespace manager for the xpath query to include
the defined namespaces. In your example, you have a default namespace. If
you don't provide the namespace manager, then your query will ALWAYS return
zero results.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Michael C# said:
OK, here's the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far so good.
I run an XPath query against it to retrieve all the field names. Everything
there works fine.

Here's my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I'm trying to come up with three XPath queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
3) All tblItem ndoes that have the word Remote in the Description (i.e., all
tblItem where Category contains the word 'Remote' in any position)

Coming from a SQL background, I'm having a hard time implementing XPath
expressions. I was hoping someone here could point me in the right
direction. I've tried several combinations, like "tblItem/ID[.='1']",
"tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of them
seem
to
be working though...

TIA
 
G

Guest

Hi Michael,

Since you have elements instead of attributes in your XML, your xpath query
i.e. tblItem[ID='Miscellaneous'] will not work since ID in this case is a
element and not a attribute. This type of XPath query can be done only for
attributes.
To make it work, you need to use the text() function. the XPath query for
your three scenarios are as follows,

1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem where ID = 1)

/DataSet1/tblItem//ID[text()='1']

2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')

/DataSet1/tblItem//Category[text()='Miscellaneous']

3) All tblItem ndoes that have the word Remote in the Description (i.e., all
tblItem where Category contains the word 'Remote' in any position)

/DataSet1/tblItem//Description[contains(text(),'Remote')]

For more information abt Xpath queries... look at the below url...

<http://www.developer.com/net/net/article.php/11087_3383961_1>

HTH

Regards,
Madhu

MVP - C# | MCSD.NET
 
N

Nick Malik [Microsoft]

The two queries I posted worked for me, using XPath Xpress (downloaded off
of gotdotnet.com)
//tblItem[Category='Miscellaneous']
this one works as long as the namespace is right.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Madhu said:
Hi Michael,

Since you have elements instead of attributes in your XML, your xpath query
i.e. tblItem[ID='Miscellaneous'] will not work since ID in this case is a
element and not a attribute. This type of XPath query can be done only for
attributes.
To make it work, you need to use the text() function. the XPath query for
your three scenarios are as follows,

1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem where ID = 1)

/DataSet1/tblItem//ID[text()='1']

2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')

/DataSet1/tblItem//Category[text()='Miscellaneous']

3) All tblItem ndoes that have the word Remote in the Description (i.e., all
tblItem where Category contains the word 'Remote' in any position)

/DataSet1/tblItem//Description[contains(text(),'Remote')]

For more information abt Xpath queries... look at the below url...

<http://www.developer.com/net/net/article.php/11087_3383961_1>

HTH

Regards,
Madhu

MVP - C# | MCSD.NET

Michael C# said:
OK, here's the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far so good.
I run an XPath query against it to retrieve all the field names. Everything
there works fine.

Here's my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I'm trying to come up with three XPath queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
3) All tblItem ndoes that have the word Remote in the Description (i.e., all
tblItem where Category contains the word 'Remote' in any position)

Coming from a SQL background, I'm having a hard time implementing XPath
expressions. I was hoping someone here could point me in the right
direction. I've tried several combinations, like "tblItem/ID[.='1']",
"tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of them seem to
be working though...

TIA
 
M

Michael C

Now that sounds like it hit the nail on the head. Where can I find out more
about the namespace manager?

Thanks

Nick Malik said:
one more thing: a good link with a tutorial on XPath queries is:
http://www.w3schools.com/xpath/xpath_syntax.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Nick Malik said:
your first two queries are not difficult:
1) All tblItem nodes that have a child ID node with a value of 1
(i.e.,
all
tblItem where ID = 1),
//tblItem[ID=1]

2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
//tblItem[Category='Miscellaneous']

3) All tblItem ndoes that have the word Remote in the Description
(i.e.,
all
tblItem where Category contains the word 'Remote' in any position)
I don't know this one.


One thing that you have to keep in mind, if you want to do this in code, is
that you have to set the namespace manager for the xpath query to include
the defined namespaces. In your example, you have a default namespace. If
you don't provide the namespace manager, then your query will ALWAYS return
zero results.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Michael C# said:
OK, here's the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far so good.
I run an XPath query against it to retrieve all the field names. Everything
there works fine.

Here's my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I'm trying to come up with three XPath queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1
(i.e.,
all
tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
3) All tblItem ndoes that have the word Remote in the Description
(i.e.,
all
tblItem where Category contains the word 'Remote' in any position)

Coming from a SQL background, I'm having a hard time implementing XPath
expressions. I was hoping someone here could point me in the right
direction. I've tried several combinations, like "tblItem/ID[.='1']",
"tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of them
seem
to
be working though...

TIA
 
N

Nick Malik [Microsoft]

try these:
http://msdn.microsoft.com/library/e...nManageNamespacesUsingXmlNamespaceManager.asp
http://msdn.microsoft.com/library/en-us/dnxmlnet/html/xpthviewer.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Michael C said:
Now that sounds like it hit the nail on the head. Where can I find out more
about the namespace manager?

Thanks

Nick Malik said:
one more thing: a good link with a tutorial on XPath queries is:
http://www.w3schools.com/xpath/xpath_syntax.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Nick Malik said:
your first two queries are not difficult:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e.,
all
tblItem where ID = 1),

//tblItem[ID=1]

2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')

//tblItem[Category='Miscellaneous']

3) All tblItem ndoes that have the word Remote in the Description (i.e.,
all
tblItem where Category contains the word 'Remote' in any position)

I don't know this one.


One thing that you have to keep in mind, if you want to do this in
code,
is
that you have to set the namespace manager for the xpath query to include
the defined namespaces. In your example, you have a default
namespace.
If
you don't provide the namespace manager, then your query will ALWAYS return
zero results.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
OK, here's the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far so good.
I run an XPath query against it to retrieve all the field names.
Everything
there works fine.

Here's my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I'm trying to come up with three XPath queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e.,
all
tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
3) All tblItem ndoes that have the word Remote in the Description (i.e.,
all
tblItem where Category contains the word 'Remote' in any position)

Coming from a SQL background, I'm having a hard time implementing XPath
expressions. I was hoping someone here could point me in the right
direction. I've tried several combinations, like "tblItem/ID[.='1']",
"tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of them seem
to
be working though...

TIA
 
M

Michael C#

Hey Nick,

Based on my previous XML I tried the following code:

Dim XDoc As New System.Xml.XmlDocument
Dim XPathQuery As String = "//tblItem[Category='Miscellaneous']"
XDoc.Load(Server.MapPath("StoreItems.xml"))
Dim XMLResults As System.Xml.XmlNodeList = XDoc.SelectNodes(XPathQuery)

XMLResults consistently returns 0 nodes, regardless of the XPathQuery value
I pass to it. Any ideas why it's returning nothing? I started playing with
a Namespace Manager also... I'm using the default namespace, do I really
need it? I also tried the queries posted by Madhu with the same results.

Thanks

Nick Malik said:
try these:
http://msdn.microsoft.com/library/e...nManageNamespacesUsingXmlNamespaceManager.asp
http://msdn.microsoft.com/library/en-us/dnxmlnet/html/xpthviewer.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Michael C said:
Now that sounds like it hit the nail on the head. Where can I find out more
about the namespace manager?

Thanks

Nick Malik said:
one more thing: a good link with a tutorial on XPath queries is:
http://www.w3schools.com/xpath/xpath_syntax.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
message
your first two queries are not difficult:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e.,
all
tblItem where ID = 1),

//tblItem[ID=1]

2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')

//tblItem[Category='Miscellaneous']

3) All tblItem ndoes that have the word Remote in the Description (i.e.,
all
tblItem where Category contains the word 'Remote' in any position)

I don't know this one.


One thing that you have to keep in mind, if you want to do this in code,
is
that you have to set the namespace manager for the xpath query to include
the defined namespaces. In your example, you have a default namespace.
If
you don't provide the namespace manager, then your query will ALWAYS
return
zero results.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
OK, here's the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far
so
good.
I run an XPath query against it to retrieve all the field names.
Everything
there works fine.

Here's my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I'm trying to come up with three XPath queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e.,
all
tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
3) All tblItem ndoes that have the word Remote in the Description (i.e.,
all
tblItem where Category contains the word 'Remote' in any position)

Coming from a SQL background, I'm having a hard time implementing XPath
expressions. I was hoping someone here could point me in the right
direction. I've tried several combinations, like "tblItem/ID[.='1']",
"tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of
them
seem
to
be working though...

TIA
 
M

Michael C#

Oops, that's VB.NET. Here's the C#.NET version:

System.Xml.XmlDocument XDoc = New System.Xml.XmlDocument();
string XPathQuery = "//tblItem[Category='Miscellaneous']";
XDoc.Load(Server.MapPath("StoreItems.xml"));
System.Xml.XmlNodeList XMLResults = XDoc.SelectNodes(XPathQuery);

Thanks

Nick Malik said:
try these:
http://msdn.microsoft.com/library/e...nManageNamespacesUsingXmlNamespaceManager.asp
http://msdn.microsoft.com/library/en-us/dnxmlnet/html/xpthviewer.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Michael C said:
Now that sounds like it hit the nail on the head. Where can I find out more
about the namespace manager?

Thanks

Nick Malik said:
one more thing: a good link with a tutorial on XPath queries is:
http://www.w3schools.com/xpath/xpath_syntax.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
message
your first two queries are not difficult:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e.,
all
tblItem where ID = 1),

//tblItem[ID=1]

2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')

//tblItem[Category='Miscellaneous']

3) All tblItem ndoes that have the word Remote in the Description (i.e.,
all
tblItem where Category contains the word 'Remote' in any position)

I don't know this one.


One thing that you have to keep in mind, if you want to do this in code,
is
that you have to set the namespace manager for the xpath query to include
the defined namespaces. In your example, you have a default namespace.
If
you don't provide the namespace manager, then your query will ALWAYS
return
zero results.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
OK, here's the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far
so
good.
I run an XPath query against it to retrieve all the field names.
Everything
there works fine.

Here's my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I'm trying to come up with three XPath queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e.,
all
tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
3) All tblItem ndoes that have the word Remote in the Description (i.e.,
all
tblItem where Category contains the word 'Remote' in any position)

Coming from a SQL background, I'm having a hard time implementing XPath
expressions. I was hoping someone here could point me in the right
direction. I've tried several combinations, like "tblItem/ID[.='1']",
"tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of
them
seem
to
be working though...

TIA
 
M

Michael C#

Hey I think I got it. Looks like I need to prefix *everything* with the
namespace; i.e.,

string XPathQuery = "//ns:tblItem/ns:Category[text()='Miscellaneous']";

Looks like it's a combination of both your great info. Thanks guys!


Michael C# said:
Oops, that's VB.NET. Here's the C#.NET version:

System.Xml.XmlDocument XDoc = New System.Xml.XmlDocument();
string XPathQuery = "//tblItem[Category='Miscellaneous']";
XDoc.Load(Server.MapPath("StoreItems.xml"));
System.Xml.XmlNodeList XMLResults = XDoc.SelectNodes(XPathQuery);

Thanks

Nick Malik said:
try these:
http://msdn.microsoft.com/library/e...nManageNamespacesUsingXmlNamespaceManager.asp
http://msdn.microsoft.com/library/en-us/dnxmlnet/html/xpthviewer.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Michael C said:
Now that sounds like it hit the nail on the head. Where can I find out more
about the namespace manager?

Thanks

one more thing: a good link with a tutorial on XPath queries is:
http://www.w3schools.com/xpath/xpath_syntax.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
message
your first two queries are not difficult:

1) All tblItem nodes that have a child ID node with a value of 1
(i.e.,
all
tblItem where ID = 1),

//tblItem[ID=1]

2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')

//tblItem[Category='Miscellaneous']

3) All tblItem ndoes that have the word Remote in the Description
(i.e.,
all
tblItem where Category contains the word 'Remote' in any position)

I don't know this one.


One thing that you have to keep in mind, if you want to do this in code,
is
that you have to set the namespace manager for the xpath query to
include
the defined namespaces. In your example, you have a default namespace.
If
you don't provide the namespace manager, then your query will ALWAYS
return
zero results.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
OK, here's the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far
so
good.
I run an XPath query against it to retrieve all the field names.
Everything
there works fine.

Here's my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I'm trying to come up with three XPath
queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1
(i.e.,
all
tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
3) All tblItem ndoes that have the word Remote in the Description
(i.e.,
all
tblItem where Category contains the word 'Remote' in any position)

Coming from a SQL background, I'm having a hard time implementing
XPath
expressions. I was hoping someone here could point me in the
right
direction. I've tried several combinations, like "tblItem/ID[.='1']",
"tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of
them
seem
to
be working though...

TIA
 
M

Mike Schilling

Nick Malik said:
your first two queries are not difficult:
1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem where ID = 1),
//tblItem[ID=1]

2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
//tblItem[Category='Miscellaneous']

3) All tblItem ndoes that have the word Remote in the Description (i.e., all
tblItem where Category contains the word 'Remote' in any position)

I'm guessing that should read "where Description contains the word 'Remote'
in any position"

If so, it's

//tbItem[contains(Description, "Remote")]

By the way, the XPath 1.0 spec is simple and readable. You can find it at
http://www.w3.org/TR/xpath .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top