473,382 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Xpath On selected node??

Hi,
From the sample xml, I am trying to select a book and then select the

author of the select book.

In the example code first SelectSingleNode selects a book. Second
SelectSingleNode on the book node should return the author of the
selected book (???). But I am getting the first author in the document
(Margaret Atwood), not the author of the selected book (Jane Austen).

Thanks.
Sample Code:
-----------------------------------------------
using System;
using System.IO;
using System.Xml;

public class Sample
{
public static void Main()
{

XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml");

//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new
XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:samples");

//Select the book node with the matching attribute value.
XmlNode book, author;
XmlElement root = doc.DocumentElement;
book =
root.SelectSingleNode("/descendant::book[@bk:ISBN='1-861001-57-6']",
nsmgr);
Console.WriteLine(book.OuterXml);

author = book.SelectSingleNode("/descendant::author");

Console.WriteLine(author.OuterXml);
}
-----------------------------------------------
Sample XML:
-----------------------------------------------
<?xml version="1.0" ?>
<!-- a fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>

<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
</bookstore>

Nov 16 '05 #1
2 3466
probashi,

I believe you want to use the expression "descendant::author". I
believe using the slash bumps the expression back to the root of the
document.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"probashi" <pr******@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hi,
From the sample xml, I am trying to select a book and then select the

author of the select book.

In the example code first SelectSingleNode selects a book. Second
SelectSingleNode on the book node should return the author of the
selected book (???). But I am getting the first author in the document
(Margaret Atwood), not the author of the selected book (Jane Austen).

Thanks.
Sample Code:
-----------------------------------------------
using System;
using System.IO;
using System.Xml;

public class Sample
{
public static void Main()
{

XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml");

//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new
XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:samples");

//Select the book node with the matching attribute value.
XmlNode book, author;
XmlElement root = doc.DocumentElement;
book =
root.SelectSingleNode("/descendant::book[@bk:ISBN='1-861001-57-6']",
nsmgr);
Console.WriteLine(book.OuterXml);

author = book.SelectSingleNode("/descendant::author");

Console.WriteLine(author.OuterXml);
}
-----------------------------------------------
Sample XML:
-----------------------------------------------
<?xml version="1.0" ?>
<!-- a fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>

<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
</bookstore>

Nov 16 '05 #2
"probashi" <pr******@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hi,
From the sample xml, I am trying to select a book and then select the

author of the select book.

In the example code first SelectSingleNode selects a book. Second
SelectSingleNode on the book node should return the author of the
selected book (???). But I am getting the first author in the document
(Margaret Atwood), not the author of the selected book (Jane Austen).

Thanks.
Sample Code:
-----------------------------------------------
using System;
using System.IO;
using System.Xml;

public class Sample
{
public static void Main()
{

XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml");

//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new
XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:samples");

//Select the book node with the matching attribute value.
XmlNode book, author;
XmlElement root = doc.DocumentElement;
book =
root.SelectSingleNode("/descendant::book[@bk:ISBN='1-861001-57-6']",
nsmgr);
Console.WriteLine(book.OuterXml);

author = book.SelectSingleNode("/descendant::author");


[...]

I can't test it right now, but I guess that should read:

author = book.SelectSingleNode("./descendant::author");

The dot is saying, select from the current branch. If you don't add it, it
will select from the root of the document.

Let me know if this works.

By the way, I think you can select the author in one statement. Check
following links for more information:

http://www.w3schools.com/xpath/
http://www.w3.org/TR/xpath

Cheers,
---
Tom Tempelaere

Nov 16 '05 #3

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

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
1
by: kurt hansen | last post by:
hi I thought that this would be easy, but maybe not so much. I want to: pass an xpath expression and a string value to a stylesheet and copy the source xml document, changing the value of...
4
by: Andreas Håkansson | last post by:
I have a price of XML that looks like this <Root> <SomeNode> ..... </SomeNode> <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> ... </Signature> </Root>
3
by: Patrick | last post by:
Dear Ng, after I have loaded am XmlDataDocument file, I try to extract a single node via XmlElement myElem = (XmlElement)this.XmlDataDocument.SelectSingleNode("/Document/Headerdata/myElem");...
13
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...
9
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
1
by: woodworthjames | last post by:
Hello, not sure i am using xpath correctly. have the following trivial xml. <?:xml version="1.0" standalone="yes" ?> <top attr="1"> content1 content2 <inner id="first" day="friday"> content3...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.