0
votes

I asked similiar to this question before but I'm still not handled this case correctly, firstly I'm saying that I am new to this XQuery language so configure the recursive strategy is really hard for me for now.

XQuery Sample Code to be executed :

declare variable $stuff :=
  <doc>
  <embed-session>
    <session>
          <title>Fred Smith</title>
     <session>
     <title>Ahmet Özcan</title>
     <session>
     <title>Erman Bey</title>
     </session>
     </session>
    </session>
    </embed-session>
  </doc>;

declare function local:change($node as node()) as element()
{
  typeswitch($node)
  case element() return 
    element { fn:node-name($node) } {
      attribute title {data($node/title)},
      $node/session ! local:change(.)
    }
  default return $node
};

local:change($stuff/embed-session/session)

The output of the code : 

<session title="Fred Smith">
  <session title="Ahmet Özcan">
    <session title="Erman Bey"/>
  </session>
</session>

It seems that it fits with my expectations but if I add one more embed session node to my stuff variable for example :

<embed-session>
<session>
<title>Ferhat Bey</title>
</session>
</embed-session>

to my parameter of the function it says that : [XPTY0004] Cannot treat item() sequence as node(): (..., )

The expected output of the code after add the embed-session mentioned below :

<session title="Fred Smith">
  <session title="Ahmet Özcan">
    <session title="Erman Bey"/>
  </session>
</session>

<session title="Ferhat Bey"/>

I know it is sequence but why I couldn't assign sequence to node as parameter ?

1
Please always add actual and expected output, along with any errors that occur. Code, that is not working is generally a bad description of what exactly you're trying to do. - Jens Erat
@JensErat I just want to describe my goal, as an algorithm because the main thing is for me syntax and the return types of the functionality, I've tried many things and I always got an exceptions so I couldn't put codes of my examples, decided to describe the way I need. - Hayra
@Hayra As Jens' already said, your question is lacking information. To be honest, I have no idea what you are trying to achieve and hence I can not help you. You didn't give any error description or describe what you want to do. As you said, your syntax looks very wrong for xquery, but how could we help you with that? If you want to learn the complete syntax, you might want to read a book or the specification. - dirkk
Also, you might want to search SO for some examples of recursive XQuery functions, e.g. stackoverflow.com/questions/2431470/… - dirkk

1 Answers

0
votes

It's always a good idea to declare the expected types of your function arguments. I think you are expecting $node to be a node, but in your function call you are supplying a sequence of nodes. element(x) only matches a singleton sequence, so it is not matching when you supply more than one. If you had written "$node as node()" then this simple error would have been caught much more quickly, and we would all have saved a lot of time.