Newbie to XQuery. I am using XQIB (“XQuery In the Browser”, i.e. Zorba implemented in JavaScript) to process it client-side.
In the following test document, if I click the first button, “Add Paragraphs” and then the second button, “Make some paragraphs bold”, it works as expected. However, if I click “Add Paragraphs” twice (regardless of whether or not I click the second button in-between), the second button stops working.
Is this likely a bug in XQIB or does XQuery just behave differently than I expect?
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XQIB Test</title>
<meta charset="UTF-8"/>
<script type="text/javascript" src="mxqueryjs/mxqueryjs.nocache.js"></script>
<script type="application/xquery">
declare default element namespace "http://www.w3.org/1999/xhtml";
declare namespace hresume="http://microformats.org/profile/hresume";
declare namespace hcard="http://microformats.org/profile/hcard";
declare updating function local:addparas($loc, $evtObj) {
insert node <p>test1</p>
as last into b:dom()//body,
insert node <p>test2</p>
as last into b:dom()//body
};
b:addEventListener(b:dom()//input[@value='Add paragraphs'], "onclick", xs:QName("local:addparas"))
</script>
<script type="application/xquery">
declare updating function local:boldparas($loc, $evtObj) {
replace node b:dom()//p[.='test1'] with <p><b>test1</b></p>
};
b:addEventListener(b:dom()//input[@value='Make some parapgraphs bold'], "onclick", xs:QName("local:boldparas"))
</script>
</head>
<body>
<h1>Insert example</h1>
<input type="button" value="Add paragraphs" />
<input type="button" value="Make some parapgraphs bold" />
<p>Hey</p>
</body>
</html>
What XQIB should do is raise a runtime error. In the situation that you describe, the target expression of the replace in local:boldparas returns a sequence of multiple nodes, where according to the specification, it must be a single node:
You will achieve the desired result by iterating the replace operation as follows: