XSD中的多属性打包

Posted on

XSD中的多属性打包

定义XSD的时候,除了元素外还有属性;在使用XML时属性也占有相当大的比例,由此可见属性十分重要。例如,定义一本图书的XML元素有图书名称、出版社、作者、价格等。以作者为例,可以将其联系方式作为一种属性,如qq、msn、tel,如图1.18所示。 说明: http://images.51cto.com/files/uploadimg/20110728/103912698.jpg(http://images.51cto.com/files/uploadimg/20110728/103912698.jpg) 图1.18 XSD中的多属性打包

使用xs:attributegGroup定义一个属性组,设置name="att"(为组命名),然后在xs:attributeGroup内部定义属性组的成员,成员数量没有限制。代码如下:

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

  3. targetNamespace=http://www.mingrisoft.com xmlns="http://www.mingrisoft.com"

  4. elementFormDefault="qualified">

  5. <xs:element name="book">

  6. <xs:element name="name" type="xs:string" />

  7. <xs:element name="publisher" type="xs:string" />

  8. <xs:element name="company" type="xs:string" />

  9. <xs:element name="author">

  10. <xs:extension base="xs:string">

  11. <xs:attributeGroup ref="att">

  12. <xs:element name="ISBN" type="xs:string" />

  13. <xs:element name="price" type="xs:double" />

  14. <xs:element name="url" type="xs:string" />

  15. <xs:attributeGroup name="att">

  16. <xs:attribute name="tel" type="xs:string" use="required">

  17. <xs:attribute name="qq" type=" xs:integer">

  18. <xs:attribute name="msn" type=" xs:string">

(1)建立XSD文档,声明文档根元素和命名空间。声明一个根节点book,添加复杂类型xs:cimplexType以及book下的子元素(name、publisher、company、ISBN、price和url),为各个子元素添加xs:string类型限定。

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

  3. targetNamespace=http://www.mingrisoft.com xmlns="http://www.mingrisoft.com"

  4. elementFormDefault="qualified">

  5. <xs:element name="book">

  6. <xs:element name="name" type="xs:string" />

  7. <xs:element name="publisher" type="xs:string" />

  8. <xs:element name="company" type="xs:string" />

  9. <xs:element name="ISBN" type="xs:string" />

  10. <xs:element name="price" type="xs:double" />

  11. <xs:element name="url" type="xs:string" />

(2)在company下面添加一个元素author,然后在xs:simpleType上扩展xs:simpleType和xs:extension元素,在xs:extension上为author内容声明属性xs:string。

  1. <xs:element name="author">

(3)在xs:schema内部和book根节点平级的地方,声明一个xs:attributeGroup,设置其name="att";在xs:attributeGroup内部声明3个属性,分别为tel、qq和msn,形成一个具有3个属性的属性组。

  1. <xs:attributeGroup name="att">

  2. <xs:attribute name="tel" type="xs:string" use="required">

  3. <xs:attribute name="qq" type="xs:integer">

  4. <xs:attribute name="msn" type="xs:string">

(4)在声明author 的地方,扩展xs:extension元素,在其内部声明一个xs:attributeGroup引用,引用刚才声明的属性组。

  1. <xs:extension base="xs:string">

  2. <xs:attributeGroup ref="att">

心法领悟018:author属性组中的属性。

在本实例中为author添加了一个属性组,内部有3个属性tel、qq、msn,这样是为了更好地说明xs:attributeGroup的使用方式,但一般不建议这么定义XML,最好的方式是把tel、qq和msn定义成author的子元素。

希望本站内容对您有点用处,有什么疑问或建议请在后面留言评论
转载请注明作者(RobinChia)和出处 It so life ,请勿用于任何商业用途