代码之家  ›  专栏  ›  技术社区  ›  IgnacioHR

XQuery更新以包装现有节点

  •  1
  • IgnacioHR  · 技术社区  · 9 年前

    我试图使用XQuery update将一个节点与另一个新节点包装在一起,但我遇到了几个不同的错误,让我举一个简短的示例:

    初始XML为

    <a id="test">
      <title>title</title>
    </a>
    

    XQuery代码为:

    copy $x := db:open('testdb')/a[@id eq 'test']/title 
    modify replace node $x 
    with <b>{$x}</b> 
    return <ok/>
    

    我正在使用 basex 作为周围的数据库,这就是我使用db:open函数的原因。

    <a id="test">
      <b>
        <title>title</title>
      </b>
    </a>
    

    但我收到一个错误:

    [XUDY0009] Target has no parent: element title {...}.
    

    注:查询结果:

    db:open('testdb')/a[@id eq 'test']/title
    

    <title>title</title>
    

    如果我修改查询如下

    copy $x := db:open('testdb')/a[@id eq 'test']/title 
    modify replace node db:open('testdb')/a[@id eq 'test']/title
    with <b>{$x}</b> 
    return <ok/>
    

    那么错误是

    [XUDY0014] Node was not created by copy clause: element title {...}.
    

    1 回复  |  直到 9 年前
        1
  •  3
  •   Christian Grün    9 年前

    copy $a := db:open('testdb')/a[@id eq 'test']
    modify replace node $a/title with <b>{ $a/title }</b> 
    return $a
    

    在BaseX中 update

    db:open('testdb')/a[@id eq 'test'] update {
      replace node title with <b>{ title }</b> 
    }
    

    copy :

    let $title := db:open('testdb')/a[@id eq 'test']/title 
    return replace node $title with <b>{ $title }</b> 
    
    推荐文章