xquerybasexxquery-update

BaseX: Trying to move elements between namespaces in XQuery Update Facility


I would like to normalize XSD input file to put xs: before any XSD elements without xs: prepended with XQuery.

copy $c:=doc("test.xsd")
modify
(
  if($c/schema)
  then
  (
    for $item in $c//*
    where substring($item/text(), 1, 3) != "xs:"
    return rename node $item as concat("xs:", $item/text())
  )
  else ()
)
return $c

Running this xqy on an input XSD without xs: prepended returns input file unchanged. I do update the $c and return it. So what is wrong?

input file:

<?xml version="1.0"?>
<schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<element name="note" type="xs:string"/>

</schema>

expected output:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="note" type="xs:string"/>

</xs:schema>

PS: It seems that my question causes confusion. I need xs: present because I have another text processing program which only handles elements with xs: prefix.


Solution

  • This should work (it’s pretty similar to your approach):

    copy $doc := doc("test.xsd")
    modify (
      if($doc/*:schema) then (
        for $item in $doc//*
        let $name := name($item)
        where not(starts-with($name, 'xs:'))
        let $new-name := xs:QName('xs:' || replace($name, '^.+:', ''))
        return rename node $item as $new-name
      )
      else ()
    )
    return $doc