Meaning of HTML prefix attribute (Open Graph Protocol)?
Asked Answered
E

2

61

I'm new to Facebook Open Graph Protocol. I have been trying to figure out what the meaning of the HTML prefix attribute is. The closest post on Stack Overflow I could find dealing with this subject is this post which was very helpful as it deals with what the differences are among the various possible syntaxes and which one to use.

However the post does not say anything about what this prefix attribute is or what it does. AFAIK this is not a standard HTML attribute. I was able to find this document which I think is where the W3C defines this attribute but was not able to make any sense of it.

Could someone please explain to me:

  1. What does the prefix attribute do?

  2. Can I write

    <html prefix="og:http://ogp.me/ns#">

    instead of

    <html prefix="og: http://ogp.me/ns#">

    or would that be a syntax error?

  3. Can I include multiple prefix attributes for any given HTML tag?

    <head prefix="a: http://www.aaa.com/ns#" prefix="b: http://www.bbb.com/ns#">

    Based on my knowledge of HTML, this would be wrong, but then prefix is a nonstandard attribute. So perhaps, what I would like to know is, can I write:

    <head prefix="a: http://www.aaa.com/ns# b: http://www.bbb.com/ns#">

    or does specifying multiple prefixes at any place in the HTML tree not make sense for some reason?

  4. If this prefix tag is not part of the HTML spec, how would submitting a page containing this attribute to a code validator ever result in my code being standards compliant?

Thank you for answering my questions.

Edile answered 14/1, 2015 at 12:54 Comment(1)
Good question. I, for one, am actually a little annoyed about the invention of a nonstandard attribute where a perfectly fine solution already exists. I am unable to find any rationale.Nimrod
H
62

First:

prefix is one of the attributes defined by the RDFa (Resource Description Framework in Attributes) extension. RDFa is used to implement the Semantic Web in web pages represented in many markup languages, like HTML and XML. Instead of having a web page telling the browser just how it should be structured, now you can also tell it what the page represents, like a Person, a List of Products, etc.

And for you to be able to semantically represent data on your web page, you need to use a semantic vocabulary, which tells the browser exactly that: what's being represented in your page. A popular semantic vocabulary is schema.org.

But sometimes a singular semantic vocabulary cannot represent and describe all of your web page. That's where the prefix attribute comes in:

What does the prefix attribute do?

The prefix attribute allows you to specify one or more semantic vocabularies being used. The following example uses two vocabularies to represent a Person that has a favorite Animal: schema.org and vocab.org.

<p vocab="http://schema.org/" prefix="ov: http://open.vocab.org/terms/" resource="#manu" typeof="Person">
    My name is <span property="name">MY_NAME</span>

    and my telephone is <span property="telephone">MY_TELEPHONE</span>.

    My favorite animal is the <span property="ov:preferredAnimal">FAVORITE_ANIMAL</span>.
</p>

Can I write <html prefix="og:http://ogp.me/ns#"> instead of <html prefix="og: http://ogp.me/ns#"> or would that be a syntax error?

Besides from not being able to find any examples on the web using the former syntax, from the specification of the RDFa Core you can see that at least one space is indeed mandatory:

prefix
    a white space separated list of prefix-name IRI pairs of the form
    NCName ':' ' '+ xsd:anyURI

Can I include multiple prefix attributes for any given HTML tag?

Yes, you can. From the specification of the prefix attribute given above, and from lots of examples given on the specification of the RDFa Core, the following is allowed:

<html
    prefix="foaf: http://xmlns.com/foaf/0.1/
            dc: http://purl.org/dc/terms/"
>
    <!-- your page -->
</html>

If this prefix tag is not part of the HTML spec, how would submitting a page containing this attribute to a code validator ever result in my code being standards compliant?

As mentioned by @wensveen, HTML5 supports the RDFa attributes out-of-the-box.

Hitchhike answered 25/3, 2016 at 23:25 Comment(2)
Just for reference, here it is the official explanation from w3.org: w3.org/TR/xhtml-rdfa-primer/#default_prefixesAncell
prefix attribute may also be omitted entirely for a good number of vocabularies thanks to RDFa Initial Contexts, helping simplify your code and reducing overall bandwidth consumption.Strathspey
N
2

It appears the prefix attribute is indeed defined in the RDFa specification you linked. While you need to specify the correct media type for XHTML documents (application/xhtml+xml) and the correct doctype (<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.1//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-2.dtd">) and probably some more XML push-ups, this isn't necessary for HTML5. In HTML5 you can use the RDFa-defined attributes out-of-the-box.

See: HTML+RDFa 1.1 specification

I don't know if there are any specific advantages of this way of mapping a prefix to a namespace. It isn't entirely clear to me either.

Nimrod answered 7/9, 2015 at 12:19 Comment(1)
Interesting - I knew about the SVG and Math additions to HTML5, but I never heard of HTML+RDFa. You can add RDFa attributes without any notice (Doctype, MIME type, namespace declaration) and still produce correct HTML5 according to the W3C validator. I didn't find a word about this on developer resources like MDN.Synchrotron

© 2022 - 2024 — McMap. All rights reserved.