News Ticker

Menu

XML_Schemas_Attribute

<XML_Schemas>Bài 4:  Thuộc tính.


1.Định nghĩa một thuộc tính đơn giản.


<xsd:attribute name="tên của thuộc tính" type="Kiểu dữ liệu"/>

Các kiểu dữ liệu phổ biến :

  • xs:string
  • xs:decimal
  • xs:integer
  • xs:boolean
  • xs:date
  • xs:time
VD: Một phần tử XML Student có thuộc tính MSV

<student MSV="M581626">
<name>Tu</name>
</student>

Định nghĩa của thuộc tính:

<xsd:attribute name="MSV" type="xs:string"/>

2. Định nghĩa một thuộc tính phức tạp.

<attribute
default=string
fixed=string
form=qualified|unqualified
id=ID
name=NCName
ref=QName
type=QName
use=optional|prohibited|required
any attributes
>

(annotation?,(simpleType?))

</attribute>

- name : tên của thuộc tính.
- type : kiểu dữ liệu.
- default : giá trị mặc định.
- fixed : giá trị cố định.
- use Sử dụng :  optional - Tùy chọn có thể có hoặc không (Mặc định)
                 prohibited -  Các thuộc tính không được thực hiện.
                 required - Yêu cầu bắt buộc phải có nội dung
(annotation?,(simpleType?)) để thiết lập thuộc tính một cách chặt chẽ hơn.

VD:

<xsd:attribute name="MSV" type="xsd:string" default="M581626"/>

<xsd:attribute name="MSV" type="xsd:string" fixed="M581626"/>

Chạy thử để hiểu sự khác biệt của default và fixed

<xsd:attribute name="MSV" type="xsd:string" use="required"/>

File students.xml:

<?xml version="1.0" encoding="UTF-8"?>
<students  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://exam.com/students students.xsd"
xmlns = "http://exam.com/students">

<student MSV="M581626">

<name>Tu</name>
</student>


</students>

File student.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns="http://exam.com/students"
targetNamespace="http://exam.com/students"
elementFormDefault="qualified" 
attributeFormDefault="unqualified">

<xsd:element name="students">

<xsd:complexType>
<xsd:sequence>
<xsd:element name="student">

<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="MSV" type="xsd:string"/>
</xsd:complexType>

</xsd:element>
</xsd:sequence>
</xsd:complexType>

</xsd:element>

</xsd:schema>



Chúc các bạn thành công.






Share This:

Post Tags:

2 comments to ''XML_Schemas_Attribute"

ADD COMMENT

  • To add an Emoticons Show Icons
  • To add code Use [pre]code here[/pre]
  • To add an Image Use [img]IMAGE-URL-HERE[/img]
  • To add Youtube video just paste a video link like http://www.youtube.com/watch?v=0x_gnfpL3RM