![]() |
|
|
| Subject:
Xpath: ancestor to view all parents of search?
Category: Computers > Programming Asked by: kennethwsmith-ga List Price: $5.00 |
Posted:
03 Jan 2005 06:55 PST
Expires: 28 Jan 2005 10:48 PST Question ID: 450922 |
I have the following XML:
<?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']
<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 I want to be able to return the following:
<?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.
Thanks, I have tried all kinds of things, and can't figure it out. I
am using Cold Fusion XmlSearch() which accepts all Xpath
functionality. No .net or VB please! |
|
| There is no answer at this time. |
|
| Subject:
Re: Xpath: ancestor to view all parents of search?
From: vladimir-ga on 03 Jan 2005 07:08 PST |
Does this query give the results you want? //Vehicle[type='Car']/ancestor-or-self::* Vladimir |
| Subject:
Re: Xpath: ancestor to view all parents of search?
From: kennethwsmith-ga on 03 Jan 2005 07:26 PST |
No, that still returns the Boats, which I don't want in there. Thanks! |
| Subject:
Re: Xpath: ancestor to view all parents of search?
From: kennethwsmith-ga on 03 Jan 2005 08:19 PST |
Looks to me now that I need to use xsl to transform the data that I want to return. Does this sound right? |
| Subject:
Re: Xpath: ancestor to view all parents of search?
From: vladimir-ga on 03 Jan 2005 08:23 PST |
Umm, yeah, now that I think of it you're probably right. Vladimir |
| Subject:
Re: Xpath: ancestor to view all parents of search?
From: kennethwsmith-ga on 03 Jan 2005 10:18 PST |
anyone know how to do this? This is super time-sensitive, so we are trying all avenues to get this done. |
| Subject:
Re: Xpath: ancestor to view all parents of search?
From: kennethwsmith-ga on 03 Jan 2005 12:41 PST |
We are writing a work around, so this is not needed any longer. Thanks. |
| Subject:
Re: Xpath: ancestor to view all parents of search?
From: vladimir-ga on 03 Jan 2005 12:57 PST |
I'm not an XSLT expert, but something like this seems to do what you need.
It could probably be written much cleaner.
Vladimir
<?xml version="1.0" encoding="iso-8859-2" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Vehicle[type='Car']">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="*">
<xsl:if test="descendant::Vehicle[type='Car']">
<xsl:call-template name="acopyof"/>
</xsl:if>
</xsl:template>
<xsl:template name="acopyof">
<xsl:copy>
<xsl:for-each select="@*">
<xsl:copy/>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet> |
| Subject:
Re: Xpath: ancestor to view all parents of search?
From: kennethwsmith-ga on 04 Jan 2005 06:46 PST |
Thats great! One small clarification, and I will award the answer. With a LocationStreet in the xml: <?xml version='1.0' encoding='utf-8' ?> <Locations> <Location name='A'> <LocationStreet>123 Main Street</LocationStreet> <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'> <LocationStreet>423 Elm Drive</LocationStreet> <Vehicles> <Vehicle vin='5'> <type>Car</type> <color>Orange</color> </Vehicle> </Vehicles> </Location> </Locations> The LocationStreet does not show up. How can I get that to show up in the xml? |
| Subject:
Re: Xpath: ancestor to view all parents of search?
From: vladimir-ga on 04 Jan 2005 07:19 PST |
Well, it can be achieved in a number of ways I guess. If what you
really mean is "copy the entire XML tree filtering out the vehicles
that are not cars" then you can use a stylesheet like the one below.
It seems more elegant than listing every element that you want to copy
(like LocationStreet etc.).
Vladimir
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Vehicle[type!='Car']">
</xsl:template>
<xsl:template match="*">
<xsl:call-template name="acopyof"/>
</xsl:template>
<xsl:template name="acopyof">
<xsl:copy>
<xsl:for-each select="@*">
<xsl:copy/>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet> |
| Subject:
Re: Xpath: ancestor to view all parents of search?
From: kennethwsmith-ga on 04 Jan 2005 07:41 PST |
You are awesome! Can you help me negate (?) this string? //Tourcode[Code='AAO' or Code='ALH']/HotelList/Hotel[HotelCode='ABC']/HotelCode I changed it to this: //Tourcode[Code!='AAO' and Code!='ALH']/HotelList/Hotel[HotelCode!='ABC']/HotelCode but it is not giving me what I am looking for. |
| Subject:
Re: Xpath: ancestor to view all parents of search?
From: vladimir-ga on 04 Jan 2005 07:51 PST |
Can you give a sample of the XML and the expected results? Vladimir |
| Subject:
Re: Xpath: ancestor to view all parents of search?
From: vladimir-ga on 04 Jan 2005 08:16 PST |
I'm not a Google Researcher, just an enthusiast of the Google Answers service, so I can't post answers, just comments like the ones above. So I guess this one's free, you can treat it as a New Year's gift. I hope to one day become a Google Researcher, if you want to help me you can e-mail answers-support@google.com and tell them that vladimir-ga helped you when noone else could and that you think they should make me a researcher. :) Vladimir |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
| Search Google Answers for |
| Google Home - Answers FAQ - Terms of Service - Privacy Policy |