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

XPath search problem 1

Status
Not open for further replies.

graabein

Programmer
Joined
Oct 9, 2002
Messages
186
Location
NO
Hi,

I'm relatively new to XML and XPath programming. I am working on a program for my record collection with XML and C# .NET. The XPath search for album nodes is the problem.

XML example:
Code:
<Albums>
  <Album ID=&quot;10&quot;>
    <Artist>Young, Neil</Artist>
    <Artist>Pearl Jam</Artist>
    <Title>Mirrorball</Title>
    <Year>1995</Year>
  </Album>
  <Album ID=&quot;14&quot;>
    <Artist>Young, Neil</Artist>
    <Title>After The Gold Rush</Title>
    <Label>Reprise</Label>
    <Year>1970</Year>
  </Album>
  <Album ID=&quot;142&quot;>
    <Artist>Pearl Jam</Artist>
    <Title>Ten</Title>
  </Album>
</Albums>

Now I want to write an XPathExpression that selects the following:

1) Search independent of lower/upper case
2) &quot;pearl&quot; search returns both Ten and After The Gold Rush
3) Search title as well
4) Return distinct albums (unique ID)

I have the following C# code that does some of this...
Code:
expr = nav.Compile(&quot;/Albums/Album[contains(Artist, '&quot; + criteria + &quot;')]&quot;;

It does return all Neil Young albums with criteria like &quot;You&quot;, but it does not locate the Pearl Jam collaboration on Mirrorball. Pearl Jam-search only returns the Ten album. Some XPath gurus willing to help me out?

[peace]
graabein
 
Not a C# user, but as you can see, your XPATH pattern only matches the first Artist element, which doesn't satisfy the contains condition. (Test this by reversing the order of the Artist elements.)

An XPATH solution involves iteration (for-each) or apply-templates. I can't help you with the C# implementation.
 
Well I did 1,2, and 4, you can figure out 3 by yourself ;-)

1: use translate() function to convert yournode to lcase (make sure your criteria are lcase to)
contains(translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghicklmnopgrstuvwxyz'), '&quot; + criteria + &quot;')]

2: Look for an album that has any artist that has any etc:
/Albums/Album[Artist[contains etc ]]

4: check if there is a preceding node with the same ID:
[@ID!=preceding::Album/@ID]

All put together makes the mother-of-all-x-path-expressions:
/Albums/Album[Artist[contains(translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghicklmnopgrstuvwxyz'), '&quot; + criteria + &quot;')] and @ID!=preceding::Album/@ID]
 
Thanks jel, that's very nice! Works like a charm!
[cook]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top