Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Xpath ancestor to view all parents of search?

Status
Not open for further replies.

KenNECIVSIPS

Technical User
Aug 23, 2002
29
US
I am trying to return only child elements that match the specific criteria.

Here is my xml:

Code:
<?xml version='1.0' encoding='utf-8' ?>
<Locations>
	<Location name='A'>
		<Vehicles>
			<Vehicle vin='1'>
				<type>Car</type>
				<color>Red</color>
			</Vehicle>
			<Vehicle vin='2'>
				<type>Car</type>
				<color>Blue</color>
			</Vehicle>
			<Vehicle vin='3'>
				<type>Boat</type>
				<color>Red</color>
			</Vehicle>
			<Vehicle vin='4'>
				<type>Boat</type>
				<color>Green</color>
			</Vehicle>
			<Vehicle>
				<type>Boat</type>
				<color>Yellow</color>
			</Vehicle>
		</Vehicles>
	</Location>
	<Location name='B'>
		<Vehicles>
			<Vehicle vin='5'>
				<type>Car</type>
				<color>Orange</color>
			</Vehicle>
		</Vehicles>
	</Location>	
</Locations>

The following query gives me all of the vehicles of type 'Car':

//Vehicle[type='Car']

Code:
<Vehicle vin='1'>
	<type>Car</type>
	<color>Red</color>
</Vehicle>
<Vehicle vin='2'>
	<type>Car</type>
	<color>Blue</color>
</Vehicle>
<Vehicle vin='5'>
	<type>Car</type>
	<color>Orange</color>
</Vehicle>

But what if I want this XML with all of the parent information? I want to be able to return the following:

Code:
<?xml version='1.0' encoding='utf-8' ?>
<Locations>
	<Location name='A'>
		<Vehicles>
			<Vehicle vin='1'>
				<type>Car</type>
				<color>Red</color>
			</Vehicle>
			<Vehicle vin='2'>
				<type>Car</type>
				<color>Blue</color>
			</Vehicle>
		</Vehicles>
	</Location>
	<Location name='B'>
		<Vehicles>
			<Vehicle vin='5'>
				<type>Car</type>
				<color>Orange</color>
			</Vehicle>
		</Vehicles>
	</Location>	
</Locations>

Notice that the Boats are not in this XML.

How do I do this. I have tried ancestor, parent, etc. but I can;t figure it out.

Thanks!
 
is there not a parentNode property?

That is what it is XML DOM...
Reference:

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
hmmm...

Did you say you tried these:

parent::cd Selects the cd parent of the current node

ancestor::cd Selects all cd ancestors of the current node

Reference:

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
for an example of parentNode...

goto the try-it-yourself page here:

the Original text looks like this:
Code:
<html>
<body>
<script type="text/vbscript">

set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("cdcatalog.xml")

path="/catalog/cd[price>10.80]/price"
set nodes=xmlDoc.selectNodes(path)

for each x in nodes
  document.write("<xmp>")
  document.write(x.xml)
  document.write("</xmp>")
next

</script>
</body>
</html>

And the Output:
Code:
<price>10.90</price>
<price>10.90</price>
<price>10.90</price>

But, by changing these 2 lines:
Code:
<html>
<body>
<script type="text/vbscript">

set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("cdcatalog.xml")

path="/catalog/cd[price>10.80]/price"
set nodes=xmlDoc.selectNodes(path)

for each x in nodes
  document.write("<xmp>")
  [b]set p=x.parentNode
  document.write(p.xml)[/b]
  document.write("</xmp>")
next

</script>
</body>
</html>

You get this:
Code:
<cd>
	<title>Empire Burlesque</title>
	<artist>Bob Dylan</artist>
	<country>USA</country>
	<company>Columbia</company>
	<price>10.90</price>
	<year>1985</year>
</cd>
<cd>
	<title>One night only</title>
	<artist>Bee Gees</artist>
	<country>UK</country>
	<company>Polydor</company>
	<price>10.90</price>
	<year>1998</year>
</cd>
<cd>
	<title>Black angel</title>
	<artist>Savage Rose</artist>
	<country>EU</country>
	<company>Mega</company>
	<price>10.90</price>
	<year>1995</year>
</cd>

Is this what you are looking for?

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
also, have you seen this thread: thread426-960527

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top