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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting a child within a child

Status
Not open for further replies.

Kaissi

Programmer
Sep 10, 2007
2
US
Hello here is my XML source file

<DeliverySchedule>
<Header>
<DistributorCode>MC</DistributorCode>
<TransmissionDate>2007-01-02</TransmissionDate>
<TransmissionTime>14:02:00.000</TransmissionTime>
</Header>
<Detail>
<StoreDelivery>
<SiteNum>001507</SiteNum>
<DSchedule>
<Order>
<Date>2007-09-02</Date>
<Time>08:02:01.000</Time>
</Order>
<Delivery>
<Date>2007-09-03</Date>
<Time>06:12:02.000</Time>
</Delivery>
</DSchedule>
<DSchedule>
<Order>
<Date>2007-09-04</Date>
<Time>08:02:03.000</Time>
</Order>
<Delivery>
<Date>2007-09-05</Date>
<Time>06:12:04.000</Time>
</Delivery>
</DSchedule>
</StoreDelivery>
<StoreDelivery>
<SiteNum>001510</SiteNum>
<DSchedule>
<Order>
<Date>2007-09-02</Date>
<Time>08:02:05.000</Time>
</Order>
<Delivery>
<Date>2007-09-03</Date>
<Time>06:12:06.000</Time>
</Delivery>
</DSchedule>
<DSchedule>
<Order>
<Date>2007-09-08</Date>
<Time>08:02:07.000</Time>
</Order>
<Delivery>
<Date>2007-09-09</Date>
<Time>06:12:08.000</Time>
</Delivery>
</DSchedule>
</StoreDelivery>
</Detail>
<Count>4</Count>
</DeliverySchedule>


I need to query the "Order" field or child where the field "Date" inside it is equal for example "2007-09-02" and its parent "SiteNum" = 001507. Can I do that?

This will really help me a lot, thank you
 
You can collect all the DSchedule nodes by the xpath like this.
[tt] "/DeliverySchedule/Detail//DSchedule[child::Order/Date='2007-09-02' and preceding-sibling::SiteNum='001507']"[/tt]
 
Amendment
I did not take notice of the existence of StoreDelivery node in the original xml doc. With StoreDelivery the xpath sure is more rational and precise. Here is the amended version.
[tt] "/DeliverySchedule/Detail/[blue]StoreDelivery[/blue]/DSchedule[child::Order/Date='2007-09-02' and preceding-sibling::SiteNum='001507']"[/tt]
 
Yes you can do that, but what do you want to return? This is the xpath I would use:
Code:
/DeliverySchedule/Detail/StoreDelivery[SiteNum='001507']/DSchedule[Order/Date='2007-09-02']

Jon

"I don't regret this, but I both rue and lament it.
 
Thank you guys both worked, I wanted to get either the Order node or the DSchedule node.


I used this as well
QueryString = "//DSchedule[Order/Date='2007-09-02' and ../SiteNum='001507']"

 
That XPath expression will work, but if you have it evaluated in a loop, you may wish to use Jon's suggestion instead. The [tt]//[/tt] operator causes the entire document to be searched for <DSchedule> elements. Again, if this is done in a loop with a larger document, you may not like the performance.

Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top