简体   繁体   English

XQuery替换具有前缀的元素值

[英]Xquery replace an element value that has a prefix

I'm a newbie with Xquery. 我是Xquery的新手。 An post already exists on this query but I'm having problems when the XML has a prefix as follows: 此查询中已经存在一个帖子,但是当XML具有如下前缀时,我遇到了问题:

Source 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 to change value that was provided in another post: 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(/*)

I have a prefix for elements in my XML document. 我在XML文档中有一个元素前缀。 So when I execute the query I get the following error: 因此,当我执行查询时,出现以下错误:

ERROR - The prefix "bo" for element "bo:catid" is not bound. 错误-元素“ bo:catid”的前缀“ bo”未绑定。

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. 但是,我无法通过提供的信息解决此问题,并且需要知道我在做什么错。

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). 根据XML 1.0的建议,您的XML格式正确,但是根据XML Namespaces 1.0,它的名称空间格式不正确(因为它使用的名称空间前缀未绑定到任何名称空间URI)。 Most tools in the XML eco-system will only handle namespace-well-formed XML, and this is an absolute requirement for using XQuery. XML生态系统中的大多数工具只能处理命名空间格式良好的XML,这是使用XQuery的绝对要求。

Declaring namespace prefixes in the query doesn't get away from the requirement to have namespace-well-formed input. 在查询中声明名称空间前缀并不能避免使用具有名称空间格式的输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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