Phil
Phil

Reputation: 29

BaseX - insert node {...} into //.. for multiple nodes

I am currently facing a problem with the BaseX native XML-Database.

I have got a sample dataset where I want to check whether a certain attribute in a certain node exists and if it does then set a value "true". If it doesn't exist I want to insert a new attribute. This is my code for a single Node:

if(fn:exists(//Dataset[@attribute="2"]/@b)) then
  replace value of node //Dataset[@attribute="2"]/@b with "true"
else
  insert node (attribute { 'b' } { "CREATED!" }) into //Dataset[@attribute="2"]

The problem I am facing at the moment is that I cannot find a way of iterating through all nodes of the type "Dataset" for example and check every single node.. It always says "Single element or document expected as insert target".

Upvotes: 2

Views: 1260

Answers (1)

Paul Sweatte
Paul Sweatte

Reputation: 24617

Okay, Googled for a long time but 15 minutes more would have saved me from posting this question:

for $dataset in //mondial/Dataset
 let $DOCH := $dataset/@DOCH
 return
 if(fn:exists($DOCH)) then (
replace value of node $DOCH with "true")
else (
  insert node (attribute DOCH {"true"}) into $dataset)

Upvotes: 1

Related Questions