以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DTD/XML Schema 』  (http://bbs.xml.org.cn/list.asp?boardid=23)
----  W3C XML Schema 内建字符串类型比较(含schema及xml文件)  (http://bbs.xml.org.cn/dispbbs.asp?boardid=23&rootid=&id=25812)


--  作者:98900969r
--  发布时间:12/22/2005 10:22:00 PM

--  W3C XML Schema 内建字符串类型比较(含schema及xml文件)
string,字符串。
  可以包含回车符,换行符,制表符.
QName (qualified name),有前缀名称类型。
  前缀和局部名称必须都是NCName类型,中间用冒号连接.
normalizedString,规范化字符串类型。
  string中回车符、换行符、制表符将被去掉 。
token,记号字符串类型。
  normalizedString中头、尾及中间多余的空格字符将被去掉。
NMTOKEN,名称记号字符串类型。
  限制 token 类型中只能包含数字, 字母, 下划线, 冒号, 及其他名字字符 (可以由数字开头 )
name,名称字符串类型。
  限制 token 类型中只包含数字, 字母, 下划线, 冒号, 及其他名字字符, 且非数字开头
NCName,无冒号名称字符串类型。
  限制name类型中不含冒号

这里提供一个schema和它的一个xml,来说明W3C XML Schema 各种内建字符串类型的区别。欢迎大家添加更有趣的例子。

charTypes.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- by 98900969r. to compare various XML Schema built-in string-based simple types. -->
<!-- 把被注释的不合法例子“放出来”,就会有验证错误 -->
<strings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="charTypes.xsd">
 <primitiveTypes>
 
  <!-- string,字符串。 可以包含回车符,换行符,制表符 -->
  <string>
        */??:123abc?          ???@#$%*()_+      +=-{}|/~!,.;:
                                    </string>
  
  <!-- QName (qualified name),有前缀名称类型。前缀和局部名称必须都是NCName类型,中间用冒号连接 -->
  <QName>q:a1234</QName>
  
  <!-- invalid QName, 不合法例子
  <QName>q:?abc</QName>
  <QName>q:*abc</QName>
  <QName>q: abc</QName>
  <QName>q:1abc</QName>
  -->
 </primitiveTypes>
 <derivedTypes>
 
  <!--  normalizedString,规范化字符串类型。string中回车符、换行符、制表符将被去掉 。-->
  <normalizedString>  */??:123abc?          ???@#$%*()_+      +=-{}|/~!,.;:</normalizedString>
  
  <!-- token,记号字符串类型。normalizedString中头、尾及中间多余的空格字符将被去掉。-->
  <token>*/??:123abc? ???@#$%*()_+ +=-{}|/~!,.;:</token>
  
  <!-- NMTOKEN,名称记号字符串类型。
  限制 token 类型中只能包含数字, 字母, 下划线, 冒号, 及其他名字字符 (可以由数字开头 ) -->
  <NMTOKEN>:1234</NMTOKEN>
  <NMTOKEN>_1234</NMTOKEN>
  
  <!-- invalid NMTOKEN,不合法例子
  <NMTOKEN>?asdfsad</NMTOKEN>
  <NMTOKEN>*1223a</NMTOKEN>
  <NMTOKEN>?1234</NMTOKEN>
  <NMTOKEN>a1234?</NMTOKEN>
  <NMTOKEN>/a1234</NMTOKEN>
  <NMTOKEN>)a1234</NMTOKEN>
  <NMTOKEN>$a1234</NMTOKEN>
  <NMTOKEN>_12 34</NMTOKEN>
  -->
  
  <!-- name,名称字符串类型。限制 token 类型中只包含数字, 字母, 下划线, 冒号, 及其他名字字符, 且非数字开头 -->
  <Name>:1234</Name>
  <Name>q:123a</Name>
  
  <!-- invalid Name 不合法例子
  <Name>1234</Name>
  -->
  
  <!-- NCName,无冒号名称字符串类型。限制name类型中不含冒号 -->
   
  <NCName>_1234</NCName>
  <NCName>abcd123</NCName>
  <NCName>书名1</NCName>
  
  <!-- invalid NCName  不合法例子
  <NCName>:123a</NCName>
  <NCName>q:123a</NCName>
  -->
 </derivedTypes>
</strings>

charTypes.xsd

<?xml version="1.0" encoding="UTF-8"?>
<!-- by 98900969r. to compare various XML Schema built-in string-based simple types. -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
 <xs:annotation>
  <xs:documentation>
  references:
  [1] Primitive XML Data Types
  http://doc.ddart.net/xmlsdk/htm/xsd_ref_6ldf.htm
  [2] Derived XML Data Types
  http://doc.ddart.net/xmlsdk/htm/xsd_ref_6z03.htm
  [3] w3schools, XML Schema Tutorial : String data types are used for values that contains character strings.
  http://www.w3schools.com/schema/schema_dtypes_string.asp
  </xs:documentation>
 </xs:annotation>
 <xs:element name="strings">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="primitiveTypes">
     <xs:complexType>
      <xs:sequence>
       <!-- string - Represents character strings. -->
       <!-- string,字符串。 可以包含回车符,换行符,制表符 -->
       <xs:element name="string" type="xs:string" maxOccurs="unbounded"/>
       <!-- QName - Represents a qualified name.
       A qualified name is composed of a prefix and a local name separated by a colon.
       Both the prefix and local names must be an NCName.
       The prefix must be associated with a namespace URI reference, using a namespace declaration. -->
       <!-- QName (qualified name),有前缀名称类型。前缀和局部名称必须都是NCName类型,中间用冒号连接 -->
       <xs:element name="QName" type="xs:QName" maxOccurs="unbounded"/>
      </xs:sequence>
     </xs:complexType>
    </xs:element>
    <xs:element name="derivedTypes">
     <xs:complexType>
      <xs:sequence>
       <!-- normalizedString - Represents white space normalized strings.
       This data type is derived from string. A string that does not contain line feeds, carriage returns, or tabs -->
       <!--  normalizedString,规范化字符串类型。
       string中回车符、换行符、制表符将被去掉 被当成空字符相邻头、尾及中间多余的"空字符"去掉。-->
       <xs:element name="normalizedString" type="xs:normalizedString" maxOccurs="unbounded"/>
       <!--  token - Represents tokenized strings.
       This data type is derived from normalizedString.
       A string that does not contain line feeds, carriage returns, tabs, leading or trailing spaces, or multiple spaces -->
       <!-- token,记号字符串类型。normalizedString中头、尾及中间多余的空格字符将被去掉。-->
       <xs:element name="token" type="xs:token" maxOccurs="unbounded"/>
       <!-- NMTOKEN - Represents the NMTOKEN attribute type.
       An NMTOKEN is set of name characters (letters, digits, and other characters) in any combination.
       Unlike Name and NCName, NMTOKEN has no restrictions on the starting character.
       This data type is derived from token. (only used with schema attributes) -->
       <!-- NMTOKEN,名称记号字符串类型。
       限制 token 类型中只能包含数字, 字母, 下划线, 冒号, 及其他名字字符 (可以由数字开头 ) -->
       <xs:element name="NMTOKEN" type="xs:NMTOKEN" maxOccurs="unbounded"/>
       <!-- Represents names in XML.
       A Name is a token that begins with a letter, underscore, or colon and continues with name characters
       (i.e. letters, digits, and other characters).
       This data type is derived from token. -->
       <!-- name,名称字符串类型。限制 token 类型中只包含数字, 字母, 下划线, 冒号, 及其他名字字符, 且非数字开头 -->
       <xs:element name="Name" type="xs:Name" maxOccurs="unbounded"/>
       <!-- Represents noncolonized names. This data type is the same as Name, except it cannot begin with a colon.
       This data type is derived from Name. -->
       <!-- NCName,无冒号名称字符串类型。限制name类型中不含冒号 -->
       <xs:element name="NCName" type="xs:NCName" maxOccurs="unbounded"/>
      </xs:sequence>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>


[此贴子已经被作者于2005-12-23 0:53:57编辑过]

--  作者:kelindun
--  发布时间:6/5/2006 11:15:00 AM

--  
楼主,强人也!想请教楼主是怎么学XML的??

--  作者:lao_cai
--  发布时间:4/13/2007 11:52:00 AM

--  
NMTOKEN应该还可以包含其他字符,例如. -等
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
62.500ms