代码之家  ›  专栏  ›  技术社区  ›  sprawled octopus

html5-微数据-向作者添加书籍(反之亦然)

  •  1
  • sprawled octopus  · 技术社区  · 11 年前

    我正在用Schema.org微数据标记一个网站,结果遇到了一个问题。我知道我可以定义一本书,然后它就是作者(我会用../而不是 http://schema.org/ 用于空间):

    <div itemscope itemtype="../Book">
    <span itemprop="name">Charlie and the Chocolate Factory</span>
    written by <span itemprop="author">Roald Dahl</span>
    </div>
    

    但我正在标记一个关于作者的页面,所以我想用一种方法列出他的书,并说所有这些文章都是他写的,而不需要一遍又一遍地列出他的名字:

    <article itemscope itemtype="../Person" id="main">
    <h1 itemprop="name">Roald Dahl</h1>
    <p>His books included <span itemscope itemtype="../Book">Charlie and the
    Chocolate Factory</span> and 
    <span itemscope itemtype="../Book">James and the Giant Peach</span></p>
    </article>
    

    有什么想法吗?只是把 itemref=“主要” 没有帮助(因为我试图给出一个特定的关系,作者的关系,而不仅仅是笼统地将这两件事联系起来)。空跨度元素有意义吗?:

    <span itemscope itemtype="../Book">James and the Giant Peach
    <span itemprop="author" itemref="main"></span></span>
    

    更新 :我想我已经想清楚了,对于任何抬头看的人来说。您可以为itemscope定义一个itemprop,以便稍后与itemref一起使用。所以我认为答案是:

    <article itemscope itemtype="../Person" itemprop="author" id="main">
    <h1 itemprop="name">Roald Dahl</h1>
    <p>His books included <span itemscope itemtype="../Book" itemref="main">Charlie
    and the Chocolate Factory</span> and 
    <span itemscope itemtype="../Book" itemref="main">James and the Giant Peach</span>
    </article>
    

    不确定如果有不止一个itemprop我想链接回来,我会怎么做(Schema.org完全是双向关系,只定义了其中一种方式),但这解决了我的特殊问题。

    更新2 :这已经解决了我以前的问题,但我现在无法将其嵌入任何其他项目范围。所以,我想把整件事标记为 文章 ,我不能这样做:

    <main itemscope itemtype="../Article">
    <article itemscope itemtype="../Person" itemprop="author" id="main">
    <h1 itemprop="name">Roald Dahl</h1>
    ....
    </article></main>
    

    现在我的itemprop=“author”破解也被分配到了顶级 文章 。现在我的代码告诉人们,罗尔德·达尔不仅写了所给的书,还写了这篇文章!

    有什么建议可以解决这个问题,或者解决我最初的问题,但不会导致这个问题?

    1 回复  |  直到 11 年前
        1
  •  0
  •   ajax    11 年前

    考虑到你的问题,我得出了两个(都有点尴尬)解决方案。分享我的想法。

    所以回答你最初的问题。你可以说你_ 著者 _ 写入 _ 并使用新的 http://schema.org/WriteAction 班其描述

    创作具有创造性的书面内容的行为。

    适合您的用例。事实上,这一页的例子已经涵盖了这一点。

    然后我们可以这样做:

    <main itemscope itemtype="http://schema.org/Article">
        <article  itemscope itemtype="http://schema.org/WriteAction">
            <div itemprop="agent" itemscope itemtype="http://schema.org/Person">
                <h1 itemprop="name">Roald Dahl</h1>
            </div>
            <p>His books included <span itemprop="result" itemscope itemtype="http://schema.org/Book"><span itemprop="name">Charlie and the Chocolate Factory</span></span> and 
            <span itemprop="result" itemscope itemtype="http://schema.org/Book"><span itemprop="name">James and the Giant Peach</span></span></p>
        </article>
    </main>
    

    下一个想法是关于你最新的问题。除了在页面上某个不可见的标签中重复部分数据外,我看不到其他选项 文章 。但也许我错过了smth。 例如。,

    <main itemscope itemtype="http://schema.org/Article">
        <article>
            <h1>Roald Dahl</h1>
            <p>His books included <span itemscope itemtype="http://schema.org/Book" itemref="main">
                <span itemprop="name">Charlie and the Chocolate Factory</span></span>
            and 
            <span itemscope itemtype="http://schema.org/Book" itemref="main">
                <span itemprop="name">James and the Giant Peach</span></span></p>
        </article>
    </main>
    <!-- in the footer or any other place -->
    <div itemscope itemtype="http://schema.org/Person" itemprop="author" id="main">
        <meta itemprop="name" content="Roald Dahl"/>
    </div>
    

    两种解决方案都不太好,但都有效。