1

In my most recent attempt to access the XML database from my nightmares, I've come very close. I actually succeeded, using the test database; however, when I apply it to the database I'm actually trying to access, rather than the BaseX sample database, I get a special brand of error, which is a valid complaint about the XML file:

via http://basex.org/products/live-demo/

doc('test') :=

<item>
<item_number>1171270</item_number>
<seller_info>
<seller_company_id>6356</seller_company_id>
<seller_rating>C31</seller_rating>
<seller_rating>T150 hr.</seller_rating>
</seller_info>
<product_info>
<unit>2022</unit>
<sinfo>55 cases</sinfo>
<sinfo>Yu-gi-oh trading card pack</sinfo>
<sinfo>.45kg per unit</sinfo>
<sinfo>24.7500kg shipment</sinfo>
</product_info>
<product_info>
<unit>9291</unit>
<sinfo>7 units</sinfo>
<sinfo>Naruto, Classic, action figure</sinfo>
<sinfo>1.8kg per unit</sinfo>
<sinfo>12.6kg shipment</sinfo>
</product_info>
</item>

0: write your own query... :=

let $doc := doc('test') 
for $v in $doc//item
where contains($v/product_info/unit,'9291')
return 
$v/seller_info/seller_company_id

Returns:

Error:
Stopped at line 3, column 39: [XPTY0004] Single item expected, (element unit { ... }, element unit { ... }) found.

I can't say I didn't expect to encounter a problem like this. Unfortunately, I didn't format the XML document--someone else did--, and it's all terribly formatted, as you can see. Part of the reason I'm trying to access it: to restructure it.

Is there a way to run the query I'm trying to run on the document and get results without having this error spit at me? I'm also looking at the error of having Single item expected in my returns, when I try to target sinfo as a return value, right? Is there any way to get, for example, all of the sinfo's? Or how about only the second sinfo for each product_info without having this nasty error spit back at me?

2
  • 1
    I like the demo link u mentioned here. It is useful to try and test queries.
    – Jayy
    Sep 19, 2012 at 8:21
  • @KaipaMSarma Yeah, me too. I like it better than the applets mentioned at w3.org. It would be really nice if they showed the XML files the queries manipulate. :) I also like BaseX as a Stylus Studio alternative. It's got most of the functionality paralleled. Look out for my next question: 'how to run XQuery commands on web applications'. :) Sep 19, 2012 at 23:25

3 Answers 3

9

Your path $v/product_info/unit in the where clause will yield more than one item per call, while the contains() function only accepts single items as arguments. The following query will give you the expected results:

let $doc := doc('test') 
for $v in $doc//item
where some $i in $v/product_info/unit satisfies contains($i,'9291')
return $v/seller_info/seller_company_id

Another solution (in the predicate [...], each item will be bound to the context item . and processed one by one):

let $doc := doc('test') 
for $v in $doc//item
where $v/product_info/unit[contains(.,'9291')]
return $v/seller_info/seller_company_id
2
  • @Christian, This looks good to me (much akin to the response I had expected); however, I won't be able to test it for a few hours. Until then, thank you, for your response. Sarma, Thank you for the verification. Sep 18, 2012 at 10:41
  • @Christian I'm working on another question, here: stackoverflow.com/questions/12550694/…. I haven't been able to figure out how to apply this method to that query when there are multiple <comment> elements per entry. I want to create a separate question, but I don't see the fundamental difference between the two queries that makes one so much more difficult to apply this to than the other. Sep 26, 2012 at 0:05
2

As Christian mentioned, contains() xpath function expects a single node, but since in your xml root, there are two nodes returned by the xpath expression. Hence the XQuery can be written as

let $doc := doc('test')  
for $v in $doc//item 
where $v/product_info/unit[.='9291']
return  $v/seller_info/seller_company_id

Another important thing to note here is the usage of contains() function. This function returns true if the string is contained in the Main string as a whole or a part of it. That means it will return true for units having value as

9291

19291

23171239291237283

...

Hence it is advised to use the right functions as per the need.

2
  • Hmmm...> But to be honest, I can't see what's happening. Sep 25, 2012 at 23:43
  • The reason I'm curious is because I'm working on another question: stackoverflow.com/questions/12550694/…, and when I add an additional <comment> to an entry, that code also breaks. I can't see how to apply this solution to that more complex query. Sep 26, 2012 at 0:04
1

You've shown us an incorrect query and asked us to guess from this what you actually intended, which isn't necessarily possible. However, my guess would be that you want the where clause to return true if any of the several values of $v/product_info/unit is equal to '9291'. You have made the common mistake of assuming that the contains() function tests whether a set of values contains a particular value, whereas it actually tests whether a string contains a particular substring. To test whether a set of values contains a particular value, use the '=' operator:

where $v/product_info/unit = '9291'
1
  • That's a familiar name.... Anyway, I've tested this, and it works. I'm curious about how it differs from Kaipa's post. Sep 19, 2012 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.