【问题标题】:Comments in DTD definitionsDTD 定义中的注释
【发布时间】:2018-05-30 14:19:53
【问题描述】:

在查看 HTML 4 dtd (https://www.w3.org/TR/html4/sgml/dtd.html) 时,我在 dtd 定义中看到了很多描述性 cmets。

<!ELEMENT A - - (%inline;)* -(A)       -- anchor -->
<!ATTLIST A
  %attrs;                              -- %coreattrs, %i18n, %events --
  charset     %Charset;      #IMPLIED  -- char encoding of linked     resource --
  type        %ContentType;  #IMPLIED  -- advisory content type --
  name        CDATA          #IMPLIED  -- named link end --
  href        %URI;          #IMPLIED  -- URI for linked resource --
  hreflang    %LanguageCode; #IMPLIED  -- language code --
  rel         %LinkTypes;    #IMPLIED  -- forward link types --
  rev         %LinkTypes;    #IMPLIED  -- reverse link types --
  accesskey   %Character;    #IMPLIED  -- accessibility key character     --
  shape       %Shape;        rect      -- for use with client-side     image maps --
  coords      %Coords;       #IMPLIED  -- for use with client-side image maps --
  tabindex    NUMBER         #IMPLIED  -- position in tabbing order --
  onfocus     %Script;       #IMPLIED  -- the element got the focus --
  onblur      %Script;       #IMPLIED  -- the element lost the focus --

>

我知道这是一个 SGML dtd。当我尝试在我的 XML dtds 中引入它们时,我收到来自处理器的解析错误。这些cmets 是否已从XML 的DTD 变体中删除?

post.dtd:36: parser error : expected '>'
<!ELEMENT A (#PCDATA) -- a link -- >
                  ^
post.dtd:36: parser error : Content error in the external subset
<!ELEMENT A (#PCDATA) -- a link -- >

【问题讨论】:

  • IIRC,XML cmets 只允许在它们自己的声明中使用(并且只有一个注释,没有前导或尾随空格 - 所以它们总是以 &lt;!-- 开头并以 --&gt; 结尾)。我很确定 XML 小组决定不允许在其他声明中使用 cmets(但现在已经是 20 多年前了,我的记忆越来越模糊了)。

标签: xml dtd sgml


【解决方案1】:

在 SGML 中,以-- 开头和结尾的 cmets 可以出现在任何地方,或者在标记声明中出现多次;在 XML 中,标记声明必须要么只包含一个注释,要么包含另一个标记声明:

<!-- valid in XML -->
<!-- only -- -- valid -- -- in -- -- SGML -->

由于 XML 被定义为 SGML 子集,因此文本字符串 -- 不允许出现在 XML cmets 的任何位置。

【讨论】:

    【解决方案2】:

    根据 X3 XML 1.0 规范,在 DTD 声明中允许使用 cmets。但是,它们是 XML 样式的 cmets,以 &lt;!-- 开头并以 --&gt; 结尾,正如前面的评论员所指出的那样。有关语法详细信息,请参阅https://www.w3.org/TR/2008/REC-xml-20081126/#NT-DeclSep

    但是,它们没有像上面的示例那样嵌入到元素或属性列表声明中。 XHTML DTD 中的示例描述了使用带有 cmets 的 XML DTD 语法的元 HTML 标记。完整的 dtd 在:https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd

    <!ELEMENT meta EMPTY>
    <!ATTLIST meta
      %i18n;
      id          ID             #IMPLIED
      http-equiv  CDATA          #IMPLIED
      name        CDATA          #IMPLIED
      content     CDATA          #REQUIRED
      scheme      CDATA          #IMPLIED
      >
    
    <!--
      Relationship values can be used in principle:
    
       a) for document specific toolbars/menus when used
          with the link element in document head e.g.
            start, contents, previous, next, index, end, help
       b) to link to a separate style sheet (rel="stylesheet")
       c) to make a link to a script (rel="script")
       d) by stylesheets to control how collections of
          html nodes are rendered into printed documents
       e) to make a link to a printable version of this document
          e.g. a PostScript or PDF version (rel="alternate" media="print")
    -->
    

    【讨论】:

      猜你喜欢
      • 2016-01-18
      • 2018-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多