简体   繁体   English

通过 Xquery 更改节点名称

[英]Changing node name via Xquery

I am trying to create an xquery which will change the name of the node from 'CNode3' to 'ChangedNode3' whenever the child element ('CCNode1') of 'CNode3' is 'CCValue1'.我正在尝试创建一个 xquery,只要“CNode3”的子元素(“CCNode1”)是“CCValue1”,它就会将节点的名称从“CNode3”更改为“ChangedNode3”。

<PNode>
  <CNode1>Node1</CNode1>
  <CNode2>Node2</CNode2>
  <CNode3>
     <CCNode1>CCValue1</CCNode1>
     <CCNode2>CCValue2</CCNode2>
  </CNode3>
  <CNode3>
     <CCNode1>CCValue3</CCNode1>
     <CCNode2>CCValue4</CCNode2>
  </CNode3></PNode>

Didn't get much help online for this particular scenario.对于这种特定情况,在线没有得到太多帮助。 I checked below link but it changes the name of all 'CNode3' to 'ChangedNode3'.我检查了下面的链接,但它将所有“CNode3”的名称更改为“ChangedNode3”。 PFB the link: http://www.xqueryfunctions.com/xq/functx_change-element-names-deep.html PFB链接: http : //www.xqueryfunctions.com/xq/functx_change-element-names-deep.html

The functx:change-element-names-deep() function only evaluates the element names. functx:change-element-names-deep()函数只计算元素名称。 You need a bit more customization.您需要更多的自定义。

You can use typeswitch in a local function:您可以在本地函数中使用typeswitch

declare function local:transform($nodes as node()*) as item()* {
    for $node in $nodes
    return 
        typeswitch($node)
            case text() return $node
            case comment() return $node
            case attribute() return $node
            case processing-instruction() return $node
            case element(CNode3) 
              return 
                if ($node[CCNode1 eq "CCValue1"]) then 
                  element ChangedNode3 {( $node/@*, local:transform($node/node()) )} 
                else 
                  element {name($node)} {($node/@*, local:transform($node/node()))}
            case element() 
              return element {name($node)} {($node/@*, local:transform($node/node()))}
            default return local:transform($node/node())
};

A full description of thetypeswitch transformation pattern . typeswitch 转换模式的完整描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM