简体   繁体   中英

replace node in basex xml using xquery

I am trying to replace a node in an xml using the following code

String xquery_replace="xquery replace node CIDEM/ShopFloor/foo[/CIDEM/ShopFloor/ShopFloorID=1] with "+new_gbXML;
session.execute(xquery_replace);

So i want for example to change the foo node of the first ShopFloor node

The xml has the following content

<CIDEM>
    <ShopFloor>
        <ShopFloorID>1</ShopFloorID>
        <foo bar="2">
            <baz>there</baz>
        </foo>
    </ShopFloor>
  <ShopFloor>
      <ShopFloorID>2</ShopFloorID>
      <foo bar="5">
          <baz>there</baz>
      </foo>
  </ShopFloor>
</CIDEM>

And I am receiving the following error "[XUTY0008] Single element, text, attribute, comment or pi expected as replace target."

Any idea why?

The error message itself is telling whats wrong.

"[XUTY0008] Single element, text, attribute, comment or pi expected as replace target."

means that it is expecting an element or text or attribute... as a target to be replaced. But the path in your query is taking it nowhere. Read my comment for proper path.

For Ex: If you want to replace the value of the attribute bar for ShopFloorID with value 1 , then the path should be CIDEM/ShopFloor[ShopFlorrID="1"]/foo/@bar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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