Solved

is there an equivalent XQUERY for this code?

Posted on 2013-06-12
9
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

734 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