xQuery递归函数/嵌套for循环(无界) - 在多个级别添加元素?

时间:2016-10-13 17:38:40

标签: xquery

是否可以使用xquery生成以下输出?输出和输入如下。基本上需要在输入xml中添加元素'Level'和值(1,1.1,1.2,1.2.3等),它没有'Level'元素。水平没有限制,可以达到任何水平。

必需的输出。

    <Root>
                    <Level>1</Level>
                    <Nodes>
                                    <Level>1.1</Level>
                                    <Param1>value1</Param1>
                                    <Param2>value2</Param2>
                    <Nodes>
                                    <Level>1.2</Level>
                                    <Param1>value5</Param1>
                                    <Param2>value7</Param2>
                                    <Nodes>
                                                    <Level>1.2.1</Level>
                                                    <Param1>value1</Param1>
                                                    <Param2>value8</Param2>
                                    </Nodes>
                    <Nodes>
                                    <Level>1.3</Level>
                                    <Param1>value3</Param1>
                                    <Param2>value6</Param2>
                                    <Nodes>
                                                    <Level>1.3.1</Level>
                                                    <Param1>value7</Param1>
                                                    <Param2>value9</Param2>
                                    </Nodes>
                                    <Nodes>
                                                    <Level>1.3.2</Level>
                                                    <Param1>value1</Param1>
                                                    <Param2>value8</Param2>
                                    </Nodes>
                    </Nodes>
                    </Nodes>
                    </Nodes> 
</Root>

输入

<Root>
                <Nodes>
                                <Param1>value1</Param1>
                                <Param2>value2</Param2>
                <Nodes>
                                <Param1>value5</Param1>
                                <Param2>value7</Param2>
                                <Nodes>
                                                <Param1>value1</Param1>
                                                <Param2>value8</Param2>
                                </Nodes>
                <Nodes>
                                <Param1>value3</Param1>
                                <Param2>value6</Param2>
                                <Nodes>
                                                <Param1>value7</Param1>
                                                <Param2>value9</Param2>
                                </Nodes>
                                <Nodes>
                                                <Param1>value1</Param1>
                                                <Param2>value8</Param2>
                                </Nodes>
                </Nodes>
                </Nodes>
                </Nodes>
</Root>

1 个答案:

答案 0 :(得分:1)

declare namespace xf = "http://tempuri.org/rb/";

declare function xf:injectLevel($node as element(), $plevel as xs:string, $clevel as xs:int) as element() 
{
    <Nodes>
      <Level>{ concat($plevel, '.', $clevel) }</Level>
      {$node/Param1}
      {$node/Param2}
      {
         for $cnodes at $pos1 in $node/Nodes
         return 
            xf:injectLevel($cnodes, concat($plevel, '.', $clevel) , xs:int($pos1))
      }
      </Nodes>
 };

 declare function xf:update($root as element()) as element() 
 {
    <root>
      <Level>1</Level>
      {
         for $Nodes at $pos in $root/Nodes
         return 
            xf:injectLevel($Nodes, xs:string(1), xs:int($pos))          
      }       
   </root>  
 };

 declare variable $root as element() external;

 xf:update($root)