在 XML 元素名称中使用 - [英] Using - in XML element name

查看:23
本文介绍了在 XML 元素名称中使用 -的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 XML 元素名称中使用 hyphen -?

Is it OK to use hyphen - in an XML element name?

例如

<rank-type>Rank</rank-type>

推荐答案

正如 Vinko 和hacker 所说,答案是

As readily stated by Vinko and hacker, the answer is

是的,名称中允许使用破折号,无论是元素名称还是属性名称.但是破折号不能作为名字的第一个字符.

Yes, dashes are allowed in names, whether for element names or attribute names. However a dash cannot be the first character in the name.

W3C 标准定义名称如下(2.3节)

The W3C standard defines names as follows (section 2.3)

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]
NameChar  ::=   NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
Name  ::= NameStartChar (NameChar)*

忽略各种扩展的 ASCII 字符和双字节字符(在语法定义中以 # 符号开头),名称需要以大写或小写字母 (AZ)、冒号 (:) 或下划线开头.此起始字符后面的字符(如果有的话,即如果名称不是单个字符)可以是任何有效的起始字符和/或任何数字 (0-9)、著名的破折号 (-) 和点(.)

Ignoring the various extended ASCII charactes and double-byte characters (which starting with # sign in the syntax definition), a name needs to start with an upper or lower case letter (A-Z), a colon (:) or an underscore. The characters following this start character, if any, i.e. if the name is not a single character, can be any of the valid start characters and/or any of the digits (0-9), the famous dash (-) and the dot (.)

正如其他回复中所指出的,在可能的情况下,建议避免某些字符,即使它们可能是合法的,以免我们冒着遇到各种解析器和互操作层的一些实现特定困难的风险.

As pointed out in the other responses, where possible, it is advisable to avoid certain characters, even though they may be legal, lest we risk encountering some implementation specific difficulties with various parsers and interop layers.

W3C 本身已经暗示使用破折号是正确的,因为它们在例如 XSL 中被广泛使用.

The W3C itself has implicitly vindicated the use of dashes as these are extensively used in XSL for example.

这篇关于在 XML 元素名称中使用 -的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆