HighTechTalks DotNet Forums  

XPath Query Help

Dotnet XML microsoft.public.dotnet.xml


Discuss XPath Query Help in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Brendan CM
 
Posts: n/a

Default XPath Query Help - 11-07-2007 , 07:53 PM






I'm trying to write a query to pull all the specific fields from a xml
file. The XML is structured in such a way that nested child elements
in a section will determine if a field exists in a form. In order to
show all the fields in an xml file I need to get all XML elements
between two elements.

Here is an example:

<root>
<container>
<elem><tag="start" /></elem>
<elem><val>Keep this one</val></elem>
<elem><tag="stop" /></elem>
<elem><val>Discard Me</val></elem>
<elem><tag="start" /></elem>
<elem><val>Keep this one</val></elem>
<elem><tag="stop" /></elem>
</container>
</root>

I want to get the <val> tags between the start and stop tags, and
discard the <val> tags outside of the start/stop tags.
Anyone know how to write an XPath query to give me this? I've been
frustrated over it for a while now using Visual XPath to figure it
out.

Thanks,
Brendan


Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: XPath Query Help - 11-08-2007 , 08:31 AM






Brendan CM wrote:

Quote:
Here is an example:

root
container
elem><tag="start" /></elem
^^^^^^^^^^^
What is that? That is not well-formed XML.

Quote:
elem><val>Keep this one</val></elem
elem><tag="stop" /></elem
^^^^^^^^^^^^^
Same here.




--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Reply With Quote
  #3  
Old   
Brendan CM
 
Posts: n/a

Default Re: XPath Query Help - 11-08-2007 , 02:43 PM



Ok, figured it out, given the following XML:
<root>
<container>
<elem><tag v="start"></tag></elem>
<elem><val>Keep this one</val></elem>
<elem><val>Keep this one too</val></elem>
<elem><tag v="stop"></tag></elem>
<elem><val>Discard Me</val></elem>
<elem><tag v="start"></tag></elem>
<elem><val>Keep this one</val></elem>
<elem><tag v="stop"></tag></elem>
</container>
</root>

This XPath will return only elems within the start stop tags:
//val[preceding::elem[./tag/@v][1]/tag/@v='start']


Reply With Quote
  #4  
Old   
Brendan CM
 
Posts: n/a

Default Re: XPath Query Help - 11-08-2007 , 02:45 PM



On Nov 8, 5:31 am, Martin Honnen <mahotr... (AT) yahoo (DOT) de> wrote:
Quote:
Brendan CM wrote:
Here is an example:
root
container
elem><tag="start" /></elem

^^^^^^^^^^^
What is that? That is not well-formed XML.
Okay, it was just an example, here is some well formed XML:

<root>
<container>
<elem><tag v="start"></tag></elem>
<elem><val>Keep this one</val></elem>
<elem><tag v="stop"></tag></elem>
<elem><val>Discard Me</val></elem>
<elem><tag v="start"></tag></elem>
<elem><val>Keep this one</val></elem>
<elem><tag v="stop"></tag></elem>
</container>
</root>

Same question applies though, surely someone here knows that last
little bit of XPath that I'm missing.
This is what I've got so far: //elem[tag[@v="start"]]/following-
sibling::*[following-sibling::*/tag[@v="stop"]]
And it doesn't work.



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.