Jump to content

XQuery/XPath: question


Izee

Recommended Posts

Hi, I'm working on a function that receives XML tags and insert the actual parameter into an XPath expressions to retrieve documents with those tags.The problem is that if I use the parameter to move the context node ( / ) the query works, but I need to insert it like an option ( [...] )So:for $d in doc("blabla.xml")//tag/tag2/$actualparameter <--- this worksfor $d in doc("blabla.xml")//tag/tag2[$actualparameter] <--- this doesn't workI think that the actual parameter contains elements, so it's the same if I write:for $d in doc("blabla.xml")//tag/tag2/<tagsent><tagsent2>A</tagsent2></tagsent>(works)orfor $d in doc("blabla.xml")//tag/tag2[<tagsent><tagsent2>A</tagsent2></tagsent>](doesn't work)What can I do?Thank you and sorry for my english :)

Link to comment
Share on other sites

Strange... in my opinion, neither of those queries should work. Perhaps that's just the risk of using a working draft specification...variables should contain only strings, or at least XSLT ones. If it's a PHP variable, then yes. I guess it can carry anything, but still, neither of those queries seems valid.And what do you mean by "works"? Selects something or returning an error?If it's returning an error, that's probably because of the element content.If it's just not selecting anything, then I guess the node set is converted to a string after all. So then the problem is what are you actually trying to select?The first "working" expression selects all element "A"s located inside tag2 located at element tag located anywhere inside document blabla.xml. The second expression selects all tag2 elements which contain an "A" element and are located inside element tag located somewhere inside document blabla.xml.Note the difference: In the first case, you select "A" childs of tag2 and in the second case you select tag2's themselves.

Link to comment
Share on other sites

Going on Boen's (aka XML Sifu) reply I think I can see what's happening here and make a bit more sense of the post.I think that you are submitting an invalid parameter.The first statement (as Boen said) selects all of the child elements with the value A.I have no knowledge of PHP whatsoever so disregard this if it sounds like b*ll*cks, but surely the second statement should be more like:for $d in doc("blabla.xml")//tag/tag2[tagsent/tagsent2=A]to mirror the effects of the first one (I'm assuming that XPATH syntax will work here)D

Link to comment
Share on other sites

"Works" means that gives me what I need. The problem was that I need it as an option.And because I have to specify what kind of parameter is, they are elements:declare function p:funct($input as element(input)*)(: etc... :)I found this solution: maybe was only my Xpath ignorance :Pfor $d in doc("blabla.xml")//tag/tag2[tagsent=$actualparameter]Where the parameter is p:funct(<tagsent><tagsent2>A</tagsent2></tagsent>)I hope it is clear.But now I have a new problem. I have a variable $input like this:<input><pitch><step>C</step><octave>4</octave></pitch><pitch><step>D</step><octave>4</octave></pitch><pitch><step>E</step><octave>4</octave></pitch></input>I have to find these elements in sequence into an XML file. They may occur several times.let $n := count($input/pitch)for $d in doc("musicblabla.xml")//measure/note[pitch=$input/pitch[position()=1]]for $i in (1 to $n) return if ($i = 2) (: if it's the first time :) then $d[pitch=$input/pitch[position()=1]] (: print the match with the first note of input :) else $d[pitch=$input/pitch[position()=$i]] (: the second, third time etc... :)It seems like it does not increment $i, so the position also does not increment.Something like 11111, 22222, 33333What can I do?

Link to comment
Share on other sites

Pitch, step, octave... using XML to store Musical data, are we? Cool :) . Anyway.This... um... query of yours. I can only say "OMG" as in "Oh My God!". WTF (as in... please tell me you know as in what) are you trying to do here?I don't know much PHP either (it's not my type) and XQuery is not exactly my friend yet (we're just getting to know each other... not like my loved one XPath and her even more beatiful sister XSLT).

Link to comment
Share on other sites

Yes musical data :) I'm just getting to know XQuery and I'm starting to get mad. I can really say that PHP is my type. But for this task I think XQuery should be better. In this query ( I don't really know what you mean with WTF but i can imagine :) )the only problem is: incremet that variable. Why doesn't it work?Mad really mad. And time is going away.Anyway thank you for support :)

Link to comment
Share on other sites

Isn't incrementing in PHP something like $i++?I mean, whatever are you trying to do, it's a PHP problem, not an XQuery one.Oh and "WTF" means "What The F*ck".

Link to comment
Share on other sites

Isn't incrementing in PHP something like $i++?I mean, whatever are you trying to do, it's a PHP problem, not an XQuery one.Oh and "WTF" means "What The F*ck".

Yes, incrementing is $i++ in PHP but actually I'm not using PHP for this query. I mean: all the code you see there is XQuery only.Maybe I'm trying do to something complex for XQuery? Do I really have to use PHP to make this query work? Maybe if I don't want to get mad it could be the solution.WTF... ok I imagined correctly :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...