简体   繁体   中英

generate rdf file with XQuery: “xmlns”: invalid attibute node-name;

For every rdf file, there is a namespace declaration at the beginning of the file. I want to create the node below.

<rdf:RDF
xmlns="http://www.w3.org/2002/07/owl#">

I try to create it with element constructor:

declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace owl="http://www.w3.org/2002/07/owl#";

element {xs:QName("rdf:RDF")} {attribute xmlns {"http://www.w3.org/2002/07/owl#"}}

error from cmd line:

test.xqy>:20,48: dynamic error [err:XQDY0044]: "xmlns": invalid attibute node-name; raised at /tmp/zorba20151126-72875-uij83f/zorba-3.0/src/runtime/core/constructors.cpp:669

It looks like the attr xmlns is not allowed to be used like that. Are there ways around?

Your desired output is not actually valid, as it does not include the definition of the rdf namespace, and xmlns is not actually an attribute (technically, it is, but it is a special one), but a namespace declaration. Treating it as an ordinary attribute is going to raise an error, because the XML specification reserves that name to be used only for namespace identifiers. If you just do

declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace owl="http://www.w3.org/2002/07/owl#";

element {xs:QName("rdf:RDF")} {}

You will get valid output of the form,

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns-#"/>

This is valid, as it includes the declaration of the rdf namespace. What your syntax is asking for is a default namespace, which includes all elements which do not fall into another namespace. We can do that by a prolog statement:

declare default element namespace "http://www.w3.org/2002/07/owl#";
declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace owl="http://www.w3.org/2002/07/owl#";

element {xs:QName("rdf:RDF")} {}

which will append the namespace, provided an element exists that is not in the rdf namespace (the default namespace declaration is not added if no element exists in it). For example, replacing the element constructor with element {xs:QName("rdf:RDF")} {<t/>} (declaring an element t which is in the default namespace) produces

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <t xmlns="http://www.w3.org/2002/07/owl#"/>
</rdf:RDF>

which is equivalent to your desired output (as far as namespace semantics are concerned).

In XQuery 3.0, we can construct the namespace node directly. Using

declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace owl="http://www.w3.org/2002/07/owl#";

element {xs:QName("rdf:RDF")} {
    namespace {""} {"http://www.w3.org/2002/07/owl#"}
}

which produces

<rdf:RDF xmlns="http://www.w3.org/2002/07/owl#" 
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>

placing the default namespace declaration on the outer element.


  • All examples here were tested with Saxon-HE 9.7.0.2J .

What you are trying to do is to output an element containing a namespace declaration for a namespace which is not used in either the element name, or in the name of any of its attributes.

As @Matthew points out, you can do that in XQuery 3.0 using the namespace constructor expression.

In XQuery 1.0, provided that the element name is known statically (which I think is the case here), you can achieve what you want using a direct element constructor:

<rdf:RDF xmlns="http://www.w3.org/2002/07/owl#" 
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">{
   ... content goes here ...
</rdf:RDF>

If you use a computed element constructor then in XQuery 1.0 the desired effect cannot be achieved. The rules are here: https://www.w3.org/TR/xquery/#id-ns-nodes-on-elements . Note that the namespace declarations in the prolog have no effect except to associate URIs with prefixes that are actually used in the element constructor.

Trying to output namespace declarations as if they were ordinary attributes is strictly not allowed: remember that your query is producing an XDM tree, not lexical XML, and that in an XDM tree, namespaces and attributes are quite different animals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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