Xquery替换具有前缀的元素值

问题描述 投票:0回答:1

我是Xquery的新手。此查询已经存在一个帖子,但是当XML具有如下前缀时,我遇到了问题:

疼痛的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更改另一篇文章中提供的值:

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(/*)

我的XML文档中有元素的前缀。因此,当我执行查询时,我收到以下错误:

错误 - 元素“bo:catid”的前缀“bo”未绑定。

我不知道如何处理前缀并在互联网上搜索相关主题。但是,我无法通过提供的信息解决这个问题,需要知道我做错了什么。

xquery
1个回答
3
投票

根据XML 1.0建议,您的XML格式正确,但根据XML Namespaces 1.0,它不是名称空间良好的形式(因为它使用未绑定到任何名称空间URI的名称空间前缀)。 XML生态系统中的大多数工具只能处理格式良好的XML,这是使用XQuery的绝对要求。

在查询中声明名称空间前缀并不能避免需要具有命名空间良好的输入。

© www.soinside.com 2019 - 2024. All rights reserved.