xml 什么是 xs:NCName 类型以及何时应该使用它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1631396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:49:15  来源:igfitidea点击:

What is an xs:NCName type and when should it be used?

xmlxsdxml-namespaces

提问by jasso

I ran one of my xml files through a schema generator and everything generated was what was expected, with the exception of one node:

我通过模式生成器运行了我的一个 xml 文件,生成的所有内容都符合预期,但一个节点除外:

<xs:element name="office" type="xs:NCName"/>

What exactly is xs:NCName? And why would one use it, rather xs:string?

究竟是xs:NCName什么?为什么要使用它,而不是xs:string

采纳答案by Andrey Adamovich

NCNameis non-colonized name e.g. "name". Compared to QName which is qualified name e.g. "ns:name". If your names are not supposed to be qualified by different namespaces, then they are NCNames.

NCName是非殖民化名称,例如“name”。与限定名称的 QName 相比,例如“ns:name”。如果你的名字不应该被不同的命名空间限定,那么它们就是 NCNames。

xs:string puts no restrictions on your names at all, but xs:NCName basically disallows ":" to appear in the string.

xs:string 对您的姓名完全没有限制,但 xs:NCName 基本上不允许“:”出现在字符串中。

回答by jasso

@skyl practically provoked me to write this answer so please mind the redundancy.

@skyl 实际上激怒了我写这个答案,所以请注意冗余。

NCNamestands for "non-colonized name". NCName can be defined as an XML Schema regular expression [\i-[:]][\c-[:]]*

NCName代表“非殖民名称”。NCName 可以定义为 XML Schema 正则表达式[\i-[:]][\c-[:]]*

...and what does that regex mean?

...那个正则表达式是什么意思?

\iand \care multi-character escapes defined in XML Schema definition.
http://www.w3.org/TR/xmlschema-2/#dt-ccesN
\iis the escape for the set of initial XML name characters and \cis the set of XML name characters. [\i-[:]]means a set that consist of the set \iexcluding a set that consist of the colon character :. So in plain English it would mean "any initial character, but not :". The whole regular expression reads as "One initial XML name character, but not a colon, followed by zero or more XML name characters, but not a colon."

\i\c是在 XML 模式定义中定义的多字符转义。
http://www.w3.org/TR/xmlschema-2/#dt-ccesN
\i是初始 XML 名称字符集的转义符,\c是 XML 名称字符集。 [\i-[:]]表示由集合组成的集合,\i不包括由冒号字符组成的集合:。所以在简单的英语中,它的意思是“任何初始字符,但不是:”。整个正则表达式读作“一个初始的 XML 名称字符,但不是冒号,后跟零个或多个 XML 名称字符,但不是冒号”。

Practical restrictions of an NCName

NCName 的实际限制

The practical restrictions of NCName are that it cannot contain several symbol characters like :, @, $, %, &, /, +, ,, ;, whitespace characters or different parenthesis. Furthermore an NCName cannot begin with a number, dot or minus character although they can appear later in an NCName.

NCName 的实际限制是它不能包含多个符号字符,如:, @, $, %, &, /, +, ,, ;, 空白字符或不同的括号。此外,NCName 不能以数字、点或减号字符开头,尽管它们可以稍后出现在 NCName 中。

Where are NCNames needed

哪里需要 NCNames

In namespace conformant XML documents all names must be either qualified names or NCNames. The following values must be NCNames (not qualified names):

在符合命名空间的 XML 文档中,所有名称都必须是限定名称或 NCNames。以下值必须是 NCNames(非限定名称):

  • namespace prefixes
  • values representing an ID
  • values representing an IDREF
  • values representing a NOTATION
  • processing instruction targets
  • entity names
  • 命名空间前缀
  • 代表 ID 的值
  • 表示 IDREF 的值
  • 表示 NOTATION 的值
  • 处理指令目标
  • 实体名称

回答by izilotti

Practically speaking...

实际来说...

Allowed characters:-, ., 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, _, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z

允许的字符:-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghi, j, k, l, m, n, o, p,q, r, s, t, u, v, w, x, y,z

Also, -and .cannot be used as the first character of the value.

此外,-并且.不能用作值的第一个字符。

Disallowed characters:, !, ", #, $, %, &, ', (, ), *, +, ,, /, :, ;, <, =, >, ?, @, [, \, ], ^, `, {, |, }, ~

不允许的字符:!"#$%&'()*+,/:;<=>?@[\]^`{|}~

回答by Skylar Saveland

http://books.xmlschemata.org/relaxng/ch19-77215.html

http://books.xmlschemata.org/relaxng/ch19-77215.html

No spaces or colons. Allows "_" and "-".

没有空格或冒号。允许“_”和“-”。

You would use this instead of string so that you can validate that the value is limited to what is allowed. It maps well to certain conventions for name/identifier like django's concept of "slug", for instance.

您将使用它而不是字符串,以便您可以验证该值是否仅限于允许的值。例如,它很好地映射到名称/标识符的某些约定,例如 django 的“slug”概念。

I upvote the person who [\i-[:]][\c-[:]]*translates into English for us.

我赞成[\i-[:]][\c-[:]]*为我们翻译成英文的人。