简体   繁体   English

为什么 W3C RDF 验证器不使用完整名称空间替换 URI?

[英]Why doesn't the W3C RDF validator replace the URI using the full namespace?

I tried validating a very simple manually-written RDF online, using the W3C RDF Validator.我尝试使用 W3C RDF Validator 在线验证一个非常简单的手动编写的 RDF。 To my surprise, it correctly resolved the URIs from the rdf namespace, but not from a different namespace (also from W3C).令我惊讶的是,它正确地解析了来自 rdf 命名空间的 URI,但不是来自不同的命名空间(也来自 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.如您所见,谓词 (rdf:type) 得到很好的扩展,并且可以单击它。 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. object (org:Organization) 根本没有展开,而且当我尝试单击该链接时,它确实向浏览器发送“org:Organization”,从而产生错误。 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.但是命名空间 org 已经像 rdf 命名空间一样定义了,如果我手动访问http://www.w3.org/ns/org#Organization ,我会得到一个 Turtle 文档。

So, my question is: why doesn't it place http://www.w3.org/ns/org#Organization in the object?所以,我的问题是:为什么不把http://www.w3.org/ns/org#Organization放在 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).问题是您试图在属性值( rdf:resource属性的值)中使用 XML 名称空间前缀,而不是作为实际 XML 元素/属性名称本身(例如rdf:type ,它是一个 XML 元素名称)。

Put differently: you can't use XML namespaces inside string values.换句话说:您不能在字符串值中使用 XML 命名空间。 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.因此,解析器不会尝试“扩展”命名空间,而只是将文字字符串值解析为 URI,并且由于org:Organization实际上是一个语法上合法的 URI,所以它会生成它。

To get your desired output, you will have to write out the full IRI string, like so:要获得所需的 output,您必须写出完整的 IRI 字符串,如下所示:

<?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:或者,如果您愿意,可以使用缩写的 RDF/XML 语法来获取:

<?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.顺便说一句:RDF/XML 是出了名的难以处理的语法。 There are other, far easier syntax formats for RDF, like for example Turtle . RDF 还有其他更简单的语法格式,例如Turtle Just sayin'.只是在说'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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