Groups | Blog | Home
all groups > dotnet xml > march 2004 >

dotnet xml : XPath question


veloearl NO[at]SPAM hotmail.com
3/31/2004 6:38:16 AM
Let's say I have the following XML:

<authors>
<author>
<name>Mike Galos</name>
<nationality>French</nationality>
</author>
<author period="modern">
<name>Eva Corets</name>
<nationality>British</nationality>
</author>
<author>
<name>Cynthia Randall</name>
<nationality>Canadian</nationality>
</author>
<author>
<name>Stefan Knorr</name>
<nationality>Canadian</nationality>
</author>
<author period="modern">
<name>Paula Thurman</name>
<nationality>British</nationality>
</author>
</authors>

I want to use an XPath expression to select all Canadian authors, but
want the results to be nested within their ancestor like so:

<authors>
<author>
<name>Cynthia Randall</name>
<nationality>Canadian</nationality>
</author>
<author>
<name>Stefan Knorr</name>
<nationality>Canadian</nationality>
</author>
</authors>

Is this possible with XPath? If I use

/authors/author[nationality='Canadian']/ancestor-or-self::*

as my XPath expression, I get back the root node and all it's
NaraendiraKumar R. R.
4/4/2004 1:53:16 AM
XPath won't give you what you want.

-Naraen

------
[quoted text, click to view]

Martin Peck [MSFT]
4/7/2004 11:45:21 AM
Brad,

XPath will allow you to specify which nodes in your input document you want
to select. XPath statements will return a list of nodes that match your
statement. XPath itself won't transform your XML into another format.

If what you're trying to do is generate an output document that has a
different format from the input document then perhaps XSLT is what you're
really after. XSLT will allow you to transform an input XML document into an
output XML document, and as part of that transform you will specify an XPath
to tell the transform which parts of the input document you'd like
tranformed (e.g. "transform all of the Canadian authors only").

http://www.xml.com/pub/a/2000/08/holman/index.html give a good write-up of
XSLT.

There are other ways of generating your output XML (using a DOM, using
custom code) but they really depend upon what you're doing. For example, if
you're dealing with XML files, if you're dealing with XML that's already
been loaded into a DOM, if you're dealing with XML that's part of a stream.

Regards,

Martin

--
This posting is provided "AS IS" with no warranties, and confers no
rights.


[quoted text, click to view]

Daniel Cazzulino [MVP XML]
4/28/2004 12:44:34 AM
You can use the following code:

XPathNodeIterator it =
nav.Select("/authors/author[nationality='Canadian']");
XPathIteratorReader reader = new XPathIteratorReader(it, "authors");

reader.MoveToContent();
string xml = reader.ReadOuterXml();
// now xml contains the format you wanted.

// To save the subset to file
XmlTextWriter tw = new XmlTextWriter(destination);
tw.WriteNode( new XPathIteratorReader( it, "authors" ) );
tw.Close();

The XPathIteratorReader implements IXmlSerializable, so you can also return
it directly from a webservice.

Check it out at http://weblogs.asp.net/cazzu/archive/2004/04/26/120684.aspx


--
Daniel Cazzulino [MVP XML]
Clarius Consulting SA
http://weblogs.asp.net/cazzu
http://aspnet2.com
[quoted text, click to view]


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.665 / Virus Database: 428 - Release Date: 23/04/2004

AddThis Social Bookmark Button