sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Ken Getz's Avatar

Index of using XPATH?


Question posted by: Ken Getz (Guest) on November 12th, 2005 05:09 AM
Hi. I have an xml file in this format:

<strings>
<string>Item1</string>
<string>Item2</string>
<string>Item3</string>
<string>Item4</string>
</string>

I'm looking for the best way to search for a specific string, and return the
index of its node within the strings element. That is, if someone enters
Item3, I need the value 3 (or 2, if the index is 0-based). I have a feeling
there's a reasonably simple XPath expression that will get this for me, but
I'm trying to avoid iterating through all the elements to find the index. I
might have several thousands, or tens of thousands, of items to look
through, so a linear search for a match would be ugly, especially if I could
use an XmlTextReader or some other technique that doesn't require me to load
the entire XML file first into an XmlDocument object.

I know how to do this the iterative way -- if you have a suggestion about
how to do this in some elegant way, using XPath, I'd love to hear it!
Thanks -- Ken


4 Answers Posted
Martin Honnen's Avatar
Guest - n/a Posts
#2: Re: Index of using XPATH?



Ken Getz wrote:
[color=blue]
> Hi. I have an xml file in this format:
>
> <strings>
> <string>Item1</string>
> <string>Item2</string>
> <string>Item3</string>
> <string>Item4</string>
> </string>[/color]

</strings>
[color=blue]
> I'm looking for the best way to search for a specific string, and return the
> index of its node within the strings element. That is, if someone enters
> Item3, I need the value 3 (or 2, if the index is 0-based).[/color]

You can check the number of preceding siblings e.g.
/strings/string[. = 'Item3']/preceding-sibling::string
Of course XPath can count too
count(/strings/string[. = 'Item3']/preceding-sibling::string)

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Ken Getz's Avatar
Guest - n/a Posts
#3: Re: Index of using XPATH?

Cool. Thanks. I appreciate the info. Got it working, using XPathNavigator.
If the file is large, however, I don't want to load the whole thing -- it
would be better to have a framework class stop loading once it found the
correct node. Do you (or anyone else) have suggestions on how to optimize
the lookup, with perhaps a simple code sample? Thanks. -- Ken

"Martin Honnen" <mahotrash@yahoo.de> wrote in message
news:OM6BEfB0FHA.2348@TK2MSFTNGP15.phx.gbl...[color=blue]
>
>
> Ken Getz wrote:
>[color=green]
>> Hi. I have an xml file in this format:
>>
>> <strings>
>> <string>Item1</string>
>> <string>Item2</string>
>> <string>Item3</string>
>> <string>Item4</string>
>> </string>[/color]
>
> </strings>
>[color=green]
>> I'm looking for the best way to search for a specific string, and return
>> the index of its node within the strings element. That is, if someone
>> enters Item3, I need the value 3 (or 2, if the index is 0-based).[/color]
>
> You can check the number of preceding siblings e.g.
> /strings/string[. = 'Item3']/preceding-sibling::string
> Of course XPath can count too
> count(/strings/string[. = 'Item3']/preceding-sibling::string)
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/[/color]



Chris Lovett's Avatar
Guest - n/a Posts
#4: Re: Index of using XPATH?

XmlDocument is not a database system. As soon as you say you don't want to
load the whole thing you have two options:
1) Break the data into multiple XML files (but then understand there is no
XPath engine that can tie these back together - so you have ro manage which
file to load and query individually yourself).
2) use a real database system - like SQL Server 2005 (see
http://lab.msdn.microsoft.com/express/sql/).


"Ken Getz" <ken.getz@gmail.com> wrote in message
news:uPhIXqB0FHA.3896@TK2MSFTNGP10.phx.gbl...[color=blue]
> Cool. Thanks. I appreciate the info. Got it working, using XPathNavigator.
> If the file is large, however, I don't want to load the whole thing -- it
> would be better to have a framework class stop loading once it found the
> correct node. Do you (or anyone else) have suggestions on how to optimize
> the lookup, with perhaps a simple code sample? Thanks. -- Ken
>
> "Martin Honnen" <mahotrash@yahoo.de> wrote in message
> news:OM6BEfB0FHA.2348@TK2MSFTNGP15.phx.gbl...[color=green]
>>
>>
>> Ken Getz wrote:
>>[color=darkred]
>>> Hi. I have an xml file in this format:
>>>
>>> <strings>
>>> <string>Item1</string>
>>> <string>Item2</string>
>>> <string>Item3</string>
>>> <string>Item4</string>
>>> </string>[/color]
>>
>> </strings>
>>[color=darkred]
>>> I'm looking for the best way to search for a specific string, and return
>>> the index of its node within the strings element. That is, if someone
>>> enters Item3, I need the value 3 (or 2, if the index is 0-based).[/color]
>>
>> You can check the number of preceding siblings e.g.
>> /strings/string[. = 'Item3']/preceding-sibling::string
>> Of course XPath can count too
>> count(/strings/string[. = 'Item3']/preceding-sibling::string)
>>
>> --
>>
>> Martin Honnen --- MVP XML
>> http://JavaScript.FAQTs.com/[/color]
>
>
>[/color]


ken.getz@gmail.com's Avatar
ken.getz@gmail.com November 12th, 2005 05:09 AM
Guest - n/a Posts
#5: Re: Index of using XPATH?

Hi. I appreciate the reply, but of course, I don't control the XML I'm
given to parse out. There ARE techniques that don't require loading the
entire XML file -- .NET provides the XmlTextReader, which reads it in
element by element. I just wasn't sure what the best solution was. --
Ken

 
Not the answer you were looking for? Post your question . . .
196,829 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,829 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors