• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

is there an equivalent XQUERY for this code?

I need to query an XML file for obtaining:
1) the total number of "/root/div" elements and the position
2) the position of the one with text() = "very"

The logic should pass through the file one one time (not two) for speed reason since it's a very long file, so could be this:

FIND ALL DIVS USING XPath(/root/div), ASSIGN TO x
VAR i = 0
VAR v = -1
LOOP THROUGH ALL x
    INCREMENT COUNTER i
    IF x' EQUALS 'very' AND v EQUALS -1
        v = i

Open in new window


Is it some feasible with an XQUERY?
0
lucavilla
Asked:
lucavilla
  • 6
  • 3
1 Solution
 
Geert BormansInformation ArchitectCommented:
Well, what do you mean with the concept "pass through the file once"?
If your XML is in a database it doesn't matter if you have the idnexes right
If you are doing this commandline with your BAseX or Saxon, the XML will be stored in memory first and you have direct access to all nodes, so the number of passes doesn't matter either
Unless you are using Saxon 9 enterprise edition in streaming mode?
0
 
Geert BormansInformation ArchitectCommented:
your pseudo code already is kind of naieve

FIND ALL DIVS USING XPath(/root/div), ASSIGN TO x
VAR i = 0
VAR v = -1
LOOP THROUGH ALL x

first line already walks through the document collecting indexes
line 4 goes again

untested

let $div := /root/div
return
<result>
<cnt>{count($div)}</cnt>
<v-index>{index-of($div, contains(., 'very'))}</v-index>
</result>
0
 
lucavillaAuthor Commented:
Hi Gertone!

For "pass through the file once" I think I meant that the loop through the whole file (in your example it could be this: "let $div := /root/div") should be done once, for speed.
I'm fascinated by your XQUERY and it seems to work too!

I'm making my first steps into this magic XQUERY and I love it more and more...
so does the "let" produces an array of values in this case? is "array" or "object" the appropriate term for referring to the datatype of this $div?  (just for learning)

Thanks again!
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Geert BormansInformation ArchitectCommented:
the "let" produces a "sequence" of "nodes"
"sequence" is the correct XPath terminology, it is very much like an array, but invariable
but it is nodes, so in context of their document
that is why a XSLT or XQuery processor needs to make references to nodes and not copies from nodes
0
 
lucavillaAuthor Commented:
Thanks Gertone for your precious help!
One last info: what do I have to study and use to modify (especially to insert and delete nodes) XML files? XSLT? XML DOM? XML DML?
0
 
Geert BormansInformation ArchitectCommented:
XML DOM is pretty unconvenient when it gets tricky

For inserting nodes, XSLT makes most sense
To get started, this is a very good book
http://shop.oreilly.com/product/9780596000530.do
the 1st edition is out of sale and covers XSLT1 only
I prefer that one, since grasping the added functionality is easy, grasping the basics is best done with a good book like this one.
You can get it in electronic version stil... or read it on safari
0
 
Geert BormansInformation ArchitectCommented:
note: I made an edit since the above reads a bit more awkward if I leave the word 'DOM' out :-)
0
 
lucavillaAuthor Commented:
Ah, 480 pages as first lecture to learn XLST?  are you sure?  :)
0
 
Geert BormansInformation ArchitectCommented:
yeah, half the book is code and there is an extensive reference and the last chapters aren't that important. It was my first book and I worked through it in less than a day. But it really helped me understand teh basics
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now