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?
lucavillaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Geert BormansConnect With a Mentor Information 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
 
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
 
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.