Question

I have a function which returns item()*. The output of this function is something like the following:

<Root>
  <Value>
    <Year>1999</Year>
    <A>50</A>
    <B>100</B>
  </Value>
  <Value>
    <Year>2000</Year>
    <A>50</A>
    <B>100</B>
  <Value>
</Root>

This output is supposed to be passed to a second function. I want to be able to parse this and return a different output. I've tried converting the output to a string using fn:parse-xml($output) but I get an error saying No text allowed before root element. However, if I declare a local variable inside the second function and assign it the string output it works.

This works:

declare function function2($outputFromFunction1 as item()*) item()*{
  let sToX := fn:parse-xml(" <Root>
      <Value>
        <Year>1999</Year>
        <A>50</A>
        <B>100</B>
      </Value>
      <Value>
        <Year>2000</Year>
        <A>50</A>
        <B>100</B>
      <Value>
    </Root>")...
};

I am able to parse this variable (for $Value in $sToX/Value...).

Why does it work in this case and not when I use the argument that is passed in?

Was it helpful?

Solution

When the function returns xml-elements as item() you do not need to parse them. They already are in parsed form.

Just use:

for $Value in $outputFromFunction/Value

(If you call parse-xml on this, it is first converted to a string, i.e. "199950100200050100")

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top