xquery - BaseX: correct syntax to place multiple 'replace ' -
declare function local:stripns($name xs:string?) xs:string? { if(contains($name, ':')) substring($name, functx:index-of-string($name, ':') + 1) else $name }; $x in doc("test.xml")//*[@ref] let $tmp:=local:stripns($x/@ref) return replace value of node $x/@ref $tmp
i want strip namespace value of ref
, type
attribute. <test ref='haha:123' type='hehe:456'/>
should become <test ref='123' type='456'/>
. don't know correct syntax, below ideal .xqy file want:
declare function local:stripns($name xs:string?) xs:string? { if(contains($name, ':')) substring($name, functx:index-of-string($name, ':') + 1) else $name }; $x in doc('test.xml')//*[@ref] let $tmp:=local:stripns($x/@ref) return replace value of node $x/@ref $tmp, $x in doc('test.xml')//*[@type] let $tmp:=local:stripns($x/@type) return replace value of node $x/@ref $tmp
but contains syntax error:
[xudy0017] node can replaced once: attribute ref {"123"}.
$ basex -u test.xqy
use command above test. output written test.xml
.
the issue you're having typo in second flwor expression: try replace same attribute twice.
for $x in doc('test.xml')//*[@type] let $tmp:=local:stripns($x/@type) return replace value of node $x/@ref $tmp (: ^^^^ should @type :)
anyway, query overly complex. first of all, xquery knows substring-after($string, $token)
function you're rewriting yourself. can cut down function to
declare function local:stripns($name xs:string?) xs:string? { if(contains($name, ':')) substring-after($name, ':') else $name };
while @ same time removing functx dependency.
furthermore, can select multiple, different attributes single query, simplifying query to
for $attribute in doc('test.xml')//(@ref, @type) return replace value of node $attribute local:stripns($attribute)
finally, adding simple where
clause lets give whole function (and reducing number of updates attributes @ same time, speed query large documents):
for $attribute in doc('test.xml')//(@ref, @type) contains($attribute, ':') return replace value of node $attribute substring-after($attribute, ':')
Comments
Post a Comment