Solved

is there an equivalent XQUERY for this code?

Posted on 2013-06-12
9
163 Views
Last Modified: 2013-06-14
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
Comment
Question by:lucavilla
  • 6
  • 3
9 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39240366
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
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 39240382
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
 

Author Comment

by:lucavilla
ID: 39240480
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39240511
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:lucavilla
ID: 39240895
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39240911
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39240928
note: I made an edit since the above reads a bit more awkward if I leave the word 'DOM' out :-)
0
 

Author Comment

by:lucavilla
ID: 39240983
Ah, 480 pages as first lecture to learn XLST?  are you sure?  :)
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39241981
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now