0

i've just made up an example to illustrate what I will ask you:

=> Is it ok to create a variable, put data in it, and to manipulate the return of this variable the same way I would manipulate a path??

This code below ,

  let $variable :=(for $pou in blabla/blabla
     return (element thing {attribute status {data($pou/name)},
     let $thing2 := for $z in $pou/bacon[@status="c"]
        return element thing3 {data($z/code)} return($thing2) })) return()

would do something like

<thing>
<thing2><thing3></thing3>
</thing2>
</thing>

is it ok to access a tag present in the structure created previously with "$variable/thing2" ???

  let $variable :=(for $pou in adress/blablalba/adresse/blabla/blabla
     return (element thing {attribute status {data($pou/name)},
     let $thing2 := for $z in $pou/bacon[@status="c"]
        return element thing3 {data($z/code)} return($thing2) })) return($variable/thing2)

In my real project I created multiple variables that display correctly when I launch it, but when i try to access/refer to them it does not work, I start to believe than you cannot access what was just made inside a variable the same way? But i'm a bit stuck!

I hope you understand my problem, cause I admit it is really bad explained

Cheers

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Raphael St
  • 661
  • 8
  • 23
  • 1
    In the end you're dealing with the same problem like [this question on implementing a count](http://stackoverflow.com/questions/10294677/updating-counter-in-xquery/10295046#10295046). Variables in XQuery are immutable. The highest-voted answer has does not explain the problem in detail (but posts a solution for that particular problem), [my one mainly describes the problem](http://stackoverflow.com/a/10295046/695343). If you'd like help with solving your particular problem, make sure to _explain what you want to do_ and _add some data_. Read about how to post an [SSCCE](http://www.sscce.org). – Jens Erat Nov 28 '13 at 23:29

1 Answers1

1

Yes, you can do what you want. You're getting errors not because what you want to do is impossible but because your path expressions are not correct.

To illustrate that XPath expressions can successfully navigate inside variables whose values are constructed in the query, consider the following query:

let $variable := <a><b>10</b><b>20</b><b>30</b></a>
return $variable/b[number(.) lt 25]

This returns the first two b elements among the children of the a element which is the value of $variable.

In your examples, you are binding $variable to a sequence of thing elements, each of which contains a sequence of thing3 elements. You then seek to retrieve a thing2 element, which cannot possibly exist (the only occurrence of the string thing2 in your queries is as the name of a variable).

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
  • Thank you for this answer, how do I select only the current node? I tried self::node() like : $variable/self::node(), but i've got the whole thing with the children!! How do i only select the node ?? – Raphael St Nov 30 '13 at 17:26
  • You don't have (in my small example) a node of the form `` -- you have an `a` element with children. There is no node of the form `` around for you to select; if you want one, you will need to construct one. – C. M. Sperberg-McQueen Nov 30 '13 at 17:43
  • How do I refer to the second tag "b" or the third tag "b" ?? I'm stuck – Raphael St Nov 30 '13 at 18:12
  • 1
    `$variable/b[2]` or `$variable/b[3]`. It may pay to spend some time reviewing an XPath tutorial. – C. M. Sperberg-McQueen Dec 01 '13 at 02:55