News Ticker

Menu

Browsing "Older Posts"

XPath

Wednesday, May 18, 2016 / 1 Comment

<XML_XPath> Bài 8: Một số cú pháp cơ bản của XPath.

1.Select node

Các bạn có thể đọc tại http://www.w3schools.com/xsl/xpath_syntax.asp.

2.Các toán tử

Các bạn có thể đọc tại http://www.w3schools.com/xsl/xpath_operators.asp.

3. Ví dụ: <nguồn https://www.w3.org>

  • - para selects the para element children of the context node
  • - * selects all element children of the context node
  • - text() selects all text node children of the context node
  • - @name selects the name attribute of the context node
  • - @* selects all the attributes of the context node
  • - para[1] selects the first para child of the context node
  • - para[fn:last()] selects the last para child of the context node
  • - */para selects all para grandchildren of the context node
  • - /book/chapter[5]/section[2] selects the second section of the fifth chapter of the book whose parent is the document node that contains the context node
  • - chapter//para selects the para element descendants of the chapter element children of the context node
  • - //para selects all the para descendants of the root document node and thus selects all para elements in the same document as the context node
  • - //@version selects all the version attribute nodes that are in the same document as the context node
  • - //list/member selects all the member elements in the same document as the context node that have a list parent
  • - .//para selects the para element descendants of the context node
  • - .. selects the parent of the context node
  • - ../@lang selects the lang attribute of the parent of the context node
  • - para[@type="warning"] selects all para children of the context node that have a type attribute with value warning
  • - para[@type="warning"][5] selects the fifth para child of the context node that has a type attribute with value warning
  • - para[5][@type="warning"] selects the fifth para child of the context node if that child has a type attribute with value warning
  • - chapter[title="Introduction"] selects the chapter children of the context node that have one or more title children whose typed value is equal to the string Introduction
  • - chapter[title] selects the chapter children of the context node that have one or more title children
  • - employee[@secretary and @assistant] selects all the employee children of the context node that have both a secretary attribute and an assistant attribute
  • - book/(chapter|appendix)/section selects every section element that has a parent that is either a chapter or an appendix element, that in turn is a child of a book element that is a child of the context node.
  • - If E is any expression that returns a sequence of nodes, then the expression E/. returns the same nodes in document order, with duplicates eliminated based on node identity.

4. Các Hàm <Functions>

4.1 Boolean functions ( T - F )

• Boolean boolean(arg) //returns false if object equal to zero/NaN/empty 
• Boolean false() //returns the Boolean value false 
• Boolean lang(string arg) //to check language which is used in context node 
• Boolean not(arg) //
• Boolean true() // returns the Boolean value true

4.2 Node-set Functions

• number count(node-set arg) //returns the number of nodes 
• node-set id(string arg) //returns a node-set 
• last() // returns a value equal to the context size 
• string local-name(optional node-set arg) //returns local name(context node) 
• string name(optional node-set arg) //returns the nam of node in QName format 
• string namespace-uri(optional node-set arg) //returns namespace-uri of node-set 
• Number position() //returns value equal to the context position

4.3 Numeric Functions

• number ceiling( number arg) //returns the smallest integer greater than this 
• number floor(number arg) //returns the largest integer that is lower than this 
• number number(arg) //to convert string, Boolean, node-set to number //returns NaN if can not convert 
• number round(number arg) //returns the integer that is closest to the number arg 
• number sum(node-set arg) // return sum of node in node-set // return NaN if node can’t converted to number

4.4 String Functions

• string concat(string arg1, arg2) //returns the concatenation of those strings 
• Boolean contains(string arg1, arg2) //returns true if arg1 contains arg2 
• string normalize-space(string arg1) //=trim & replace whitespace=single space 
• Boolean start-with(string arg1, arg2) //returns true if arg1 starts with arg2 
• string string(arg) //to convert Boolean, node-set, number to string 
• number string-length(string arg) //returns the length of the string arg 
• string substring(string arg1, number arg2, optional number arg3) //return substring of arg1 
• string substring-before(string arg1, arg2) // 
• string substring-after(string arg1, arg2) // 
• string translate(string arg1, arg2, arg 3) //

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

Cách nhúng 1 tài liệu XSD vào tài liệu XML

/ No Comments

<XML_Schemas>Bài 7 : Nhúng 1 tài liệu XSD và Import trong XSD.
 

1. Nhúng 1 tài liệu XSD vào tài liệu XML

Như các bạn đã biết 2 file XML và XSD là độc lập với nhau
vậy làm thế nào có thể sử dụng File XSD để check nội dung của file XML.
 Ở bài này mình sẽ nói về cách nhúng 1 tài liệu XSD vào tài liệu XML.

Ở mỗi file XSD đều có phần:

<schema xmlns=”http://www.w3.org/2001/XMLSchema” xmlns:target=”http://www.exam.com/name” 
targetNamespace=”http://www.exam.com/name” 
elementFormDefault=”qualified”>

Những phần bôi đậm và gạch chân là những phần thay đổi. Để nhúng tài liệu XSD vào tài liệu XML ta cần căn cứ từ phần trên.
Trong tài liệu XML ta thêm đoạn mã sau vào gốc của tài liệu.


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://exam.com/name name.xsd"
xmlns = "http://exam.com/name"

xmlns = "http://exam.com/name" trùng với xmlns:target="http://www.exam.com/name" trong tài liệu XSD.

xsi:schemaLocation = "http://exam.com/name name.xsd"  tên cần trùng với file XSD, file name.xsd và file XML phải đặt cùng thư mục.

2. Import trong XSD

Cú pháp :

<import namespace="..." schemaLocation="...">
Câu lệnh import luôn được đặt sau <schema>

VD:
<xsd:import namespace="http://exam.com/name" schemaLocation="name.xsd"/>

3. Include trong XSD

Cú pháp: Dùng khi update nên mạng.

<include schemaLocation="...">

VD:
<include schemaLocation=”http://http://nguyenphutuvnua.blogspot.com/xml/name.xsd”/>


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














XML_Schemas_Tổng hợp

/ No Comments

<XML_Schemas>Bài 6: Bài tập tổng hợp trên lớp.


1. File contacts.xml.

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

<contacts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://exam.com/contact contact.xsd"
xmlns:n = "http://exam.com/name"
xmlns = "http://exam.com/contact"
>
<contact contact_ID = "M581626" know="M581626" gender="Male" blog="http:\\www.nguyenphutuvnua.blogspot.com\2016\05\xmlschemasattribute.html">
<name>

<n:firstname>Nguyen</n:firstname>
<n:midname></n:midname>
<n:lastname>Tu</n:lastname>

</name>

<location>

<address></address>

</location>

<phone  type="Home" >7666</phone>
<decription>
<br></br>
</decription>
</contact>
<contact contact_ID = "M583626" know="M581626 M583626" gender="Female" blog="http:\\www.nguyenphutuvnua.blogspot.com\2016\05\xmlschemasattribute.html" emails="nguyentudb1995@gmail.com">
<name>

<n:firstname>Nguyen</n:firstname>
<n:lastname>Tu</n:lastname>

</name>

<location>

<address></address>

</location>

<phone type="Cell">78887</phone>
<decription>
<br/>
</decription>

</contact>

</contacts>


2. File contact.xsd

2.1 Phần tử "contacts"

<xsd:element name="contacts">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="contact" type="complexType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>


2.2 Phần tử "contact"

<xsd:element name="contact" type="complexType" minOccurs="0" maxOccurs="unbounded"/>

<xsd:complexType name="complexType">
<xsd:sequence>
<xsd:element name="name" type="name"/>
<xsd:element name="location" type="location"/>
<xsd:element name="phone">
<xsd:complexType mixed="true">
<xsd:attribute name="type" type="phoneType" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="decription" type="discription"/>
</xsd:sequence>
<xsd:attribute name="contact_ID" type="xsd:ID" use="required"/>
<xsd:attribute name="know" type="xsd:IDREFS" use="optional"/>
<xsd:attribute name="gender" type="gender" use="required"/>
<xsd:attribute name="blog" type="blog" use="optional"/>
<xsd:attribute name="emails" type="email" use="optional"/>
</xsd:complexType>

2.3 Phần tử "name"

<xsd:element name="name" type="name"/>

<xsd:complexType name="name">
<xsd:sequence>
<xsd:element name="firstname" type="flName"/>
<xsd:element name="midname" type="midName" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="lastname" type="flName"/>
</xsd:sequence>
</xsd:complexType>

<xsd:simpleType name="midName">
<xsd:restriction base="xsd:string">
<xsd:minLength value="0"/>
<xsd:maxLength value="50"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="flName">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
<xsd:maxLength value="50"/>
<xsd:pattern value="[a-zA-Z]{1,20}"/>
</xsd:restriction>
</xsd:simpleType>

2.4 Phần tử "location"

<xsd:element name="location" type="location"/>

<xsd:complexType name="location">
<xsd:choice>
<xsd:element name="address" type="xsd:string"/>
<xsd:sequence>
<xsd:element name="latitude" type="xsd:string"/>
<xsd:element name="longitude" type="xsd:string"/>
</xsd:sequence>
</xsd:choice>
</xsd:complexType>

2.5 Phần tử "phone"

<xsd:element name="phone">
<xsd:complexType mixed="true">
<xsd:attribute name="type" type="phoneType" use="required"/>
</xsd:complexType>
</xsd:element>

<xsd:simpleType name="phoneType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Home"/>
<xsd:enumeration value="Fax"/>
<xsd:enumeration value="Work"/>
<xsd:enumeration value="Cell"/>
</xsd:restriction>
</xsd:simpleType>

2.6 Phần tử "decription"

<xsd:element name="decription" type="discription"/>

<xsd:complexType name="discription" mixed="true">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="b" type="xsd:string"/>
<xsd:element name="u" type="xsd:string"/>
<xsd:element name="i" type="xsd:string"/>
<xsd:element name="em" type="xsd:string"/>
<xsd:sequence>
<xsd:element name="br">
<xsd:complexType>
<xsd:attribute name="br" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:element name="strong" type="xsd:string"/>
</xsd:choice>
</xsd:complexType>

2.7 Các thuộc tính

2.7.1 contact_ID
Dữ kiệu kiểu ID và bắt buộc 1 contact phải có 1 "contact_ID". 

<xsd:attribute name="contact_ID" type="xsd:ID" use="required"/>

2.7.2 know(ID list)
Dữ kiệu kiểu IDREFS và 1 contact phải có thể có hoặc không 1 "know". 

<xsd:attribute name="know" type="xsd:IDREFS" use="optional"/>

2.7.3 gender
Dữ kiệu chỉ có thể chọn male|female và 1 contact phải có "gender". 

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

<xsd:simpleType name="gender">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Male"/>
<xsd:enumeration value="Female"/>
</xsd:restriction>
</xsd:simpleType>

2.7.4 blog
Thuộc tính blog phải có định dạng chuẩn http:\\www."abcd...".blogspot.com\"..."\"...".html.

<xsd:attribute name="blog" type="blog" use="optional"/>

<xsd:simpleType name="blog">
<xsd:restriction base="xsd:string">
<xsd:pattern value="((http|ftp|smtp):\\\\www\.\w{6,30}\.blogspot\.(com|edu|gov)                                 (\\\w{2,10}){0,5}\\\w{0,50}\.html)"/>
</xsd:restriction>
</xsd:simpleType>

2.7.5 email
Thuộc tính email phải có định dạng chuẩn "...."@"...".com.

<xsd:attribute name="emails" type="email" use="optional"/>

<xsd:simpleType name="email">
<xsd:restriction base="xsd:string">
<xsd:pattern value="([a-z]([a-z0-9]){4,50}@[a-z]{3,10}\.(com|edu|qov))"/>
</xsd:restriction>
</xsd:simpleType>


Link down bài này : contacts.xml contact.xsd name.xsd

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





XML_Schemas_simpleType

/ No Comments

<XML_Schemas>Bài 5: simpleType 

1. Cú pháp

<simpleType
id=ID
name=NCName
any attributes
>

(annotation?,(restriction|list|union))

</simpleType>

Trên lớp ta thường quan tâm đến phần trong nó là " restriction|list|union ".

id : ID của Type
name : Tên của Type.

2.Restriction


<restriction
id=ID
base=QName
any attributes
>

Content for simpleType:
(annotation?,(simpleType?,(minExclusive|minInclusive|
maxExclusive|maxInclusive|totalDigits|fractionDigits|
length|minLength|maxLength|enumeration|whiteSpace|pattern)*))

Content for simpleContent:
(annotation?,(simpleType?,(minExclusive |minInclusive|
maxExclusive|maxInclusive|totalDigits|fractionDigits|
length|minLength|maxLength|enumeration|whiteSpace|pattern)*)?,
((attribute|attributeGroup)*,anyAttribute?))

Content for complexContent:
(annotation?,(group|all|choice|sequence)?,
((attribute|attributeGroup)*,anyAttribute?))

</restriction>

Ví dụ 1 : 

<xsd:element name="age">
  <xsd:simpleType>
    <xsd:restriction base="xsd:integer">
      <xsd:minInclusive value="0"/>
      <xsd:maxInclusive value="100"/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:element>

Thuộc tính tuổi<age> có kiểu dữ liệu là integer và lằm trong khoảng từ [0 - 100].

Ví dụ 2 : 

 <xsd:simpleType name = "fname">
    <xsd:restriction base="xsd:string">
      <xsd:pattern value="[a-zA-Z]{1,20}"/>
    </xsd:restriction>
 </xsd:simpleType>

Tạo 1 kiểu có tên là "fname" base trên kiểu string dữ liệu của kiểu chỉ nhận các kí tự chữ in thường và in hoa.Tối thiểu 1 kí tự tối đa 20 kí tự.

Ví dụ 3 :

 <xsd:simpleType name = "lname">
    <xsd:restriction base="xsd:string">
      <xsd:minLength value="3"/>
      <xsd:maxLength value="20"/>
    </xsd:restriction>
  </xsd:simpleType>

Tạo 1 kiểu có tên là "lname" base trên kiểu string.Tối thiểu 5 kí tự tối đa 20 kí tự.

Ví dụ 4 : 

<simpleType name="phoneType"> 
    <restriction base="string"> 
        <enumeration value="Home"/> 
        <enumeration value="Work"/> 
        <enumeration value="Cell"/> 
        <enumeration value="Fax"/> 
    </restriction> 
</simpleType>
Tạo 1 kiểu có tên là "phonType" base trên kiểu string.Chỉ nhận các giá trị là : Home | Work | Cell | Fax.

3. Pattern

- "." 1 kí tự bất kì.
- "\d" 1 kí tự số (0 - 9).
- "\w" 1 kí tự văn bản (a - z A - Z).
- "\D" kí tự không phải là số.
- "\W" kí tự không phải là kí tự chữ.
- "\t" kí tự Tab.
- "\n" kí tự xuống dòng .
- "\." kí tự ".".
- "[]" đoạn. VD: [0 9], [a z].
- "()" nhóm.
- "{m,n}" lặp min = m , max = n lần.
- "{m}" lặp đúng m lần.
- "+"  một hoặc nhiều.
- "*"  không hoặc nhiều.
- "?" 1 hoặc 0.
- "|" hoặc.

Ví dụ: 

<xsd:pattern value="[a-zA-Z]{1,20}"/>
Từ 1 - 20 kí tự có giá trị trong khoảng [a - z] [A - Z].
<xsd:pattern value="male|female"/>
Chỉ nhận 1 trong 2 giá trị "male" hoặc "female".
<xsd:pattern value="\w{10}"/>
Đúng 10 kí tự văn bản.

Ví dụ 2:

Thuộc tính blog có dạng : http:\\www.nguyenphutuvnua.blogspot.com\2016\05\xmlschemasattribute.html

<xsd:pattern 
value="((http|ftp|smtp):\\\\www\.\w{6,30}\.blogspot\.(com|edu|gov)(\\\w{2,10}){0,5}\\\w{0,50}\.html)"
/>

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
















XML_Schemas_Attribute

Tuesday, May 17, 2016 / 2 Comments

<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.