Warm tip: This article is reproduced from stackoverflow.com, please click
rdf xml

What is the point of a namespaced root node in XML and where would such a namespace be defined?

发布于 2020-04-20 10:46:05

I'm currently looking into a SPARQL ontology definition, which is in XML - something I am quite hazy on.

I'm familiar with the concept of namespacing. i.e. an XML node like this:

<rdf:Description rdf:about="...">
    <rdf:type rdf:resource="..."/>
    <rdf:type rdf:resource="..."/>
    <rdfs:domain rdf:resource="..."/>
    <rdfs:range rdf:resource="..."/>
</rdf:Description>

is referencing the rdf and rdfs namespaces, which are defined in the XML document root node:

xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"

And I understand then that this indicates to any XML parsing application that the node then is effectively:

<Description about="...">
    <type resource="..."/>
    <type resource="..."/>
    <domain resource="..."/>
    <range resource="..."/>
</Description>

With the tags (Description, type, domain, and range) as well as the tag-attributes (about and resource) unique in the context of parsing rdf XML. Which I guess the parser needs rules for - but these tags would never get confused with tags of the same name but defined for a different purpose. i.e. xmlns="http://finanacial-instruments.com/xmlns", where tags of the same name are used in different contexts (please let me know if this does not make sense).

The question

In this particular XML document that I am looking at, the root node is itself namespaced:

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
    xmlns="http://meta.icos-cp.eu/ontologies/cpmeta/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

i.e. the root node is <rdf:RDF ...>. From my perspective this doesn't really matter since I already knew it was an RDF document. But what is the point of having a namespaced root node, and where is that namespace defined?

============== EDIT

Looking at the answer below, this link: http://www.jclark.com/xml/xmlns.htm helped me.

It includes the following:

<cars:part xmlns:cars="http://www.cars.com/xml"/>

maps to

<{http://www.cars.com/xml}part/>

I didn't realize that a namespace was also applicable to the tag in which the namespace is specified.

Questioner
Zach Smith
Viewed
30
Michael Kay 2020-02-05 18:24

James Clark provides a useful summary of how namespaces work at

http://www.jclark.com/xml/xmlns.htm

and (years later) a thoughtful blog on whether the design was done right at

https://blog.jclark.com/2010/01/xml-namespaces.html

(The comments are also worth reading. Most of them are from experts whose opinions are worth taking seriously.)

As regards the question "what is the point?", many people would argue that it achieves very little, but it was the fashionable thing to do at the time. Perhaps the reason it was fashionable is that people thought they were building in some kind of future-proofing, and keeping your options open for an unknown future is a common trait among good software designers.

As regards the question "where is the namespace defined", the answer is: it depends on the namespace. In many cases if you go to the URI associated with the namespace it will point you to a specification, but in other cases it won't, and you just have to search for it. As with all things in software, sometimes people use a namespace and never get around to writing any documentation for it.