introduce to xml
DESCRIPTION
Content: - What is XML? - Well-Formed XML - XML Namespaces References: Beginning XML, 5th Edition, Joe Fawcett, Liam R. E. Quin, Danny AyersTRANSCRIPT
![Page 1: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/1.jpg)
Introduction
Nguyễn Đăng Khoa
![Page 2: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/2.jpg)
Content
• What is XML?• Well-Formed XML• XML Namespaces
![Page 3: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/3.jpg)
What is XML?
• Stands for Extensible Markup Language• First draft was published in 1996• A revised version as recommendation on Feb
10, 1998 (by W3C)• XML derived as a subset of SGML (Standard
Generalized Markup Language)
![Page 4: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/4.jpg)
XML’s goals
• Before XML– Data formats were proprietary
• Goals:– To make data more interchangeable– Is readable by both humans and machines
![Page 5: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/5.jpg)
Data-centric vs. Document-centric
• 2 main types of XML formats– Store pure data: configuration– Add metadata to documents: XHTML
![Page 6: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/6.jpg)
Advantages
• A clear separation between data and presentation
• Easy extensibility of XML files• Hierarchical Data Representation• Interoperability
![Page 7: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/7.jpg)
Disadvantages
• Increase in the size of the file
![Page 8: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/8.jpg)
XML In Practice
• Configuration Files• Web Services• Web Content (XHTML)• Document Management• Database Systems• Image Representation• Business Interoperability
![Page 9: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/9.jpg)
Well-Formed XML
![Page 10: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/10.jpg)
Well-Formed XML - XML Prolog
![Page 11: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/11.jpg)
Well-Formed XML - XML Prolog• Optional• Must come first
• version• 1.0 (default) or 1.1
• encoding• UTF-8 (default) or
variety of Unicode• standalone• yes (default) or no
![Page 12: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/12.jpg)
Well-Formed XML - Comment
![Page 13: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/13.jpg)
Well-Formed XML - Comment• Human read <!-- comment -->
![Page 14: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/14.jpg)
Well-Formed XML – Root element
![Page 15: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/15.jpg)
Well-Formed XML – Root element• Must have one and only
one in document• Everything else lies
under this element to form a hierarchical tree
![Page 16: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/16.jpg)
Well-Formed XML – Elements
![Page 17: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/17.jpg)
Well-Formed XML – Elements• Basic building blocks• Can be used to show
individual or repetitive items of data
• 2 ways to define <element>
content </element> <element />
![Page 18: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/18.jpg)
Well-Formed XML – Elements• All elements must be nested
underneath the root element• You can’t have the end tag of
an element before the end tag of one nested below it<myElement><elementA>
<elementB></elementA></elementB>
</myElement>
![Page 19: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/19.jpg)
Well-Formed XML – Naming Styles• Pascal-casing
<MyElement />• Camel-casing
<myElement />• Underscored names
<my_element />• Hyphenated names
<my-element />
![Page 20: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/20.jpg)
Well-Formed XML – Naming Specifications
• can begin with either an underscore or an uppercase or lowercase letter from the Unicode character set
• Subsequent characters can also be a dash (-) or a digit
• Case-sensitive• the start and end tags
must match exactly• cannot contain spaces
![Page 21: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/21.jpg)
Well-Formed XML – Naming Specifications - Examples
![Page 22: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/22.jpg)
Well-Formed XML – Naming Specifications - Examples
![Page 23: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/23.jpg)
Well-Formed XML – Exercise
• <list><title>The first list</title><item>An item</list>
• <item>An item</item><item>Another item</item>
• <para>Bathing a cat is a <emph>relatively</emph> easy task as long as the cat is willing.</para>
• <bibl><title>How to Bathe a Cat<author></title>Merlin Bauer<author></bibl>
![Page 24: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/24.jpg)
Well-Formed XML - Attributes
![Page 25: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/25.jpg)
Well-Formed XML - Attributes• name-value pairs
associated with an element
![Page 26: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/26.jpg)
Well-Formed XML – Attributes - Rules• consist of a name and a
value separated by an equals sign
• The name follows the same rules as element names
• The value must be in quotes
• There must be a value part• Attribute names must be
unique per element
![Page 27: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/27.jpg)
Well-Formed XML – Attributes - Examples
![Page 28: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/28.jpg)
Well-Formed XML – Attributes - Examples
![Page 29: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/29.jpg)
Well-Formed XML – Character content - Restrictions
![Page 30: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/30.jpg)
Well-Formed XML – Character content - Restrictions
• Ampersand (&)• Left angle bracket (<)
![Page 31: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/31.jpg)
Well-Formed XML – Entity and Character References
• There are two ways of inserting characters into a document that cannot be used directly– Entity references
• Start with an ampersand (&) and finish with a semicolon (;)• There are five built-inentity references in XML
– Character references• Begin with &# and end with a semicolon (;)• Example: the Greek letter omega (Ω) as a reference it would
be Ω in hexadecimal or Ω in decimal
![Page 32: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/32.jpg)
Well-Formed XML – Entity and Character References
![Page 33: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/33.jpg)
Well-Formed XML – Entity and Character References
<!DOCTYPE myElement [<!ENTITY copyright “© Wrox 2012”>
]>
![Page 34: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/34.jpg)
Well-Formed XML – Elements Versus Attributes
![Page 35: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/35.jpg)
Well-Formed XML – Elements Versus Attributes
Rule ????
![Page 36: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/36.jpg)
Well-Formed XML – Elements Versus Attributes
Attributes• There is only one piece of
data• Names cannot be repeated• Make file size is smaller
– Good to sent across network
Elements• The data is not a simple
type• Items may need to be
repeated• Items can be ordered• A large amount of content
that is just text
![Page 37: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/37.jpg)
Well-Formed XML – Elements Versus Attributes - Examples
![Page 38: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/38.jpg)
Well-Formed XML – Elements Versus Attributes - Examples
![Page 39: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/39.jpg)
Well-Formed XML – Processing Instructions
• is used to communicate with the application that is consuming the XML– It is not used directly by the XML parser at all
![Page 40: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/40.jpg)
Well-Formed XML – CDATA
• These are used as a way to avoid repetitive escaping of characters
• Starts with <![CDATA[ and ends with ]]>• Example: you want data in your document
1 kilometer < 1 mile1 pint < 1 liter1 pound < 1 kilogram
![Page 41: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/41.jpg)
Well-Formed XML – CDATA
![Page 42: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/42.jpg)
Well-Formed XML – CDATA
• A common use of CDATA is in XHTML, the XML version of HTML
![Page 43: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/43.jpg)
Well-Formed XML – CDATA
• A common use of CDATA is in XHTML, the XML version of HTML
![Page 44: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/44.jpg)
Well-Formed XML – Exercise
![Page 45: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/45.jpg)
XML Namespaces – Example
You need a new table
Dining table
Database table
HTML table
![Page 46: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/46.jpg)
XML Namespaces
• A way of grouping elements and attributes under a common heading in order to differentiate them from similarly-named items
![Page 47: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/47.jpg)
XML Namespaces – Example
![Page 48: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/48.jpg)
XML Namespaces – Example
![Page 49: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/49.jpg)
XML Namespaces – Why do you need namespaces?
• You won’t always be using own XML formats entirely within your own systems
![Page 50: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/50.jpg)
XML Namespaces – How do you choose a namespace?
• In Java, are called packages• In C#, are called namespaces– System.Windows.Forms.Timer– System.Timers.Timer– System.Threading.Timer
![Page 51: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/51.jpg)
XML Namespaces – How do you choose a namespace?
• You can choose virtually any string of characters to make sure your element’s full name is unique
• W3C recommend– URIs
![Page 52: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/52.jpg)
URLs, URIs, and URNs
• URL is a Uniform Resource Locator, tells you the how and where of something– [Scheme]://[Domain]:[Port]/[Path]?
[QueryString]#[FragmentId]– http://www.wrox.com/remtitle.cgi?isgn=0470114878
• URN is a Uniform Resource Name, is simply a unique name– urn:[namespace identifier]:[namespace specific string]– urn:isbn:9780470114872
• URI is a Uniform Resource Identifier, is URL or URN
![Page 53: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/53.jpg)
XML Namespaces – How to declare a namespace?
• If you want all elements to be under the namespace– Declare a default namespace
![Page 54: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/54.jpg)
XML Namespaces – How to declare a namespace?
• If you want specific elements to be under the namespace– Declare a namespace explicitly– Choose prefix to represent namespace• Some prefixes are reserved, such as xml, xmlns, and
any other combinations beginning with the characters xml
![Page 55: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/55.jpg)
XML Namespaces – How to declare a namespace?
Qualified Name
(QName)Local Name
![Page 56: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/56.jpg)
XML Namespaces – How to declare a namespace?
![Page 57: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/57.jpg)
XML Namespaces – Declaring more than one namespace
• <applicationUsers> element belongs to http://wrox.com/namespaces/applications/hr/config namespace
• <user> elements belong to http://wrox.com/namespaces/general/entities namespace
![Page 58: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/58.jpg)
XML Namespaces – Declaring more than one namespace
![Page 59: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/59.jpg)
XML Namespaces – Declaring more than one namespace
![Page 60: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/60.jpg)
XML Namespaces
![Page 61: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/61.jpg)
XML Namespaces – Real world
• XML Schemas– Defining the structure of a document
• Combination documents– Merging documents from more than one source
• Versioning– Differentiating between different versions of an
XML format
![Page 62: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/62.jpg)
XML Namespaces – Combination documents
![Page 63: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/63.jpg)
XML Namespaces – Versioning
• Differentiating between different versions of an XML format
• Go back to employees.xml– Namespace is
http://wrox.com/namespaces/general/employee– Newer version:
http://wrox.com/namespaces/general/employee/v2
![Page 64: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/64.jpg)
XML Namespaces – Versioning
How do I want the application to handle the two different versions?
![Page 65: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/65.jpg)
XML Namespaces – Versioning
• Version one of the application opens a version one file
• Version one of the application opens a version two file
• Version two of the application opens a version one file
• Version two of the application opens a version two file
![Page 66: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/66.jpg)
XML Namespaces – Versioning – Practical
![Page 67: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/67.jpg)
XML Namespaces – When to use and not use namespaces
When namespaces are needed
• When there’s no choice• When you need
interoperability• When you need validation
When namespaces are not needed
• When you have the need to store or exchange data for relatively small documents that will be seen only by a limited number of systems
![Page 68: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/68.jpg)
XML Namespaces – Common namespaces
• The XML Namespacehttp://www.w3.org/XML/1998/namespace– Attributes:• xml:lang• xml:space• xml:base• xml:id• xml:Father
![Page 69: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/69.jpg)
XML Namespaces – Common namespaces
• The XMLNS Namespacehttp://www.w3.org/2000/xmlns/
• The XML Schema Namespacehttp://www.w3.org/2001/XMLSchema
• The XSLT Namespace (xsl or xslt)http://www.w3.org/1999/XSL/Transform
• The SOAP Namespace (soap, soap12)http://schemas.xmlsoap.org/soap/envelope/ (SOAP 1.1)http://www.w3.org/2003/05/soap-envelope (SOAP 1.2)
• The WSDL Namespace (wsdl)http://www.w3.org/ns/wsdl (1.0, 2.0)
![Page 70: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/70.jpg)
XML Namespaces – Common namespaces
• The Atom Namespacehttp://www.w3.org/2005/Atom
• The MathML Namespacehttp://www.w3.org/1998/Math/MathML
• The Docbook Namespacehttp://docbook.org/ns/docbook
![Page 71: Introduce to XML](https://reader036.vdocuments.net/reader036/viewer/2022081413/546b38a1af795919088b5df3/html5/thumbnails/71.jpg)
XML Namespaces – Exercise