Download presentation
Presentation is loading. Please wait.
1
XML
2
Introduction XML stands for EXtensible Markup Language.
XML is a markup language much like HTML XML was designed to describe data, not to display data XML tags are not predefined. You must define your own tags XML is designed to be self-descriptive XML is a W3C Recommendation
3
XML Based on Standard Generalized Markup Language (SGML)
Version 1.0 introduced by World Wide Web Consortium (W3C) in 1998 Bridge for data exchange on the Web
4
HTML vs XML The Difference Between XML and HTML
XML is not a replacement for HTML. XML and HTML were designed with different goals: XML was designed to describe data, with focus on what data is HTML was designed to display data, with focus on how data looks HTML is about displaying information, while XML is about carrying information.
5
XML HTML HTML vs XML Extensible set of tags Fixed set of tags
Content orientated Standard Data infrastructure Allows multiple output forms Fixed set of tags Presentation oriented No data validation capabilities Single presentation
6
XML application XML Separates Data from HTML
XML Simplifies Data Sharing XML Simplifies Data Transport XML Simplifies Platform Changes XML Makes Your Data More Available Internet Languages Written in XML
7
XML structure XML documents form a tree structure that starts at "the root" and branches to "the leaves". <?xml version="1.0" encoding="UTF-8"?> <student> <name>Tove</name> <city>Jani</city> <percentage>Reminder</percentage> </student>
8
Syntax All XML Elements Must Have a Closing Tag
XML Tags are Case Sensitive XML Elements Must be Properly Nested XML Documents Must Have a Root Element XML Attribute Values Must be Quoted White-space is Preserved in XML Comments in XML <!-- This is a comment -->
9
Entity References Some characters have a special meaning in XML.
<message>if age < 25 then</message> < less than > greater than & & ampersand ' ' apostrophe " " quotation mark
10
Element An XML element is everything from (including) the element's start tag to (including) the element's end tag. An element can contain: other elements text attributes or a mix of all of the above...
11
<bookstore> <book category="CHILDREN">
<bookstore> <book category="CHILDREN"> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title>Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
12
Continue Empty XML Elements XML Naming Rules
<book></book> & <book /> XML Naming Rules Names can contain letters, numbers, and other characters Names cannot start with a number or punctuation character Names cannot start with the letters xml (or XML, or Xml, etc) Names cannot contain spaces Any name can be used, no words are reserved.
13
Continue Make names descriptive: <first_name>, <last_name>. Make names short and simple, like this: <book_title> not like this: <the_title_of_the_book>. Avoid "-". If you name something "first-name", some software may think you want to subtract name from first. Avoid ".". If you name something "first.name", some software may think that "name" is a property of the object "first." Avoid ":". Colons are reserved to be used for something called namespaces (more later). Non-English letters are perfectly legal in XML, but watch out for problems if your software doesn't support them.
14
Attributes XML elements can have attributes, just like HTML.
Attributes provide additional information about an element. <file type="gif">computer.gif</file> XML Attributes Must be Quoted (Single or double) XML Attributes for Metadata
15
Avoid XML Attributes? Some of the problems with using attributes are:
attributes cannot contain multiple values (elements can) attributes cannot contain tree structures (elements can) attributes are not easily expandable (for future changes) Attributes are difficult to read and maintain. Use elements for data. Use attributes for information that is not relevant to the data.
16
Namespace XML Namespaces provide a method to avoid element name conflicts. In XML, element names are defined by the developer. This often results in a conflict when trying to mix XML documents from different XML applications.
17
<table> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table> <table> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table>
18
Solution Solving the Name Conflict Using a Prefix
XML Namespaces - The xmlns Attribute When using prefixes in XML, a so-called namespace for the prefix must be defined. The namespace is defined by the xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI". (Uniform Resource Identifier) URI can be URL or URN
19
<root> <h:table xmlns:h="http://www. w3
<root> <h:table xmlns:h=" <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> <f:table xmlns:f=" <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> </root>
20
Encoding UTF : Universal charter set for Transformation Format
UTF-8 : 8 uses 1 byte (8-bits) to represent characters in the ASCII set, and two or three bytes for the rest. TF-8 is the standard character encoding on the web. UTF-8 is the default character encoding for HTML5, CSS, JavaScript, PHP, SQL, and XML.
21
XML with CSS With CSS (Cascading Style Sheets) you can add display information to an XML document. <?xml-stylesheet type="text/css" href=“xc.css"?>
22
XSLT XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML documents. XSLT stands for XSL Transformations. XSLT to transform XML documents into other formats, like XHTML. XSL consists of three parts: XSLT - a language for transforming XML documents XPath - a language for navigating in XML documents XSL-FO - a language for formatting XML documents
23
Correct Style Sheet Declaration
<xsl:stylesheet version="1.0" xmlns:xsl=" Or <xsl:transform version="1.0" xmlns:xsl=" <?xml-stylesheet type="text/xsl" href="simple.xsl" ?>
24
<xsl:template>
An XSL style sheet consists of one or more set of rules that are called templates. A template contains rules to apply when a specified node is matched. The match attribute is used to associate a template with an XML element. Match = “/” => that means entire document has been selected At the end you have to write the </xsl:template>
25
<xsl:value-of>
The <xsl:value-of> element is used to extract the value of a selected node. The <xsl:value-of> element can be used to extract the value of an XML element and add it to the output stream of the transformation. <tr> <td><xsl:value-of select="catalog/cd/title"/></td> <td><xsl:value-of select="catalog/cd/artist"/></td> </tr>
26
<xsl:for-each>
The <xsl:for-each> element allows you to do looping in XSLT. <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each>
27
<xsl:sort> The <xsl:sort> element is used to sort the output. <xsl:for-each select="catalog/cd"> <xsl:sort select="artist"/> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each>
28
<xsl:if> The <xsl:if> element is used to put a conditional test against the content of the XML file. <xsl:for-each select="catalog/cd"> <xsl:if test="price > 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> <td><xsl:value-of select="price"/></td> </tr> </xsl:if> </xsl:for-each>
29
<xsl:choose> The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests. <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price > 10"> <td bgcolor="#ff00ff"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="artist"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each>
30
DTD A Document Type Definition (DTD) defines the legal building blocks of an XML document. It defines the document structure with a list of legal elements and attributes. Internal and external DTD
31
Internal DTD <!DOCTYPE root-element [element-declarations]>
<?xml version="1.0"?> <!DOCTYPE nirma [ <!ELEMENT nirma (department, no_of_students, no_of_faculty)> <!ELEMENT department (#PCDATA)> <!ELEMENT no_of_students (#PCDATA)> <!ELEMENT no_of_faculty (#PCDATA)> ]> <nirma> <department> CSE </department> < no_of_students> 2300 </ no_of_students > < no_of_faculty> 53 </ no_of_faculty > </note>
32
External DTD Why? <!DOCTYPE root-element SYSTEM "filename">
With a DTD, each of your XML files can carry a description of its own format. With a DTD, independent groups of people can agree to use a standard DTD for interchanging data. Your application can use a standard DTD to verify that the data you receive from the outside world is valid.
33
Building Blocks The main building blocks of both XML and HTML documents are elements. Elements Attributes Entities PCDATA CDATA
34
Elements In a DTD, elements are declared with an ELEMENT declaration.
<!ELEMENT element-name category> or <!ELEMENT element-name (element-content)> Empty elements will be declare with the categorical keyword EMPTY. <br / > or <no_of holidays> </no_of_holidays> <!ELEMENT br EMPTY> <!ELEMENT element-name (#PCDATA)>
35
Continue Elements declared with the category keyword ANY, can contain any combination of parsable data. <!ELEMENT element-name ANY> Elements with one or more children are declared with the name of the children elements inside parentheses. <!ELEMENT element-name (child1,child2,...)> <!ELEMENT element-name (child-name)> The example above declares that the child element “department" must occur once, and only once inside the "nirma" element
36
Continue The + sign in the example above declares that the child element “department" must occur one or more times inside the "nirma" element. <!ELEMENT element-name (child-name+)> The * sign in the example above declares that the child element “department" can occur zero or more times inside the "nirma" element. <!ELEMENT element-name (child-name*)> ?
37
Continue <!ELEMENT note (to,from,header,(message|body))>
The example above declares that the "note" element must contain a "to" element, a "from" element, a "header" element, and either a "message" or a "body" element. <!ELEMENT note (#PCDATA|to|from|header|message)*> The example above declares that the "note" element can contain zero or more occurrences of parsed character data, "to", "from", "header", or "message" elements.
38
Attributes <!ATTLIST element-name attribute-name attribute-type attribute- value> <!ATTLIST payment type CDATA "check"> Xml code <payment type=“check” />
39
Attributes Value Explanation value The default value of the attribute
#REQUIRED The attribute is required #IMPLIED The attribute is optional #FIXED value The attribute value is fixed
40
Default value DTD: <!ELEMENT square EMPTY> <!ATTLIST square width CDATA "0"> Valid XML: <square width="100" />
41
Required DTD: <!ATTLIST person number CDATA #REQUIRED> Valid XML: <person number="5677" /> Invalid XML: <person />
42
Implied DTD: <!ATTLIST contact fax CDATA #IMPLIED> Valid XML: <contact fax=" " /> Valid XML: <contact />
43
FIXED DTD: <!ATTLIST sender company CDATA #FIXED "Microsoft"> Valid XML: <sender company="Microsoft" /> Invalid XML: <sender company="W3Schools" />
44
Enumerated Attribute Values
DTD: <!ATTLIST payment type (check|cash) "cash"> XML example: <payment type="check" /> or <payment type="cash" />
45
Entity DTD Example: <!ENTITY writer "Donald Duck."> <!ENTITY copyright "Copyright W3Schools."> XML example: <author>&writer;©right;</author>
46
XSD XML Schema is an XML-based alternative to DTD.
XML Schema Definition (XSD). Extension of the file will be .xsd XSD is a successor of DTD. XSD support data type and namespace.
47
Purpose Defines elements that can appear in a document
Defines attributes that can appear in a document Defines which elements are child elements Defines the order of child elements Defines the number of child elements Defines whether an element is empty or can include text Defines data types for elements and attributes Defines default and fixed values for elements and attributes
48
XML Schemas are Extensible
Reuse your Schema in other Schemas Create your own data types derived from the standard types Reference multiple schemas in the same document
49
<schema> The <schema> element is the root element of every XML Schema. <?xml version="1.0"?> <xs:schema> </xs:schema>
50
Attributes with <schema>
xmlns:xs= targetNamespace elementFormDefault Xmlns schemaLocation
51
Simple Element A simple element is an XML element that contains only text. It cannot contain any other elements or attributes. <xs:element name="xxx" type="yyy"/> xs:string, xs:decimal, xs:integer, xs:boolean, xs:date, xs:time XML file <lastname>Refsnes</lastname> XSD File <xs:element name="lastname" type="xs:string"/> default and fixed value
52
Simple Attributes Simple elements cannot have attributes. If an element has attributes, it is considered to be of a complex type. But the attribute itself is always declared as a simple type. XML code <lastname lang="EN">Smith</lastname> XSD code <xs:attribute name="lang" type="xs:string"/> default, fixed, use = “Required”
53
Restriction/Facets Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets. Types of restriction Restrictions on Values Restrictions on a Set of Values Restrictions on a Series of Values Restrictions on Whitespace Characters Restrictions on Length Restrictions for Datatypes
54
Restriction on value <xs:element name="age"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="120"/> </xs:restriction> </xs:simpleType> </xs:element>
55
Restrictions on a Set of Values
<xs:element name="car"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Audi"/> <xs:enumeration value="Golf"/> <xs:enumeration value="BMW"/> </xs:restriction> </xs:simpleType> </xs:element>
56
Restrictions on a Series of Values
<xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-z]"/> </xs:restriction> </xs:simpleType> </xs:element>
57
Continue <xs:element name="initials"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/> </xs:restriction> </xs:simpleType> </xs:element> If I want to validate that entered charter should be only X or Y or Z?
58
Restrictions on a Series of Values
<xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="([a-z])*"/> </xs:restriction> </xs:simpleType> </xs:element> If I want to validate data sToP….. What should be the validation? What should be the validation for male ---- female option in gender?
59
Continue <xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-zA-Z0-9]{8}"/> </xs:restriction> </xs:simpleType> </xs:element>
60
Restrictions on Whitespace Characters
<xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="preserve"/> </xs:restriction> </xs:simpleType> </xs:element> Other values can be : replace, collapse
61
Restrictions on Length
<xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="8"/> </xs:restriction> </xs:simpleType> </xs:element> minLenght and maxLenght
62
Restrictions for Datatypes
Constraint Description enumeration Defines a list of acceptable values fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero length Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value) maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal to this value) maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero minExclusive Specifies the lower bounds for numeric values (the value must be greater than this value) minInclusive Specifies the lower bounds for numeric values (the value must be greater than or equal to this value) minLength Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero pattern Defines the exact sequence of characters that are acceptable totalDigits Specifies the exact number of digits allowed. Must be greater than zero whitespace Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled
63
Empty elements <product prodid="1345" /> XSD code
<xs:element name="product"> <xs:complexType> <xs:complexContent> <xs:restriction base="xs:integer"> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:restriction> </xs:complexContent> </xs:complexType> </xs:element>
64
Indicators Order indicator Occurrence indicator Group indicator
All, choice and sequence Occurrence indicator minOccurs maxOccurs Group indicator Group name attributeGroup name
65
Order indicator All : sequence doesn’t matter
Sequence: sequence matter Choice :either one child element or another can occur
66
Occurrence indicator Occurrence indicators are used to define how often an element can occur. <xs:element name="child_name" type="xs:string“ maxOccurs="10" minOccurs="0"/> Unbounded
67
Group indicator <xs:group name="persongroup"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="birthday" type="xs:date"/> </xs:sequence> </xs:group> <xs:element name="person" type="personinfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:group ref="persongroup"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType>
68
Substitution <xs:element name="name" type="xs:string"/> <xs:element name="navn" substitutionGroup="name"/> <xs:complexType name="custinfo"> <xs:sequence> <xs:element ref="name"/> </xs:sequence> </xs:complexType> <xs:element name="customer" type="custinfo"/> <xs:element name="kunde" substitutionGroup="customer"/>
69
<customer> <name>John Smith</name> </customer>
<kunde> <navn>John Smith</navn> </kunde> block attribute
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.