News Ticker

Menu

Browsing "Older Posts"

Browsing Category "XML"

XSLT_Bài tập.

Thursday, May 19, 2016 / No Comments

<XML_XSLT> Bài 10: Bài tập trên lớp về XSLT.

1. FIle XML dowload  student.xml student.html

2. FIle XSLT dowload student.xsl

3. Tổng hợp tất cả các bài tập: XML_Tổng hợp tất cả bài tập

XSLT

/ No Comments

<XML_XSLT> Bài 9: Tổng quan.

Cũng như nhưng ngôn ngữ lập trình khác trong XSLT cũng thực hiện theo kiểu có duy nhất một chương trình chính <main> để thực hiện các yêu cầu và có các function bên trong. Và có các chương trình con<Function>.

Cú pháp.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="text"/>
<xsl:template match="/">

</xsl:template>
</xsl:stylesheet>

1. Biến và tham số

1.1 Biến <Variable>

Khai báo:

<xsl:variable name="tên biến" select="Giá trị của biến"/>

Trong XSLT giá trị của biến là cố định không thay đổi.

Ví dụ: Tính tổng 3 + 2.

<xsl:template name="add">
<xsl:variable name="a" select="3"/>
<xsl:variable name="b" select="2"/>
<xsl:value-of select="$a + $b"/>
</xsl:template>

Gọi lại trong hàm main<main template>

<xsl:call-template name="add"/>

1.2 Tham số

Biến là giá trị cố định không thể thay đổi nên trong XSLT ta có thể sử dụng tham số để thao tác khi có sự thay đổi giá trị.

Khai báo: 

<xsl:param name="tên tham số" select="Giá trị"/>
Truyền giá trị:
<xsl:with-param name="tên tham số" select="giá trị"/>

Ví dụ: Tính tổng 3 + 2

<xsl:template name="add2">
<xsl:param name="a" />
<xsl:param name="b"/>
<xsl:value-of select="$a + $b"/>
</xsl:template>

main template.

<xsl:call-template name="add">
<xsl:with-param name="a" select="3"/>
<xsl:with-param name="b" select="2"/>
</xsl:call-template>

1.2 Lệnh if trong XSLT

Cú pháp:

<xsl:if test="Điều kiện">
  ...Câu lệnh...
</xsl:if>

Lệnh if kiểm tra điều kiện: Nếu điều kiện đúng thì thực hiện câu lệnh.

Ví dụ:

<xsl:template match="/">
<xsl:variable name="test" select="5"/>
<xsl:if test="$test &gt; 5">
Gia tri cua bien test lon hon 5
</xsl:if>
<xsl:if test="$test &lt; 5">
Gia tri cua bien test nho hon 5
</xsl:if>
<xsl:if test="$test = 5">
Gia tri cua bien test bang 5
</xsl:if>

</xsl:template>

Kết quả : Gia tri cua bien test bang 5

1.3 Choose trong XSLT

Cú pháp: 

<xsl:choose>
  <xsl:when test="Điều kiện">
    ... some output ...
  </xsl:when>
  <xsl:otherwise>
    ... some output ....
  </xsl:otherwise>
</xsl:choose>

Kiểm tra điều kiện nếu đúng thì thực hiện câu lệnh bên trong không thì sẽ thực hiện câu lệnh trong "otherwise"

VD:

<xsl:template match="/">
<xsl:variable name="test" select="5"/>
<xsl:choose>
<xsl:when test="$test &gt; 5">
Gia tri cua bien test lon hon 5
</xsl:when>
<xsl:when test="$test &lt; 5">
Gia tri cua bien test nho hon 5
</xsl:when>
<xsl:otherwise>
Gia tri cua bien test bang 5
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Kết quả : Gia tri cua bien test bang 5

1.4 Vòng lặp For

Trong XSLT không có câu lệnh lặp For vậy nên ta phải tự viết vòng lặp cho nó. Dựa trên những gì mà XSLT cho phép sử dụng : IF , Param<tham số> .

VD: Bài toán in ra dẫy số nguyên từ 1 - n .

<XSLT>Lặp lùi:

<xsl:template name="for_lui">
<xsl:param name="min" select="1"/>
<xsl:param name="n"/>
<xsl:if test="$min = $n">
<xsl:value-of select="$n"/>
</xsl:if>

<xsl:if test="$min &lt; $n">
<xsl:value-of select="$n"/>
<xsl:call-template name="for_lui">
<xsl:with-param name="min" select="$min"/>
<xsl:with-param name="n" select="$n - 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

<XSLT>Lặp tiến:

<xsl:template name="for_tien">
<xsl:param name="min" select="1"/>
<xsl:param name="n"/>
<xsl:if test="$min = $n">
<xsl:value-of select="$n"/>
</xsl:if>

<xsl:if test="$min &lt; $n">
<xsl:value-of select="$min"/>
<xsl:call-template name="for_tien">
<xsl:with-param name="min" select="$min + 1"/>
<xsl:with-param name="n" select="$n"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

main template : 

<xsl:call-template name="for_lui">
<xsl:with-param name="n" select="5"/>
</xsl:call-template>

Kết quả : 54321

<xsl:call-template name="for_tien">
<xsl:with-param name="n" select="5"/>
</xsl:call-template>

Kết quả : 12345

1.5 For-each

For-each thường dùng để lặp trong XSLT 

Cú pháp: 


<xsl:for-each select="student/courses/course">

....Câu lệnh...

</xsl:for-each>

student/courses/course là XPath để trỏ tới địa chỉ cần lặp.
Lặp tất cả các "course" trong "courses" của "student".

Ví dụ:

<xsl:for-each select="student/courses/course">
<xsl:sort select="@score" order="ascending"/>
<xsl:if test="@score &gt; 8">
<tr class="goodScore">
<td><p><xsl:value-of select="position()"/></p></td>
<td><p><xsl:value-of select="@courseID"/></p>
</td>
<td><p><xsl:value-of select="title"/></p>
</td>
<td><p><xsl:value-of select="@unit"/></p></td>
<td><p><xsl:value-of select="@score"/></p></td>
</tr>
</xsl:if>
<xsl:if test="@score &lt; 5">
<tr class="badScore">
<td><p><xsl:value-of select="position()"/></p></td>
<td><p><xsl:value-of select="@courseID"/></p>
</td>
<td><p><xsl:value-of select="title"/></p>
</td>
<td><p><xsl:value-of select="@unit"/></p></td>
<td><p><xsl:value-of select="@score"/></p></td>
</tr>
</xsl:if>
<xsl:if test="(@score &gt;= 5 ) and (@score &lt;= 8)">
<tr>
<td><p><xsl:value-of select="position()"/></p></td>
<td><p><xsl:value-of select="@courseID"/></p>
</td>
<td><p><xsl:value-of select="title"/></p>
</td>
<td><p><xsl:value-of select="@unit"/></p></td>
<td><p><xsl:value-of select="@score"/></p></td>
</tr>
</xsl:if>
</xsl:for-each>



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






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.