I tried validating a very simple manually-written RDF online, using the W3C RDF Validator. To my surprise, it correctly resolved the URIs from the rdf namespace, but not from a different namespace (also from W3C). Why did this happen?
Let's take the example of
<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:org="http://www.w3.org/ns/org#" >
<rdf:Description rdf:about="https://stackexchange.com/">
<rdf:type rdf:resource="org:Organization"/>
</rdf:Description>
</rdf:RDF>
This validates, and gets parsed to
As you can see, the predicate (rdf:type) gets nicely expanded, and one can click on it. The object (org:Organization) doesn't get expanded at all, and also when I try clicking the link, it literally sends "org:Organization" to the browser, producing an error. But the namespace org has been defined just like the rdf namespace, and if I manually visit http://www.w3.org/ns/org#Organization, I get a Turtle document.
So, my question is: why doesn't it place http://www.w3.org/ns/org#Organization in the object? What should I change for the parser to do it properly?
The parser is doing it properly, actually. The problem is that you are trying to use an XML namespace prefix inside an attribute value (the value of the rdf:resource
attribute), rather than as a qualifier on an actual XML element/attribute name itself (such as rdf:type
, which is an XML element name).
Put differently: you can't use XML namespaces inside string values. The parser therefore doesn't try to "expand" the namespace, instead just parses the literal string value as a URI, and since org:Organization
is in fact a syntactically legal URI, that's what it produces.
To get your desired output, you will have to write out the full IRI string, like so:
<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:org="http://www.w3.org/ns/org#" >
<rdf:Description rdf:about="https://stackexchange.com/">
<rdf:type rdf:resource="http://www.w3.org/ns/org#Organization"/>
</rdf:Description>
</rdf:RDF>
or if you like, you can use abbreviated RDF/XML syntax, to get this:
<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:org="http://www.w3.org/ns/org#" >
<org:Organization rdf:about="https://stackexchange.com/" />
</rdf:RDF>
As an aside: RDF/XML is a notoriously tricky syntax to work with. There are other, far easier syntax formats for RDF, like for example Turtle. Just sayin'.