扩展 XHTML DTD 以在 ID 属性中使用特殊字符

发布于 2024-12-06 10:58:01 字数 134 浏览 1 评论 0原文

我想验证作为 XHTML 扩展的 XML 模板。现在,ID 属性中出现了特殊字符,例如 {|。是否可以扩展 XHTML DTD 以覆盖对 ID 属性中允许的字符的限制?或者是XML规范定义的字符?

I want to validate XML templates that are a XHTML extension. Now there are special characters like { and | in ID attributes. Is it possible to extend the XHTML DTD to overwrite the restriction to the characters allowed in the ID attribute? Or are the characters defined by the XML specification?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2024-12-13 10:58:01

不能使用字符“{”和“|”直接在 id 属性中,因为在 XML 规范 中它说

类型 ID 的值必须与名称生成相匹配。名称不得作为该类型的值在 XML 文档中出现多次;即,ID 值必须唯一标识承载它们的元素。

名称生成位于此处。如果展开语法规则,您会发现名称中允许的唯一字符由这些产生式给出:

[4] NameStartChar ::= ":" | [AZ] | “_” | [阿兹] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]

[4a] NameChar ::= NameStartChar | “-”| “。” | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]

不幸的是,不允许使用左括号和管道。这些字符的代码点分别是#7B 和#7C;不在可接受的字符范围内。

TL;DR:ID 属性的合法字符由 XML 规范拥有,并且您的两个字符不合法。

附录

以下是一些示例。以下文档通过了 W3C 验证站点上的 XHTML 验证:

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
  <head>
    <title>A title</title>
  </head>
  <body id="anid">
  </body>
</html>

但以下文档不会通过

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
  <head>
    <title>A title</title>
  </head>
  <body id="ani{d">
  </body>
</html>

我们收到错误:

Line 8, Column 16: character "{" is not allowed in the value of attribute "id"

现在非常有趣的是,如果您确实想要在 id 名称中使用左大括号,您可以尝试 这个:

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
  <head>
    <title>A title</title>
  </head>
  <body id="ani{d">
  </body>
</html>

但是你得到了同样的错误!你可能想尝试一下这个;验证器显示带有与号散列 x 7 b 分号的行,但它认为那里有一个左大括号。

最重要的是,您不能使用 XML 规范允许的字符以外的字符。

You cannot use the characters '{' and '|' directly in id attributes because in the XML specification it says

Values of type ID must match the Name production. A name must not appear more than once in an XML document as a value of this type; i.e., ID values must uniquely identify the elements which bear them.

The name production is here. If you expand the syntax rule you see that the only characters allowed in a name are given by these productions:

[4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]

[4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]

Unfortunately the left brace and the pipe are not allowed. The codepoints for those characters are #7B and #7C respectively; not in the accepted character ranges.

TL;DR: the legal characters for ID attributes are owned by the XML spec and your two characters are not legal.

ADDENDUM

Here are some examples. The following document passes validation for XHTML on the W3C validation site:

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
  <head>
    <title>A title</title>
  </head>
  <body id="anid">
  </body>
</html>

but the following will not

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
  <head>
    <title>A title</title>
  </head>
  <body id="ani{d">
  </body>
</html>

We get the error:

Line 8, Column 16: character "{" is not allowed in the value of attribute "id"

Now it's rather interesting that if you really want the left curly bracket in the id name, you can try this:

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
  <head>
    <title>A title</title>
  </head>
  <body id="ani{d">
  </body>
</html>

But you get the same error! You might want to try this; the validator shows the line with the ampersand hash x seven b semicolon but it thinks there is a left brace there.

The bottom line is that you simply cannot have ids with characters other than those allowed by the XML specification.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文