Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML BASICS.

Similar presentations


Presentation on theme: "XML BASICS."— Presentation transcript:

1 XML BASICS

2 Basics XML stands for eXtensible Markup Language.
XML was designed to store and transport data. XML was designed to be both human- and machine-readable. XML tags identify the data and are used to store and organize the data, rather than specifying how to display it like HTML tags, which are used to display the data. Three important characteristics of XML XML is extensible: XML allows you to create your own self-descriptive tags, or language, that suits your application. XML carries the data, does not present it: XML allows you to store the data irrespective of how it will be presented. XML is a public standard: XML was developed by an organization called the World Wide Web Consortium (W3C) and is available as an open standard.

3 XML Usage XML can work behind the scene to simplify the creation of HTML documents for large web sites. XML can be used to exchange the information between organizations and systems. XML can be used for offloading and reloading of databases. XML can be used to store and arrange the data, which can customize your data handling needs. XML can easily be merged with style sheets to create almost any desired output. Virtually, any type of data can be expressed as an XML document.

4 What is Markup? XML is a markup language that defines set of rules for encoding documents in a format that is both human-readable and machine-readable. Markup is information added to a document that enhances its meaning in certain ways, in that it identifies the parts and how they relate to each other. Markup language is a set of symbols that can be placed in the text of a document to demarcate and label the parts of that document. Difference Between XML and HTML XML was designed to carry data - with focus on what data is HTML was designed to display data - with focus on how data looks XML tags are not predefined like HTML tags

5 XML - Syntax

6 <?xml version="1.0" encoding="UTF-8"?>
XML Declaration <?xml version="1.0" encoding="UTF-8"?> Syntax Rules for XML declaration The XML declaration is case sensitive and must begin with "<?xml>" where "xml" is written in lower-case. If document contains XML declaration, then it strictly needs to be the first statement of the XML document. The XML declaration strictly needs be the first statement in the XML document. Tags and Elements Each XML-element needs to be closed either with start or with end elements  <element>....</element>

7 <?xml version="1.0" encoding="UTF-8"?>
<bookstore> <book category="cooking"> <title>Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title>Harry Potter</title> <author>J K. Rowling</author> <price>29.99</price> <book category="web"> <title>Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </bookstore>

8 XML Syntax Rules XML Documents Must Have a Root Element
<root>   <child>     <subchild>.....</subchild>   </child> </root> The XML Prolog <?xml version="1.0" encoding="UTF-8"?> The XML prolog is optional. If it exists, it must come first in the document. XML documents can contain international characters, like Norwegian øæå or French êèé. To avoid errors, you should specify the encoding used, or save your XML files as UTF-8. UTF-8 is the default character encoding for XML documents.

9 All XML Elements Must Have a Closing Tag
<p>This is a paragraph.</p> XML Tags are Case Sensitive <Message>This is incorrect</message> <message>This is correct</message> XML Elements Must be Properly Nested <b><i>This text is bold and italic</i></b> XML Attribute Values Must be Quoted <note date="12/11/2007">   <to>Tove</to>   <from>Jani</from> </note>

10 This will generate an XML error:
Entity References This will generate an XML error: <message>salary < 1000</message> To avoid this error, replace the "<" character with an entity reference: <message>salary < 1000</message> < less than > greater than & & ampersand  &apos; ' apostrophe " " quotation mark

11 XML Elements Comments in XML <!-- This is a -- comment -->
XML documents that conform to the syntax rules above are said to be "Well Formed" XML documents. XML Elements An XML element is everything from (including) the element's start tag to (including) the element's end tag. An element can contain: text attributes other elements or a mix of the above

12 <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> <title>, <author>, <year>, and <price> have text content because they contain text (like 29.99). <bookstore> and <book> have element contents, because they contain elements. <book> has an attribute (category="children").

13 XML Attributes XML elements can have attributes, just like HTML.
Attributes are designed to contain data related to a specific element. XML Attributes Must be Quoted Either single or double quotes can be used. <person gender="female"> or <person gender='female'> XML Elements vs. Attributes <person>   <gender>female</gender>   <firstname>Anna</firstname>   <lastname>Smith</lastname> </person> <person gender="female">   <firstname>Anna</firstname>   <lastname>Smith</lastname> </person> Avoid XML Attributes? Some things to consider when 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)

14 XML Namespaces XML Namespaces provide a method to avoid element name conflicts. This XML carries HTML table information: This XML carries information about a table(furniture) <table>   <tr>     <td>Apples</td>     <td>Bananas</td>   </tr> </table> <table>   <name>African Coffee Table</name>   <width>80</width>   <length>120</length> </table> Solving the Name Conflict Using a Prefix <h:table>   <h:tr>     <h:td>Apples</h:td>     <h:td>Bananas</h:td>   </h:tr> </h:table> <f:table>   <f:name>African Coffee Table</f:name>   <f:width>80</f:width>   <f:length>120</f:length> </f:table>

15 XML Namespaces - The xmlns Attribute
When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI". <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>

16 Namespaces in Real Use Uniform Resource Identifier (URI)
A Uniform Resource Identifier (URI) is a string of characters which identifies an Internet Resource. The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address Namespaces in Real Use XSLT is a language that can be used to transform XML documents into other formats. The XML document next slide, is a document used to transform XML into HTML. The namespace " identifies XSLT elements inside an HTML document:

17 <. xml version="1. 0" encoding="UTF-8"
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/"> <html> <body>   <h2>My CD Collection</h2>   <table border="1">     <tr>       <th style="text-align:left">Title</th>       <th style="text-align:left">Artist</th>     </tr>     <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>   </table> </body> </html> </xsl:template> </xsl:stylesheet>

18 XML Files with CSS <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/css" href="cd_catalog.css"?> <CATALOG>   <CD>     <TITLE>Empire Burlesque</TITLE>     <ARTIST>Bob Dylan</ARTIST>     <COUNTRY>USA</COUNTRY>     <COMPANY>Columbia</COMPANY>     <PRICE>10.90</PRICE>     <YEAR>1985</YEAR>   </CD> </CATALOG>

19 Document Type Declaration
XML Document Type Declaration

20 XML - DTDs way to describe XML language precisely
DTDs check vocabulary and validity of the structure of XML documents against grammatical rules of appropriate XML language. An XML DTD can be either specified inside the document, or it can be kept in a separate document and then liked separately. Syntax The DTD starts with <!DOCTYPE delimiter. An element tells the parser to parse the document from the specified root element. DTD identifier is an identifier for the document type definition, which may be the path to a file on the system or URL to a file on the internet. If the DTD is pointing to external path, it is called External Subset. The square brackets [ ] enclose an optional list of entity declarations called Internal Subset. <!DOCTYPE element DTD identifier [ declaration1 declaration2 ]>

21 Rules The document type declaration must appear at the start of the document (preceded only by the XML header) — it is not permitted anywhere else within the document. Similar to the DOCTYPE declaration, the element declarations must start with an exclamation mark. The Name in the document type declaration must match the element type of the root element.

22 #PCDATA means parse-able text data
Internal DTD elements are declared within the XML files. To refer it as internal DTD, standalone attribute in XML declaration must be set to yes. This means, the declaration works independent of external source. <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <!DOCTYPE address [ <!ELEMENT address (name,company,phone)> <!ELEMENT name (#PCDATA)> <!ELEMENT company (#PCDATA)> <!ELEMENT phone (#PCDATA)> ]> <address> <name>Tanmay Patil</name> <company>TutorialsPoint</company> <phone>(011) </phone> </address> #PCDATA means parse-able text data

23 External DTD declared outside the XML file.
They are accessed by specifying the system attributes which may be either the legal .dtd file or a valid URL. To refer it as external DTD, standalone attribute in the XML declaration must be set as no. This means, declaration includes information from the external source. address.dtd  <?xml version="1.0" encoding="UTF-8" standalone="no" ?> <!DOCTYPE address SYSTEM "address.dtd"> <address> <name>Tanmay Patil</name> <company>TutorialsPoint</company> <phone>(011) </phone> </address> <!ELEMENT address (name,company,phone)> <!ELEMENT name (#PCDATA)> <!ELEMENT company (#PCDATA)> <!ELEMENT phone (#PCDATA)>

24 XML Schemas

25 XML - Schemas XML Schema Definition (XSD).
It is used to describe and validate the structure and the content of XML data. XML schema defines the elements, attributes and data types. Schema element supports Namespaces. It is similar to a database schema that describes the data in a database. An XML Schema describes the structure of an XML document, just like a DTD. An XML document with correct syntax is called "Well Formed". An XML document validated against an XML Schema is both "Well Formed" and "Valid". XML Schema is an XML-based alternative to DTD

26 <xs:element name="note"> <xs:complexType>   <xs:sequence>     <xs:element name="to" type="xs:string"/>     <xs:element name="from" type="xs:string"/>     <xs:element name="heading" type="xs:string"/>     <xs:element name="body" type="xs:string"/>   </xs:sequence> </xs:complexType> </xs:element> The Schema above is interpreted like this: <xs:element name="note"> defines the element called "note" <xs:complexType> the "note" element is a complex type <xs:sequence> the complex type is a sequence of elements <xs:element name="to" type="xs:string"> the element "to" is of type string (text) <xs:element name="from" type="xs:string"> the element "from" is of type string <xs:element name="heading" type="xs:string"> the element "heading" is of type string <xs:element name="body" type="xs:string"> the element "body" is of type string

27 XML Schemas are More Powerful than DTD
XML Schemas are written in XML XML Schemas are extensible to additions XML Schemas support data types XML Schemas support namespaces Why Use an XML Schema? With XML Schema, your XML files can carry a description of its own format. With XML Schema, independent groups of people can agree on a standard for interchanging data. With XML Schema, you can verify data.

28 XML DOM

29 DOM  standard for accessing and manipulating documents XML documents. It presents an XML document as a tree-structure. The HTML DOM <h1 id="demo">This is a Heading</h1> <script> document.getElementById("demo").innerHTML = "Hello World!"; </script>

30 The XML DOM txt = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue; xmlDoc - the XML DOM object created by the parser. getElementsByTagName("title")[0] - get the first <title> element childNodes[0] - the first child of the <title> element (the text node) nodeValue - the value of the node (the text itself)

31 <html> <body> <p id="demo"></p> <script> var text, parser, xmlDoc; text = "<bookstore><book>" + "<title>Everyday Italian</title>" + "<author>Giada De Laurentiis</author>" + "<year>2005</year>" + "</book></bookstore>"; parser = new DOMParser(); xmlDoc = parser.parseFromString(text,"text/xml"); document.getElementById("demo").innerHTML = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue; </script> </body> </html>

32 XML Parser and Validation

33 XML parser It is a software library or a package that provides interface for client applications to work with XML documents. It checks for proper format of the XML document and may also validate the XML documents. Modern day browsers have built-in XML parsers. The goal of a parser is to transform XML into a readable code

34 XML - Validation Use our XML validator to syntax-check your XML.
Validation is a process by which an XML document is validated. An XML document is said to be valid if its contents match with the elements, attributes and associated document type declaration(DTD), and if the document complies with the constraints expressed in it. Validation is dealt in two ways by the XML parser. They are: Well-formed XML document Valid XML document

35 Well Formed XML Documents
An XML document with correct syntax is called "Well Formed". XML documents must have a root element XML elements must have a closing tag XML tags are case sensitive XML elements must be properly nested XML attribute values must be quoted Valid XML Documents A "well formed" XML document is not the same as a "valid" XML document. A "valid" XML document must be well formed. In addition, it must conform to a document type definition. There are two different document type definitions that can be used with XML: DTD - The original Document Type Definition XML Schema - An XML-based alternative to DTD A document type definition defines the rules and the legal elements and attributes for an XML document.

36 XSL and XSLT Transformation

37 XSL - XSLT XSL (EXtensible Stylesheet Language) is a styling language for XML. XSLT stands for XSL Transformations. The World Wide Web Consortium (W3C) started to develop XSL because there was a need for an XML-based Stylesheet Language. CSS = Style Sheets for HTML XSL = Style Sheets for XML XSL consists of four parts: XSLT - a language for transforming XML documents XPath - a language for navigating in XML documents XSL-FO - a language for formatting XML documents (discontinued in 2013) XQuery - a language for querying XML documents

38 XSLT XSLT stands for XSL Transformations
XSLT is the most important part of XSL XSLT transforms an XML document into another XML document XSLT uses XPath to navigate in XML documents XSLT is a W3C Recommendation With XSLT you can add/remove elements and attributes to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more.

39 XSLT Transform The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>. <xsl:stylesheet version="1.0" xmlns:xsl=" XML Document <?xml version="1.0" encoding="UTF-8"?> <catalog>   <cd>     <title>Empire Burlesque</title>     <artist>Bob Dylan</artist>     <country>USA</country>     <company>Columbia</company>     <price>10.90</price>     <year>1985</year>   </cd> </catalog>

40 Create an XSL Style Sheet
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th>Title</th>       <th>Artist</th>     </tr>     <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>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet> Create an XSL Style Sheet cdcatalog.xsl

41 Link the XSL Style Sheet to the XML Document
INCLUDE XSL FILE HERE <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?> <catalog>   <cd>     <title>Empire Burlesque</title>     <artist>Bob Dylan</artist>     <country>USA</country>     <company>Columbia</company>     <price>10.90</price>     <year>1985</year>   </cd> . . </catalog>

42 <xsl:template match="/">
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.  <xsl:template> element is used to build templates. match attribute is used to associate a template with an XML element value of the match attribute is an XPath expression (i.e. match="/" defines the whole document) <xsl:template match="/">

43 <xsl:value-of>
used to extract the value of a selected node. used to extract the value of an XML element and add it to the output stream of the transformation <xsl:value-of select="catalog/cd/title"/> <xsl:for-each> allows you to do looping in XSLT used to select every XML element of a specified node-set <xsl:for-each select="catalog/cd"> <xsl:for-each select="catalog/cd[artist='Bob']">

44 XML NEWSFEED RSS ATOM

45 XML RSS With RSS it is possible to distribute up-to-date web content from one web site to thousands of other web sites around the world. RSS allows fast browsing for news and updates. RSS stands for Really Simple Syndication RSS allows you to syndicate your site content RSS defines an easy way to share and view headlines and content RSS files can be automatically updated RSS allows personalized views for different sites RSS is written in XML

46 Why use RSS? RSS was designed to show selected data. Without RSS, users will have to check your site daily for new updates. This may be too time-consuming for many users. With an RSS feed (RSS is often called a News feed or RSS feed) they can check your site faster using an RSS aggregator (a site or program that gathers and sorts out RSS feeds). Since RSS data is small and fast-loading, it can easily be used with services like cell phones or PDA's. Web-rings with similar information can easily share data on their web sites to make them better and more useful. Who Should use RSS? Webmasters who seldom update their web sites do not need RSS! RSS is useful for web sites that are updated frequently, like: News sites - Lists news with title, date and descriptions Companies - Lists news and new products Calendars - Lists upcoming events and important days Site changes - Lists changed pages or new pages

47 How RSS Works RSS is used to share content between websites. With RSS, you register your content with companies called aggregators. So, to be a part of it: First, create an RSS document and save it with an .xml extension. Then, upload the file to your website. Next, register with an RSS aggregator. Each day the aggregator searches the registered websites for RSS documents, verifies the link, and displays information about the feed so clients can link to documents that interests them.

48 <. xml version="1. 0" encoding="UTF-8". > <rss version="2
<?xml version="1.0" encoding="UTF-8" ?> <rss version="2.0"> <channel>   <title>W3Schools Home Page</title>   <link>   <description>Free web building tutorials</description>   <item>     <title>RSS Tutorial</title>     <link>     <description>New RSS tutorial on W3Schools</description>   </item>   <item>     <title>XML Tutorial</title>     <link>     <description>New XML tutorial on W3Schools</description>   </item> </channel> </rss>

49 XML ATOM Atom is an XML -based file format used to syndicate content
Atom was designed to be a universal publishing standard for blogs and other Web sites where content is updated frequently. Atom is differentiated from RSS in several ways: Atom was developed to be vendor neutral and freely extensible by any user; it is an "open standard." Atom lies within an XML-namespace. Atom includes "autodiscovery," allowing feed aggregators to automatically detect the presence of a feed. Atom differentiates between relative and non-relative URIs. Atom has separate "summary" and "content" elements. Atom explicitly labels a payload as plaintext or HTML . Each entry has a globally unique ID.


Download ppt "XML BASICS."

Similar presentations


Ads by Google