0
votes

I'm a newbie with Xquery. An post already exists on this query but I'm having problems when the XML has a prefix as follows:

Source XML:

enter code here
<?xml version="1.0" encoding="UTF-8"?>
<so:category>
    <bo:catid>1</bo:catid>
    <bo:cattext>sport</bo:cattext>
</so:category>

Xquery to change value that was provided in another post:

declare namespace local = "http://example.org";
declare namespace bo = "http://example1.org";

declare function local:copy-replace($element as element()) {

  if ($element/self::bo:catid)
  then <bo:catid>test</bo:catid>
  else element {node-name($element)}
               {$element/@*,
                for $child in $element/node()
                 return if ($child instance of element())
                        then local:copy-replace($child)
                       else $child
               }
};
 local:copy-replace(/*)

I have a prefix for elements in my XML document. So when I execute the query I get the following error:

ERROR - The prefix "bo" for element "bo:catid" is not bound.

I'm not sure how to handle the prefix & have searched for related subject on the internet. However, I cannot resolve this issue with information provided & need to know what I'm doing wrong.

1
What you have posted as your source "XML" is not namespace well-formed XML as it uses names with colons (e.g. bo:catid) without binding that used prefix bo in some namespace declaration.Martin Honnen

1 Answers

3
votes

Your XML is well-formed according to the XML 1.0 recommendation, but it is not namespace-well-formed according to XML Namespaces 1.0 (because it uses namespace prefixes that are not bound to any namespace URI). Most tools in the XML eco-system will only handle namespace-well-formed XML, and this is an absolute requirement for using XQuery.

Declaring namespace prefixes in the query doesn't get away from the requirement to have namespace-well-formed input.