473,385 Members | 1,588 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,385 software developers and data experts.

Using XPath Against A Node

I bring in an xml file into vb.net by using xmlDoc.LoadXml(XMLString) - I
run xpath statements against the xml file to grab data from it, so I use, as
an example, //Vehicles/Vehicles/@make to get the make of the car. But then
I pass a specific node from xmlDoc into another function, not the whole
xmlDoc, just a node from it. And if I run an xpath against it, I have to
use .// (has a period at the beginning) so it does not grab info from the
parent. So I would have to use .//Vehicles/Vehicles/@make so it references
the current node. I would think that if I passed a node to a vb.net
function, I could simply use an xpath statement that would work off the
passed in node, and I could assume that the top part of that node was the
new parent. Is there a way to pass a node like this?

Derek
Dec 5 '06 #1
6 1990
Derek,

I am not sure what you meant by: "and I could assume that the top part
of that node was the new parent."

Using "/" will search the document root node immediate descendants.
Using "./" will search the context node immediate descendants.
Using "// will search anywhere in the entire document.
Using ".//" will search anywhere in the context node descendants.
Similarly, you could use "descendant::".

If you know the absolute path of the data you are looking for, you can
simply skip the "//" or the "/". For example, if "Price" were an
attribute of "Model", which is an immediate descendant of Vehicles, you
could query for the price of a model using "Model/@Price". If you are
a high paid developer you could search for a specific model using
"Model[.='Cadillac']/@Price". You don't need to use ".//Model/@Price"
or "descendant::Model/@Price".

I don't know if this was helpful but .... I don't drive a Cadillac.
Derek Hart wrote:
I bring in an xml file into vb.net by using xmlDoc.LoadXml(XMLString) - I
run xpath statements against the xml file to grab data from it, so I use, as
an example, //Vehicles/Vehicles/@make to get the make of the car. But then
I pass a specific node from xmlDoc into another function, not the whole
xmlDoc, just a node from it. And if I run an xpath against it, I have to
use .// (has a period at the beginning) so it does not grab info from the
parent. So I would have to use .//Vehicles/Vehicles/@make so it references
the current node. I would think that if I passed a node to a vb.net
function, I could simply use an xpath statement that would work off the
passed in node, and I could assume that the top part of that node was the
new parent. Is there a way to pass a node like this?

Derek
Dec 6 '06 #2
So if I pass a node to a function, can I treat the top-most element as the
top parent? That is my question. Or is the entire xml document sent with
it. I do not want it to be possible to look at the whole document in the
function. I just want to pass in a specific node. Is it possible?

Derek
"FishingScout" <fi**********@comcast.netwrote in message
news:11*********************@n67g2000cwd.googlegro ups.com...
Derek,

I am not sure what you meant by: "and I could assume that the top part
of that node was the new parent."

Using "/" will search the document root node immediate descendants.
Using "./" will search the context node immediate descendants.
Using "// will search anywhere in the entire document.
Using ".//" will search anywhere in the context node descendants.
Similarly, you could use "descendant::".

If you know the absolute path of the data you are looking for, you can
simply skip the "//" or the "/". For example, if "Price" were an
attribute of "Model", which is an immediate descendant of Vehicles, you
could query for the price of a model using "Model/@Price". If you are
a high paid developer you could search for a specific model using
"Model[.='Cadillac']/@Price". You don't need to use ".//Model/@Price"
or "descendant::Model/@Price".

I don't know if this was helpful but .... I don't drive a Cadillac.
Derek Hart wrote:
>I bring in an xml file into vb.net by using xmlDoc.LoadXml(XMLString) - I
run xpath statements against the xml file to grab data from it, so I use,
as
an example, //Vehicles/Vehicles/@make to get the make of the car. But
then
I pass a specific node from xmlDoc into another function, not the whole
xmlDoc, just a node from it. And if I run an xpath against it, I have to
use .// (has a period at the beginning) so it does not grab info from the
parent. So I would have to use .//Vehicles/Vehicles/@make so it
references
the current node. I would think that if I passed a node to a vb.net
function, I could simply use an xpath statement that would work off the
passed in node, and I could assume that the top part of that node was the
new parent. Is there a way to pass a node like this?

Derek

Dec 6 '06 #3
Derek Hart wrote:
So if I pass a node to a function, can I treat the top-most element as the
top parent? That is my question. Or is the entire xml document sent with
it. I do not want it to be possible to look at the whole document in the
function. I just want to pass in a specific node. Is it possible?
In the DOM an XmlNode always has an OwnerDocument property set to the
document that created the node. A node cannot exist without its owning
document as you can only create nodes with the factory methods of the
XmlDocument instance. A node does not need to have a ParentNode property
being non null however.

In terms of the XPath implementation with SelectSingleNode and
SelectNodes if the node is not inserted in the owning document then it
looks like the XPath / evaluates to the node itself. Thus if you do e.g.
Dim Xml_Doc as XmlDocument = New XmlDocument()
Dim SomeElement as XmlElement = Xml_Doc.CreateElement("whatever")
and pass that node around without inserting it into the document then
the node in terms of XPath and SelectNodes/SelectSingleNode does not
expose the document.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Dec 6 '06 #4
If there is some reason you don't want a method to access the document
from a node, it is possible to create a new xml document with only that
node and its descendants and pass it to a method. If the method
modifies the node you will need to do some work to replace the original
node with the modified node.

I don't know of any way to "protect" ancestor nodes within the DOM.

Derek Hart wrote:
So if I pass a node to a function, can I treat the top-most element as the
top parent? That is my question. Or is the entire xml document sent with
it. I do not want it to be possible to look at the whole document in the
function. I just want to pass in a specific node. Is it possible?

Derek
"FishingScout" <fi**********@comcast.netwrote in message
news:11*********************@n67g2000cwd.googlegro ups.com...
Derek,

I am not sure what you meant by: "and I could assume that the top part
of that node was the new parent."

Using "/" will search the document root node immediate descendants.
Using "./" will search the context node immediate descendants.
Using "// will search anywhere in the entire document.
Using ".//" will search anywhere in the context node descendants.
Similarly, you could use "descendant::".

If you know the absolute path of the data you are looking for, you can
simply skip the "//" or the "/". For example, if "Price" were an
attribute of "Model", which is an immediate descendant of Vehicles, you
could query for the price of a model using "Model/@Price". If you are
a high paid developer you could search for a specific model using
"Model[.='Cadillac']/@Price". You don't need to use ".//Model/@Price"
or "descendant::Model/@Price".

I don't know if this was helpful but .... I don't drive a Cadillac.
Derek Hart wrote:
I bring in an xml file into vb.net by using xmlDoc.LoadXml(XMLString) - I
run xpath statements against the xml file to grab data from it, so I use,
as
an example, //Vehicles/Vehicles/@make to get the make of the car. But
then
I pass a specific node from xmlDoc into another function, not the whole
xmlDoc, just a node from it. And if I run an xpath against it, I have to
use .// (has a period at the beginning) so it does not grab info from the
parent. So I would have to use .//Vehicles/Vehicles/@make so it
references
the current node. I would think that if I passed a node to a vb.net
function, I could simply use an xpath statement that would work off the
passed in node, and I could assume that the top part of that node was the
new parent. Is there a way to pass a node like this?

Derek
Dec 6 '06 #5
Any sample code on breaking the node off the original xml document into its
own "smaller" document and node?

Derek

"FishingScout" <fi**********@comcast.netwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
If there is some reason you don't want a method to access the document
from a node, it is possible to create a new xml document with only that
node and its descendants and pass it to a method. If the method
modifies the node you will need to do some work to replace the original
node with the modified node.

I don't know of any way to "protect" ancestor nodes within the DOM.

Derek Hart wrote:
>So if I pass a node to a function, can I treat the top-most element as
the
top parent? That is my question. Or is the entire xml document sent with
it. I do not want it to be possible to look at the whole document in the
function. I just want to pass in a specific node. Is it possible?

Derek
"FishingScout" <fi**********@comcast.netwrote in message
news:11*********************@n67g2000cwd.googlegr oups.com...
Derek,

I am not sure what you meant by: "and I could assume that the top part
of that node was the new parent."

Using "/" will search the document root node immediate descendants.
Using "./" will search the context node immediate descendants.
Using "// will search anywhere in the entire document.
Using ".//" will search anywhere in the context node descendants.
Similarly, you could use "descendant::".

If you know the absolute path of the data you are looking for, you can
simply skip the "//" or the "/". For example, if "Price" were an
attribute of "Model", which is an immediate descendant of Vehicles, you
could query for the price of a model using "Model/@Price". If you are
a high paid developer you could search for a specific model using
"Model[.='Cadillac']/@Price". You don't need to use ".//Model/@Price"
or "descendant::Model/@Price".

I don't know if this was helpful but .... I don't drive a Cadillac.
Derek Hart wrote:
I bring in an xml file into vb.net by using
xmlDoc.LoadXml(XMLString) - I
run xpath statements against the xml file to grab data from it, so I
use,
as
an example, //Vehicles/Vehicles/@make to get the make of the car. But
then
I pass a specific node from xmlDoc into another function, not the
whole
xmlDoc, just a node from it. And if I run an xpath against it, I have
to
use .// (has a period at the beginning) so it does not grab info from
the
parent. So I would have to use .//Vehicles/Vehicles/@make so it
references
the current node. I would think that if I passed a node to a vb.net
function, I could simply use an xpath statement that would work off
the
passed in node, and I could assume that the top part of that node was
the
new parent. Is there a way to pass a node like this?

Derek

Dec 7 '06 #6
Derek,

Here is an example. You will need to create a directory
"c:\xmlexample" or modify the paths in the code. After the code below
is the contents of an xml file that you should name "dealership.xml"
and save to the directory that you created. Even though changes can be
made to the XML DOM passed into the DoSomeXmlProcessing, they are not
available in the orignal XML DOM unless you copy them back into the
original XML DOM. You can verify this by commenting out the
....InnerXml = ...InnerXml line.

Create a VB.NET windows application.

Replace the contents of Form1.vb with this:

Imports System.Xml

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

Dim WholeXmlDocument As New XmlDocument
WholeXmlDocument.Load("c:\xmlexample\dealership.xm l")

' assume, for this example, the user wants
' all Chevy models displayed

Dim VehicleNode As XmlNode = _
WholeXmlDocument.SelectSingleNode("//Vehicles[@Make='Chevy']")

If Not VehicleNode Is Nothing Then

' Create a new document from the Vehicle
' node so the other developers cannot
' modify the actual dealership document

Dim ChevyXmlDoc As New XmlDocument
ChevyXmlDoc.AppendChild(ChevyXmlDoc.ImportNode( _
VehicleNode.Clone(), True))

Dim bDocumentModified As Boolean

bDocumentModified = DoSomeXmlProcessing(ChevyXmlDoc)

' Changes to the ChevyXmlDocument will only
' appear in the WholeXmlDocument if you copy
' the modified node back into the WholeXmlDocument
If bDocumentModified Then
VehicleNode.InnerXml = ChevyXmlDoc.Clone().InnerXml
End If

WholeXmlDocument.Save("c:\xmlexample\modified.xml" )

End If

End Sub

Private Function DoSomeXmlProcessing(ByRef ModelXmlDocument _
As XmlDocument) As Boolean

Dim bDocumentWasModified As Boolean = False

' Uplander sales are down so give it a
' nice rebate
Dim UplanderNode As XmlNode = _
ModelXmlDocument.SelectSingleNode("//Model[.='Uplander']")

If Not UplanderNode Is Nothing Then

' 5000 should boost sales!

UplanderNode.Attributes("Rebate").InnerText = "5000"

bDocumentWasModified = True

End If

Return bDocumentWasModified

End Function
End Class
=========================
dealership.xml
=========================

<?xml version="1.0" encoding="utf-8"?>
<Dealership>
<Vehicles>
<Vehicles Make="Chevy">
<Model Price="49,000" Rebate="5000">Corvet</Model>
<Model Price="34,000" Rebate="2500">Tahoe</Model>
<Model Price="21,000" Rebate="0">Uplander</Model>
</Vehicles>
<Vehicles Make="Bavarian Motor Works">
<Model Price="85,000" Rebate="0">740i</Model>
<Model Price="45,000" Rebate="0">530i</Model>
<Model Price="35,000" Rebate="0">325i</Model>
</Vehicles>
</Vehicles>
</Dealership>

Derek Hart wrote:
Any sample code on breaking the node off the original xml document into its
own "smaller" document and node?

Derek

"FishingScout" <fi**********@comcast.netwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
If there is some reason you don't want a method to access the document
from a node, it is possible to create a new xml document with only that
node and its descendants and pass it to a method. If the method
modifies the node you will need to do some work to replace the original
node with the modified node.

I don't know of any way to "protect" ancestor nodes within the DOM.

Derek Hart wrote:
So if I pass a node to a function, can I treat the top-most element as
the
top parent? That is my question. Or is the entire xml document sent with
it. I do not want it to be possible to look at the whole document in the
function. I just want to pass in a specific node. Is it possible?

Derek
"FishingScout" <fi**********@comcast.netwrote in message
news:11*********************@n67g2000cwd.googlegro ups.com...
Derek,

I am not sure what you meant by: "and I could assume that the top part
of that node was the new parent."

Using "/" will search the document root node immediate descendants.
Using "./" will search the context node immediate descendants.
Using "// will search anywhere in the entire document.
Using ".//" will search anywhere in the context node descendants.
Similarly, you could use "descendant::".

If you know the absolute path of the data you are looking for, you can
simply skip the "//" or the "/". For example, if "Price" were an
attribute of "Model", which is an immediate descendant of Vehicles, you
could query for the price of a model using "Model/@Price". If you are
a high paid developer you could search for a specific model using
"Model[.='Cadillac']/@Price". You don't need to use ".//Model/@Price"
or "descendant::Model/@Price".

I don't know if this was helpful but .... I don't drive a Cadillac.
Derek Hart wrote:
I bring in an xml file into vb.net by using
xmlDoc.LoadXml(XMLString) - I
run xpath statements against the xml file to grab data from it, so I
use,
as
an example, //Vehicles/Vehicles/@make to get the make of the car. But
then
I pass a specific node from xmlDoc into another function, not the
whole
xmlDoc, just a node from it. And if I run an xpath against it, I have
to
use .// (has a period at the beginning) so it does not grab info from
the
parent. So I would have to use .//Vehicles/Vehicles/@make so it
references
the current node. I would think that if I passed a node to a vb.net
function, I could simply use an xpath statement that would work off
the
passed in node, and I could assume that the top part of that node was
the
new parent. Is there a way to pass a node like this?

Derek
Dec 7 '06 #7

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...
2
by: Daniel Walzenbach | last post by:
Hi I need to know how to transform a XML file by using a XSLT file. Consider the following XML file <?xml version="1.0" encoding="UTF-8" standalone="yes"?><LogFile><Dog TimeStamp="16.02.2004...
0
by: Victor Hadianto | last post by:
Hi, Is it possible to automatically create an XML element given an XPath query? For example I have the following XML doc: <topic> <title>Hi this is the title</title> <description>This is a...
2
by: Bob | last post by:
I have sitemap like XML, of which every element has an attribute "url", e.g. <record menu_id="240" name="Countries and States" url="Countries.aspx"> <record2 page_id="54" url="CountriesEdit.aspx"...
6
by: Chua Wen Ching | last post by:
Hi there, I had this xml file with me (not yet consider implementing xml namespaces yet). <?xml version='1.0'?> <Object> <Windows> <EID>1</EID> <EDesc>Error 1</EDesc> </Windows>
10
by: Michael C# | last post by:
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...
3
by: Goran Djuranovic | last post by:
Hi All, Does anyone know how to retreive deepest XPath value from XML document by using VB.NET? For example, if I had an XML file like this: <Root> <Customer> <Name>MyName</Name> </Customer>...
3
by: ziggyware | last post by:
Hi All, I have updated my XPath Generator software: XPath Studio .NET. ( http://www.ziggyware.com/downloads.php?cat_id=2 ) Easily select nodes from an xml file to generate XPath statements ...
3
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
Hi; We have a TreeView that represents an xml file (or it's schema is a more accurate statement). We want to have a double click on a node in the tree generate the XPath to get to that node. ...
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: 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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.