html 文档中元数据的最佳实践?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10533764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:20:19  来源:igfitidea点击:

Best practice for meta data in a html document?

htmlmeta-tagsw3c-validation

提问by Nathan Russell

I work on a large scale, high volume, public facing web application. The successful operation of the application is very important to the business, and so there are a number of MI tools that run against it.

我从事大规模、高容量、面向公众的 Web 应用程序。应用程序的成功运行对业务非常重要,因此有许多 MI 工具与之运行。

One of these MI tools essentially looks at the html that is sent to the browser for each page request (I've simplified it quite a lot, but for the purpose of this question, its a tool that does some analysis on the html)

其中一个 MI 工具本质上是查看为每个页面请求发送到浏览器的 html(我已经对其进行了大量简化,但就这个问题而言,它是一个对 html 进行一些分析的工具)

For this MI tool to get the data it needs, we put meta data in the head element. Currently we do it as html comments:

为了让这个 MI 工具获取它需要的数据,我们将元数据放在 head 元素中。目前我们将其作为 html 注释:

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" class="">
<head>
    <!-- details = 52:AS6[rxSdsMd4RgYXJgeabsRAVBZ:0406139009] -->
    <!-- policy id = 1234567890 -->
    <!-- party id = 0987654321 -->
    <!-- email address = [email protected] -->
    <!-- error = 49 -->
    <!-- subsessionid = bffd5bc0-a03e-42e5-a531-50529dae57e3-->
    ...

And the tool simply looks for a given meta data comment with a regex

该工具只是使用正则表达式查找给定的元数据注释

As this data is meta data, I'd like to change it to html meta tags because it feels semantically correct. Something like this:

由于此数据是元数据,我想将其更改为 html 元标记,因为它在语义上是正确的。像这样的东西:

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" class="">
<head>
    <meta name="details" content="52:AS6[rxSdsMd4RgYXJgeabsRAVBZ:0406139009]" />
    <meta name="policyId" content="1234567890" />
    <meta name="partyId" content="0987654321" />
    <meta name="emailAddress" content="[email protected]" />
    <meta name="error" content="49" />
    <meta name="subsessionid" content="bffd5bc0-a03e-42e5-a531-50529dae57e3" />
    ...

This feels more semantic, and I can get the MI tool to work with it no problem - just a case of changing the regexes. However it now gives me a problem with the w3c validator. It wont validate because the meta names I'm using are not recognised. I get the error "Bad value details for attribute name on element meta: Keyword details is not registered." and it suggests I register these name values on the WHATWG wiki.

这感觉更语义化,我可以让 MI 工具毫无问题地使用它 - 只是更改正则表达式的一种情况。但是,它现在给我带来了 w3c 验证器的问题。它不会验证,因为我使用的元名称无法识别。我收到错误“元素元上的属性名称的错误值详细信息:未注册关键字详细信息。” 它建议我在 WHATWG wiki 上注册这些名称值。

Whilst I could do this it doesn't feel right. Some of my meta tags are 'generic' (such as error and emailAddress) so I could probably find an already registered name value and use that. However, most of them are industry/organisation specific. It feels wrong to register a public name value called subsessionid or partyId as these are specific to my organisation and the application.

虽然我可以这样做,但感觉不对。我的一些元标记是“通用的”(例如错误和电子邮件地址),因此我可能会找到一个已经注册的名称值并使用它。但是,它们中的大多数是特定于行业/组织的。注册一个名为 subsessionid 或 partyId 的公共名称值感觉是错误的,因为它们特定于我的组织和应用程序。

So, the question is - what is considered best practice in this case? Should I leave them as html comments? Should I use meta tags as above and not worry that w3c validation fails? (though that is increasingly important to the organisation) Should I attempt to register my meta name values on WHATWG wiki, but knowing they are not very generic? Or is there another solution?

所以,问题是 - 在这种情况下,什么被认为是最佳实践?我应该将它们保留为 html 评论吗?我应该像上面一样使用元标记而不用担心 w3c 验证失败吗?(尽管这对组织来说越来越重要)我是否应该尝试在 WHATWG wiki 上注册我的元名称值,但知道它们不是很通用?或者还有其他解决方案吗?

Appreciate your thoughts, cheers

欣赏你的想法,加油

Nathan

弥敦道



Edited to show the the final solution:

编辑以显示最终解决方案:

The full answer I'm going with is as follows. Its based on Rich Bradshaws answer, so his is the accepted one, but this is what I'm going with for completeness:

我要使用的完整答案如下。它基于 Rich Bradshaws 的回答,所以他的回答是公认的,但为了完整起见,这是我要做的:

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" class="">
<head>
    <meta name="application-name" content="Our app name" 
        data-details="52:AS6[rxSdsMd4RgYXJgeabsRAVBZ:0406139009]" 
        data-policyId="1234567890"
        data-partyId="0987654321"
        data-emailAddress="[email protected]"
        data-error="49"
        data-subsessionid="bffd5bc0-a03e-42e5-a531-50529dae57e3"
    />
    ...

This validates, so all boxes ticked :)

这验证了,所以所有的框都打勾:)

采纳答案by Rich Bradshaw

W3C validation is meaningless. HTML != XML, so there isn't any schema to validate it. No browser will choke because you added a meta element with an unregistered name. If you really are worried, you could use the data attribute on a meta element like:

W3C 验证毫无意义。HTML != XML,所以没有任何模式来验证它。没有浏览器会因为您添加了一个带有未注册名称的元元素而窒息。如果您真的很担心,您可以在元元素上使用 data 属性,例如:

<meta data-details="52:AS6[rxSdsMd4RgYXJgeabsRAVBZ:0406139009]" data-policyId="0123456789" />

at least then you know no future spec will give meaning to your data.

至少那时你知道没有未来的规范会给你的数据带来意义。

For more info read: http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#custom-data-attribute

有关更多信息,请阅读:http: //www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#custom-data-attribute

回答by unor

While your example may work, note that the keyword application-nameis for Web applicationsonly.

虽然您的示例可能有效,但请注意该关键字application-name仅适用于Web 应用程序

For usual webpages not being web applications, or if no application-nameshall be given, see some alternatives:

对于不是 Web 应用程序的普通网页,或者如果application-name不应给出,请参阅一些替代方案:

Using data-*attributes in the head

使用data-*的属性head

No need for a metaelement.

不需要meta元素。

<!DOCTYPE html>
<html>
<head
    data-details="52:AS6[rxSdsMd4RgYXJgeabsRAVBZ:0406139009]" 
    data-policyId="1234567890"
    data-partyId="0987654321"
    data-emailAddress="[email protected]"
    data-error="49"
    data-subsessionid="bffd5bc0-a03e-42e5-a531-50529dae57e3">
</head>

Using Microdata

使用微数据

You could create a vocabulary, but that's not required for local use.

您可以创建词汇表,但这不是本地使用所必需的

<!DOCTYPE html>
<html>
<head itemscope>
  <meta itemprop="details" content="52:AS6[rxSdsMd4RgYXJgeabsRAVBZ:0406139009]" />
  <meta itemprop="policyId" content="1234567890" />
  <meta itemprop="partyId" content="0987654321" />
  <link itemprop="emailAddress" href="mailto:[email protected]" /> <!-- or use a meta element if you don't want to provide a full URI with "mailto:" scheme -->
  <meta itemprop="error" content="49" />
  <meta itemprop="subsessionid" content="bffd5bc0-a03e-42e5-a531-50529dae57e3" />
</head>

Using data in a script

使用数据在一个 script

The scriptelement can be used for data blocks. You can choose any format that suits your needs. Example with plain text:

script元素可用于数据块。您可以选择适合您需要的任何格式。纯文本示例:

<!DOCTYPE html>
<html>
<head>
  <script type="text/plain">
    details = 52:AS6[rxSdsMd4RgYXJgeabsRAVBZ:0406139009]
    policyId = 1234567890
    partyId = 0987654321
    emailAddress = [email protected]
    error = 49
    subsessionid = bffd5bc0-a03e-42e5-a531-50529dae57e3
  </script>
</head>

回答by Onheiron

What if you try using the data- format to add a custom attribute to them, something like data-type or data-name and omitting the real name attribute or maybe setting it all to "abstract" or something (I donno if the validator will give problems for repeated meta names):

如果您尝试使用数据格式向它们添加自定义属性,例如 data-type 或 data-name 并省略真实名称属性,或者可能将其全部设置为“抽象”或其他内容(我不知道验证器是否会给出重复元名称的问题):

<meta data-name="details" content="52:AS6[rxSdsMd4RgYXJgeabsRAVBZ:0406139009]" />

So you could reference to that data-name to work with your meta stuff...

所以你可以参考那个数据名称来处理你的元数据......

http://html5doctor.com/html5-custom-data-attributes/

http://html5doctor.com/html5-custom-data-attributes/

回答by Stef Robinson

Either option would technically work, although the solution could come down to how your organisation feels about page validation.

任一选项在技术上都可行,尽管解决方案可能归结为您的组织对页面验证的看法。

As you say, adding information into custom metadata tags will invalidate your markup.

正如您所说,将信息添加到自定义元数据标签将使您的标记无效。

For my organisation, page validation is part of technical accessibility and is considered very important. Doing anything that would prevent pages from validating would not be allowed.

对于我的组织,页面验证是技术可访问性的一部分,被认为非常重要。不允许做任何会阻止页面验证的事情。

I wouldn't attempt to register new metadata names and values as these are specific to your organisation and not for public use.

我不会尝试注册新的元数据名称和值,因为它们特定于您的组织而不是供公共使用。

I would probably leave this information as HTML comments if this is already working for your organisation.

如果这已经适用于您的组织,我可能会将这些信息保留为 HTML 注释。