mdonoughe
mdonoughe

Reputation: 525

XPath composition in XQuery

I two XPaths, f(x) and g(y), and some XML x.

x = <example>
  <a>
    <number>1</number>
  </a>
  <b>
    <letter>A</letter>
  </b>
  <c>
    <number>2</number>
  </c>
</example>

f(x) = /example/*

g(y) = /number|/letter

How do I write h(x) in XQuery such that h(x) = g(f(x)) for any g(x)? I don't know g(x) ahead of time so I cannot modify it. I can modify f(x) if necessary. All of this needs to happen in XQuery because it's part of an Oracle query.

h(x) = g(f(x)) = $data/example/*...?

Upvotes: 1

Views: 278

Answers (2)

Michael Kay
Michael Kay

Reputation: 163322

I may be confused about the question, but if I have understood it correctly then the answer is

let $f := FFFF return $f/(GGGG)

where FFFF and GGGG are the expressions corresponding to f(x) and g(y)

But I'm assuming that you got your example wrong, and when you wrote

g(y) = /number|/letter

you meant

g(y) = number|letter

i.e. a relative selection rather than an absolute selection.

Upvotes: 1

Mr. TA
Mr. TA

Reputation: 5359

One way would be to concatenate the strings:

h(x, g_postfix) = '$data/example/*' + g_postfix

Obviously taking care of '|' which would require splitting, concatenating and then joining.

Another way would be to pass the name of the "g" function and then create dynamic SQL to execute it in a loop over the node list that's returend by f().

Upvotes: 0

Related Questions