HTML5擴(kuò)展itemscope,?itemtype,?itemprop微數(shù)據(jù)的解釋和應(yīng)用
一、微數(shù)據(jù)是?
一個頁面的內(nèi)容,例如人物、事件或評論不僅要給用戶看,還要讓機(jī)器可識別。而目前機(jī)器智能程度有限,要讓其知會特定內(nèi)容含義,我們需要使用規(guī)定的標(biāo)簽、屬性名以及特定用法等。舉個簡單例子,我們使用<h1>
標(biāo)簽包裹頁面標(biāo)題信息就是為了讓機(jī)器識別(搜索引擎 – SEO)。
而微數(shù)據(jù)是什么呢?在我看來,微數(shù)據(jù)也是為了方便機(jī)器識別而產(chǎn)生的東西。其有特定的規(guī)范,有特定的格式??梢载S富搜索引擎的網(wǎng)頁摘要。
先來看看比較官方的解釋:HTML5 微數(shù)據(jù)規(guī)范是一種標(biāo)記內(nèi)容以描述特定類型的信息,例如評論、人物信息或事件。每種信息都描述特定類型的項(xiàng),例如人物、事件或評論。例如,事件可以包含 venue、starting time、name 和 category 屬性。
微數(shù)據(jù)使用 HTML 標(biāo)記(常為 <span> 或 <div>)中的簡單屬性為項(xiàng)和屬性指定簡要的描述性名稱。
上面的釋義過于學(xué)術(shù)化,我們可以將其擱置一邊,先看個簡單的關(guān)于微數(shù)據(jù)的例子。平時,我們要在頁面上顯示對一個人的描述,HTML代碼可能如下:
<div> 我的名字是王富強(qiáng),但大家叫我小強(qiáng)。我的個人首頁是: <a >www.example.com</a> 我住在上海市富貴新村。我是工程師,目前在財富科技公司上班。 </div>
而如果使用針對人物的微數(shù)據(jù)標(biāo)記,則HTML會如下:
<div itemscope itemtype="http://data-vocabulary.org/Person"> 我的名字是<span itemprop="name">王富強(qiáng)</span>, 但大家叫我<span itemprop="nickname">小強(qiáng)</span>。 我的個人首頁是: <a itemprop="url">www.example.com</a> 我住在上海市富貴新村。我是<span itemprop="title">工程師</span>, 目前在<span itemprop="affiliation">財富科技公司</span>上班。 </div>
您會發(fā)現(xiàn),HTML代碼量多了不少,還出現(xiàn)了很多自定義的屬性,如itemscope
, itemtype
, itemprop
等。這些屬性就是方面機(jī)器識別的特定的標(biāo)記。其含義等依次如下:
itemscope
定義一組名值對,稱為項(xiàng)。
itemprop=”屬性名”
添加一個數(shù)據(jù)項(xiàng)屬性。這個屬性名可以是個單詞或是個URL,與元素包含的文本值相關(guān):
對于大部分元素,屬性名值就是元素標(biāo)簽里面的文本值(不是所有標(biāo)簽)。 對于有URL屬性的元素,該值就是URL(如<img src="">
, <a href="">
, <object data="">
等)。 對于<time>
元素,該值就是datetime=""
屬性。 對于<meta itemprop="" content="">
, 該值就是content=""
屬性。
itemref=””
允許微數(shù)據(jù)項(xiàng)通過指向特定ID(含有需要屬性的元素)包含非后代屬性。
itemtype=””
微數(shù)據(jù)定義的類型。其值為URL,扮演詞匯表名稱的作用。
itemid=””
允許詞匯表給微數(shù)據(jù)項(xiàng)定義一個全局標(biāo)識符,例如書的ISBN數(shù)值,在同樣元素上使用itemid
作為數(shù)據(jù)項(xiàng)的itemscope
和itemtype
屬性。
二、微數(shù)據(jù)語法
itemscope和itemprop
先來個小例子:
<p itemscope>下周我要去<span itemprop="name">韓紅剛</span>家拔蘿卜</p>
<p>
元素上的itemscope
使其成為了一個微數(shù)據(jù)項(xiàng),其子元素上itemprop
屬性的值name
為詞匯表中的一個關(guān)鍵屬性。一個微數(shù)據(jù)項(xiàng)至少有一個驗(yàn)證的itemprop
.
itemprop
后面的名稱可以是單詞,也可以是URL,使用URL讓這個名稱全局唯一。如果使用單詞的話,最好使用詞匯表,這個單詞在該詞匯表中有定義,同樣可以讓名稱唯一。
itemprop值
對于一些元素,itemprop
值來自元素的屬性,像是datetime
屬性或是content
屬性。還是拔蘿卜的例子:
<p itemscope><time itemprop="date" datetime="2011-12-05">下周</time>我要去 <a itemprop="url" rel="external nofollow" rel="external nofollow" >韓紅剛</a>家拔蘿卜</p>
定義了兩個itemprop
下的屬性值url
和date
,所包含的值就是一個url地址(不是元素內(nèi)的文本值韓紅剛
)和一個特定格式的時間。
在微數(shù)據(jù)中,下面的元素以它們的URLs為值:
<a href="">
<area href="">
<audio src="">
<embed src="">
<iframe src="">
<img src="">
<link href="">
<object data="">
<source src="">
<video src="">
相反,以下HTML5元素URL包含屬性不作為屬性值使用:
<base href="">
<script src="">
<input src="">
上面兩個例子,我們可以來個簡單的整合,如下:
<p itemscope><time itemprop="date" datetime="2011-12-05">下周</time>我要去 <a itemprop="url" rel="external nofollow" rel="external nofollow" ><span itemprop="name">韓紅剛</span></a>家拔蘿卜</p>
將itemprop="name"
嵌入在鏈接之中了。
嵌套項(xiàng)
我們可以給包含itemprop
的元素添加itemscope
插入嵌套項(xiàng)。
<p itemscope><span itemprop="name">五月天</span>主唱是<span itemprop="members" itemscope><span itemprop="name">阿信</span>。</span></p>
定義了一個項(xiàng),有兩個關(guān)鍵字屬性:name
和members
。該name
是五月天,members
則是嵌套項(xiàng),包含了一個值為阿信
的屬性name
。注意到members
并無文本值。
像上面p標(biāo)簽這種父輩元素沒有任何一個微數(shù)據(jù)項(xiàng)的微數(shù)據(jù)項(xiàng)稱為“頂級微數(shù)據(jù)項(xiàng)”。微數(shù)據(jù)API返回的是頂級數(shù)據(jù)項(xiàng)及其對應(yīng)的屬性,以及其中嵌套的子數(shù)據(jù)項(xiàng)。
多屬性
先看例子:
<span itemprop="members" itemscope>前S˙H˙E 的成員是 <span itemprop="name">任家萱</span>, <span itemprop="name">田馥甄</span>和 <span itemprop="name">陳嘉樺</span>.</span>
項(xiàng)目可以包含不同值的多個屬性。例如上例name
屬性定義了3個值:任家萱、田馥甄和陳嘉樺。
同一個元素同樣也可以有多個屬性關(guān)鍵名稱(用空格分隔),例如下面這個例子:
<p itemscope><span itemprop="name call">志玲姐</span> 是個美女。</p>
“志玲姐”即是名字,又是稱謂。
通過itemref頁面內(nèi)引用
還是先看個例子吧:
<p itemscope itemref="band-members">后天我要去看<span itemprop="name"> S˙H˙E</span>的演唱會,好興奮哈!</p> …… <span id="band-members" itemprop="members" itemscope>S˙H˙E 的成員是 <span itemprop="name">任家萱</span>, <span itemprop="name">田馥甄</span>和 <span itemprop="name">陳嘉樺</span>.</span>
上面的定義通過引用ID band-members
包含了members
項(xiàng)的三個成員名稱屬性,每個都是不一樣的值。
使用meta添加內(nèi)容
如果你想添加的文本并不是頁面內(nèi)容的一部分,你可以在<meta>
元素上使用content
屬性:<meta itemprop="" content="">
舉個小例子:
<p itemscope><span itemprop="name" itemscope> 鐘xx<meta itemprop="likes" content="twins成員"> </span>攝影愛好者陳老師的粉絲。</p>
不會,有些遺憾的是,某些瀏覽器會自動把<meta>
元素移動到head
標(biāo)簽中。比較靠譜的做法是使用itemref
做頁面內(nèi)引用,這樣即使瀏覽器移除了,一些工具啥的還是能夠識別出微數(shù)據(jù)。具體做法如下:
<p itemscope><span itemprop="name" itemscope itemref="meta-likes"> 鐘xx<meta id="meta-likes" itemprop="likes" content="twins成員"> </span>攝影愛好者陳老師的粉絲。</p>
項(xiàng)類型(itemtype)及全局唯一名字
通過itemtype
,我們可以給微數(shù)據(jù)項(xiàng)指定一種類型,這個屬性需要使用在含itemscope
的元素上。itemtype
的值是個URL地址,代表了微數(shù)據(jù)使用的詞匯。請注意,這個地址只能是文本字符串,用來唯一標(biāo)示詞匯表,同時,該地址不一定非要指向真是的網(wǎng)頁地址(當(dāng)然,指向最好了)。這樣我們就可以使用詞匯表中的name名稱作為itemprop
名稱做些定義了。
例如,下面這個例子。
<p itemscope itemtype="http://schema.org/MusicGroup">后天我要去看<span itemprop="name">S˙H˙E</span>的演唱會,好興奮哈!</p>
http://schema.org/MusicGroup
詞匯表中有個名為"name"的關(guān)鍵名稱,如下截圖: 用itemid全局標(biāo)識 有時,某些項(xiàng)需要唯一的標(biāo)識進(jìn)行標(biāo)識。例如書的ISBN數(shù)值,我們可以借助itemid屬性,如下例子:
<p itemscope itemtype="http://vocab.example.com/book" itemid="urn:isbn:0321687299"> <!-- 書信息… --> </p>
三、微數(shù)據(jù)的力量
說了這么多,你可能會有疑問或不耐煩了:微數(shù)據(jù)這玩意,貌似蠻復(fù)雜,頭疼的,看上去鳥用都沒有,唉,還是按ctrl+w去微博轉(zhuǎn)轉(zhuǎn)吧~~
為了打消這種疑慮,有必要中間插播介紹了微數(shù)據(jù)的真正體。我們可能都知道,給元素添加額外的語義東西,可以使用自定義data
屬性(data-*
). 但是,自定義data
屬性只是純粹的屬性,而微數(shù)據(jù)有特定的規(guī)范,有特定屬性名稱的詞匯表,且更多是服務(wù)于現(xiàn)實(shí)世界,在這方面是很強(qiáng)大的。
說得太空太虛了吧?確實(shí),過于學(xué)術(shù)化的說法往往不易讓人理解。通俗講,微數(shù)據(jù)就是在保證頁面內(nèi)容顯示良好的情況下清晰而準(zhǔn)確地勾勒出了數(shù)據(jù)的骨架與精髓,可以通過工具、API等進(jìn)行方便強(qiáng)大的數(shù)據(jù)交互。
我們都知道,現(xiàn)在流行的數(shù)據(jù)交換格式是什么?沒錯,就是JSON。從某種意義上來講,微數(shù)據(jù)的本質(zhì)就是JSON,哦?沒看出來,不急,舉個例子就會知道了。
如下HTML下的微數(shù)據(jù)代碼:
<section> <h3><a title="WDE-ex Vol11『iPad のウェブデザイン:私たちがみつけたこと 』 : ATND">WDE-ex Vol.11 — Designing for iPad: Our experience so far</a></h3> <p>On <time datetime="2010-07-21T19:00:00+09:00">July 21st 19:00 </time>-<time datetime="2010-07-21T20:00:00+09:00">20:00</time> at <span itemscope itemtype="http://schema.org/Organization"> <a itemprop="url" > <span itemprop="name">Apple Ginza</span></a></span>, <span itemscope itemtype="http://schema.org/Person"> <a itemprop="url" title="iA"> <span itemprop="name">Oliver Reichenstein</span>, CEO of iA</a> </span>, will share the lessons they've learned while creating three iPad apps and one iPad website.</p> </section>
日本,英文不懂沒關(guān)系,機(jī)器也不懂,但是它懂微數(shù)據(jù),你也一樣。上面微數(shù)據(jù)的數(shù)據(jù)本質(zhì)是什么?很亂,不是嗎?但是,如果我們只關(guān)注微數(shù)據(jù)的部分:itemscope
, itemprop
等,你會發(fā)現(xiàn)什么?
我們使用Live Microdata對上面的HTML代碼跑一下,會得到下面的JSON數(shù)據(jù):
{ "items": [ { "type": [ "http://schema.org/Organization" ], "properties": { "url": [ "http://www.apple.com/jp/retail/ginza/map/" ], "name": [ "Apple Ginza" ] } }, { "type": [ "http://schema.org/Person" ], "properties": { "url": [ "http://informationarchitects.jp/" ], "name": [ "Oliver Reichenstein" ] } } ] }
我想,您應(yīng)該大致明白機(jī)器為何可以識別微數(shù)據(jù)了。由于微數(shù)據(jù)的這種數(shù)據(jù)本質(zhì)特性,在web應(yīng)用中,我們做一些數(shù)據(jù)交互的時候,事情就會變得奇妙而輕松。例如,你訪問一個你中意的女孩子的個人主頁的時候,如果使用微數(shù)據(jù),就可以自動把她的一些信息啊聯(lián)系方式啊什么的放到你的通訊錄中,是不是很贊!或是在你的日歷表中添加一些需要完成的事件等等。很多真實(shí)世界的應(yīng)用與web應(yīng)用有了更為輕松強(qiáng)大的結(jié)合。
四、一些詞匯表介紹
這里提及的微數(shù)據(jù)詞匯表有三:schema.org詞匯表,Google豐富摘要詞匯表(www.data-vocabulary.org),WHATWG/microformats.org詞匯表。
我大致看了這幾個詞匯表,不由得心里一咯噔:媽呀,這么多,都介紹的話我這周末只能抱電腦了。因此,這里只大致提點(diǎn)部分提綱性質(zhì)內(nèi)容。
事件(Events)
- schema.org/Event vocabulary —
http://schema.org/Event
- http://www.google.com/support/webmasters/bin/answer.py?answer=164506 —
http://www.data-vocabulary.org/Event/
- vEvent —
http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#vevent
人物(Person)
- schema.org Person —
http://schema.org/Person
- vCard —
http://microformats.org/profile/hcard
- Rich Snippets Person —
http://data-vocabulary.org/Person
組織或業(yè)務(wù)(Organisation or business)
- schema.org Organization —
http://schema.org/Organization
- vCard (using
fn org
) —http://microformats.org/profile/hcard
- Rich Snippets Organization —
http://data-vocabulary.org/Organization
日歷(Calendar)
- schema.org Event —
http://schema.org/Event
- vEvent —
http://microformats.org/profile/hcalendar#vevent
- Rich Snippets Event —
http://data-vocabulary.org/Event
預(yù)覽(Review)
- schema.org Review —
http://schema.org/Review
- schema.org AggregateRating —
http://www.schema.org/AggregateRating
(跟豐富摘要itemtype
不同)- hReview —
http://microformats.org/wiki/hreview
- Rich Snippets Review —
http://data-vocabulary.org/Review
- Rich Snippets Review-aggregate —
http://www.data-vocabulary.org/Review-aggregate
許可證(License)
- Licensing works —
http://n.whatwg.org/work
產(chǎn)品和服務(wù)(Products and services)
- schema.org Product —
http://schema.org/Product
- This can be extended with productontology.org descriptions (example)
- hProduct —
http://microformats.org/wiki/hproduct
- GoodRelations Product —
http://purl.org/goodrelations/
(例如<a itemprop="http://purl.org/goodrelations/v1#availableDeliveryMethods" href="hhttp://purl.org/goodrelations/v1#UPS">via UPS</a>
)- Rich Snippets Product —
http://data-vocabulary.org/Product
Atom提要(Atom feed)
- hAtom —
http://microformats.org/wiki/hatom
食譜(Recipes)
- schema.org Recipe —
http://schema.org/Recipe
- hRecipe —
http://microformats.org/wiki/hrecipe
- Rich Snippets Recipe —
http://data-vocabulary.org/Recipe
Google豐富摘要詞匯支持微格式和RDFa這是除了微數(shù)據(jù)之外其他兩個增加內(nèi)容語義的方法。除了這種差異外,基本上與schema.org是想匹配的,除非他們在itemtype
中使用www.data-vocabulary.org
代替schema.org
。盡管google仍然支持這類詞匯,但是最新的schema.org提供了更多的詞匯,這些詞匯還被Bing和Yahoo支持,因此在選擇微數(shù)據(jù)上schema.org可以讓你笑得更久。不過您可能仍然希望簽出“富文檔摘要”,因?yàn)樗鼈兇a更簡單,書寫的時候要比schema.org來得更好。
五、瀏覽器支持
微數(shù)據(jù)瀏覽器支持情況(截止2011年8月16日)BrowserSupportChromeSafari
Firefox進(jìn)行中...Opera最快版本12.00-1033Internet Explorer
雖然瀏覽器目前對微數(shù)據(jù)的支持基本上就是大鴨蛋,但是,由于搜索引擎還有一些三方工具都鳥他,微數(shù)據(jù)目前還是很有應(yīng)用價值的。例如Bing, Google, 以及Yahoo使用schema.org詞匯表定義的微數(shù)據(jù)顯示搜索結(jié)果(特定格式的搜索結(jié)果,即將展示)。
六、豐富網(wǎng)頁摘要應(yīng)用
先看國內(nèi)應(yīng)用微數(shù)據(jù)的實(shí)例。
我們打開谷哥哥,輸入“香口魚”,輕輕地回車,稍等片刻,得到下面截圖:
我們對比可以發(fā)現(xiàn),大眾點(diǎn)評網(wǎng)搜索顯示的樣子跟下面的蕓蕓結(jié)果不一樣,有評分顯示,評論數(shù),日期等。怎么回事?莫非點(diǎn)評給Google塞了很多票子。非也,我們點(diǎn)擊鏈接進(jìn)入該頁面,右鍵查看源代碼,就會發(fā)現(xiàn)如下代碼:
上面截圖各種標(biāo)注的部分就是本文多次出現(xiàn)了微數(shù)據(jù)特有的關(guān)鍵屬性,名稱等。也就是說,點(diǎn)評的搜索結(jié)果的顯示之所以不同,是因?yàn)槠鋺?yīng)用了HTML5之微數(shù)據(jù),使用的詞匯表是相對簡單的Google豐富網(wǎng)頁摘要詞匯表中的。
七、總結(jié)
到此這篇關(guān)于HTML5擴(kuò)展itemscope, itemtype, itemprop微數(shù)據(jù)的解釋和應(yīng)用的文章就介紹到這了,更多相關(guān)HTML5微數(shù)據(jù)itemscope, itemtype, itemprop內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
CSS注釋、命名、繼承性、樣式排序等CSS技巧的小結(jié)
CSS注釋、命名、繼承性、樣式排序等CSS技巧的小結(jié)...2007-09-09div的dispaly:inline樣式 和span的區(qū)別
為什么在ie里執(zhí)行后這段代碼 兩個span標(biāo)簽雖在同一行但它們之間有空隙? 但是 兩個div標(biāo)簽雖然在同一行就沒有空隙?2008-07-07Internet Explorer 8 beta 中文版與IE7共存的解決方法
今天安裝了IE8,去微軟網(wǎng)站下載的時候發(fā)現(xiàn)已經(jīng)提供中文版的beta了,哈哈 發(fā)現(xiàn)IE8沒有傳說中的那么妖魔化,不錯的瀏覽器,新增功能不錯。2008-05-05用css alpha 濾鏡 實(shí)現(xiàn)input file 樣式美化代碼
用css alpha 濾鏡 實(shí)現(xiàn)input file 樣式美化代碼...2007-12-12我的一些關(guān)于web標(biāo)準(zhǔn)的思考筆記(一)
我的一些關(guān)于web標(biāo)準(zhǔn)的思考筆記(一)...2006-12-12