EXtensible Markup Language (XML) - Part 2

XLink is used to create hyperlinks within XML documents Any element in an XML document can behave as a link XLink supports simple links (like HTML) and extended links (for linking multiple resources together) With XLink, the links can be defined outside the linked files XLink is a W3C Recommendation

ppt45 trang | Chia sẻ: huongnt365 | Lượt xem: 515 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu EXtensible Markup Language (XML) - Part 2, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
CSC 330 E-CommerceTeacher Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad Virtual Campus, CIIT COMSATS Institute of Information TechnologyT2-Lecture-4eXtensible Markup Language (XML)Part - IIFor Lecture Material/Slides Thanks to: www.w3schools.comObjectivesXML Elements are extensibleXML AttributesXML NamespacesXML EncodingViewing XML Files 3T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML Elements are Extensible4XML elements can be extended to carry more information.Look at the following XML example: mumtaz tariq Don't forget to attend party on weekend! Let's imagine that we created an application that extracted the , , and elements from the XML document to produce this output:T2-Lecture-3 Ahmed Mumtaz Mustehsan www.w3schools.comMessageTo: MumtazFrom: TariqDon't forget to attend party on weekend!XML Elements are Extensible5Imagine that the author of the XML document added some extra information to it: 2008-01-10 Mumtaz TariqReminder Don't forget to attend party on weekend! Should the application break or crash?No. The application should still be able to find the , , and elements in the XML document and produce the same output.One of the beauty of XML, is that it can be extended without breaking applications.T2-Lecture-3 Ahmed Mumtaz Mustehsan www.w3schools.comMessageTo: MumtazFrom: TariqDon't forget to attend party on weekend!XML AttributesXML Attributes7In HTML, attributes provide additional information about elements:XML elements can have attributes, just like HTML.Attributes often provide information that is not a part of the data but it is information about data.Examplecomputer.gifThe file type is irrelevant to the data, but can be important to the software that wants to manipulate the element:T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML Attributes Must be Quoted8Attribute values must always be quoted. Either single or double quotes can be used. Example: For a person's sex, the person element can be written like this:or like this:T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML Attributes Must be Quoted9If the attribute value itself contains double quotes we can use single quotes.Example:or you can use character entities:T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML Elements vs. Attributes10Example-1:   Amna   Atif Example-2:   female   Amna   Atif T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML Elements vs. Attributes11In Example-1, sex is an attribute. In Example-2, sex is an element. Both examples provide the same information.Although there are no rules about when to use attributes or when to use elements, Attributes are handy in HTML. However:Recommendations are:In XML avoid attribute and use elements.T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML: Different ways to define information. 12In XML documents the same information can be presented in different ways.The date is presented in three different ways:Example-1:A date is defined as an attribute:   Mumtaz   Tariq   Reminder   Don't forget me this weekend! T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML: Different ways to define information.13Example-2:A date is defined as an element :   10/01/2008   Mumtaz   Tariq   Reminder   Don't forget me this weekend! T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML: Different ways to define information.14Example-3:A date is defined as an expanded date element:       27     05     2009     Mumtaz   Tariq   Reminder   Don't forget me this weekend! T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comAvoid XML Attributes?15Some 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. Recommendations:Use elements for data. Use attributes for information that is not relevant to the data.Don't end up like this: T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML Attributes for Metadata16Sometimes ID references are assigned to elements.These IDs can be used to identify XML elements the same way as the id attribute in HTML. Example:       mumtaz     tariq     Reminder     Don't forget me this weekend!         mumtaz     tariq     Re: Reminder     Thank you for attending the party   T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML Attributes for Metadata17The id attributes above are for identifying the different notes. It is not a part of the note itself.Recommendations:Metadata (data about data) should be stored as attributes, and the data itself should be stored as elements.T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML NamespacesBefore we Begin: a review of URI19Uniform 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. Another, not so common type of URI is the Universal Resource Name (URN).In our examples we will only use URLs.T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML Namespaces20In XML, element names are defined by the developer. This often results in a conflict when trying to mix XML documents from different XML applications.XML Namespaces provide a method to avoid element name conflicts.Example-1:This XML example carries HTML table information:       Apples     Bananas   T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML Namespaces21Example-2:This XML example carries information about a table (a piece of furniture):   African Coffee Table   80   120 If these XML fragments were added together, there would be a name conflict. Both contain a element, but the elements have different content and meaning.A user or an XML application will not know how to handle these differences.T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comSolving the Name Conflict Using a Prefix22Name conflicts in XML can easily be avoided using a name prefix.This XML carries information about an HTML table, and a piece of furniture:       Apples     Bananas     African Coffee Table   80   120 In the example above, there will be no conflict because the two elements have different names. But are we allowed to use prefix with a : ?T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML Namespaces : The xmlns Attribute23When using prefixes in XML 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:Example xmlns:prefix="URI". T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML Namespaces - The xmlns Attribute24The namespace declaration as an attribute can be defined:Example-1: each attribute is defined with separate xmsns        Apples     Bananas      African Coffee Table   80   120 T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML Namespaces - The xmlns Attribute25The namespace declaration as an attribute can be defined:Example-1: each attribute is defined with combined xmsns defination        Apples     Bananas      African Coffee Table   80   120 Note: The namespace URI is not used by the parser to look up information.The purpose is to give the namespace a unique name. However, often companies use the namespace as a pointer to a web page containing namespace information.T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comDefault Namespaces26Defining a default namespace for an element saves us from using prefixes in all the child elements. It has the following syntax:xmlns="namespaceURI"Example:This XML carries HTML table information:       Apples     Bananas   This XML carries information about a piece of furniture:   African Coffee Table   80   120 T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML EncodingXML Encoding28XML documents may contain international characters, like Norwegian æøå, or French êèé.To avoid errors, we should specify the encoding used, or save the XML files as UTF-8.Character EncodingCharacter encoding defines a unique binary code for each different character used in a document.In computer terms, character encoding are also called character set, character map, code set, and code page.T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comUnicode29Unicode is an industry standard for character encoding of text documents. It defines (nearly) every possible international character by a name and a number.Unicode has two variants: UTF-8 and UTF-16.UTF = Universal character set Transformation Format.UTF-8 uses a single byte (8-bits) to represent commonly used characters and two (or three) bytes for the rest.UTF-16 uses two bytes (16 bits) for most characters, and three bytes for the rest.T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comUTF-8 - The Web Standard30UTF-8 is the standard character encoding on the web.It (UTF-8) is the default character encoding for: HTML-5, CSS, JavaScript, PHP, SQL, and XML.T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML Encoding31The first line in an XML document is called the prolog:The prolog is optional. Normally it contains the XML version number.It can also contain information about the encoding used in the document. This prolog specifies UTF-8 encoding:The XML standard states that all XML software must understand both UTF-8 and UTF-16.UTF-8 is the default for documents without encoding information.In addition, most XML software systems understand encodings like ISO-8859-1, Windows-1252, and ASCII.T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comXML Errors32Most often, XML documents are created on one computer. uploaded to a server on a second computer, displayed by a browser on a third computer.If the encoding is not correctly interpreted by all the three computers, the browser might display meaningless text, or you might get an error message.Example:“This XML file does not appear to have any style information associated with it.”T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comConclusion33When you write an XML document:Use an XML editor that supports encodingMake sure you know what encoding the editor usesDescribe the encoding in the encoding attributeUTF-8 is the safest encoding to use which is a web standard T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comViewing XML FilesViewing XML Files35Raw XML files can be viewed in all major browsers.Don't expect XML files to be displayed as HTML pages.  -         mumtaz        tariq        Reminder        Don't forget me this weekend!   The XML document will be displayed with color-coded root and child elements. A plus (+) or minus sign (-) to the left of the elements can be clicked to expand or collapse the element structure. To view the raw XML source (without the + and - signs), select "View Page Source" or "View Source" from the browser menu.Note: In Safari, only the element text will be displayed. To view the raw XML, you must right click the page and select "View Source" T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comViewing an Invalid XML File36If an erroneous XML file is opened, some browsers might report the error, some may display it incorrectly.Try to open this XML file in different browsers:T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comWhy Does XML Display Like This?37XML documents do not carry information about how to display the data.Since XML tags are "invented" by the author of the XML document, browsers do not know if a tag like describes an HTML table or a dining table.Without any information about how to display the data, most browsers will just display the XML document as it is.T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.comThe second line links the XML file to the CSS file:       Empire Burlesque     Bob Dylan     USA     Columbia     10.90     1985         Hide your heart     Bonnie Tyler     UK     CBS Records     9.90     1988   . . . A Section of the XML file. T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.com38It is possible to use CSS to format an XML document.Below is an example of how to use a CSS style sheet to format an XML document:Take a look at this XML file: The CD catalogThen look at this style sheet: The CSS fileFinally, view: The CD catalog formatted with the CSS fileDisplaying your XML Files with CSST2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.com39XML TechnologiesT2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.com40Some XML Technologies:XML XSLTXML XpathXML XlinkXML XpointerXML XqueryRecommended by W3CDisplaying XML with XSLTXSLT (eXtensible Stylesheet Language Transformations) is the recommended style sheet language for XML.XSLT is far more sophisticated than CSS. Elements and attributes can be added/removed Elements can also be rearranged and sorted, perform tests and make decisions about which elements to hide and display, etc.XSLT uses XPath to find information in an XML document.XML - XSLTT2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.com41XPath is Used in XSLTXPath is a syntax for defining parts of an XML documentXPath uses path expressions to navigate in XML documentsXPath contains a library of standard functionsXPath is a major element in XSLTXPath is also used in XQuery, XPointer and XLinkXML - XpathT2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.com42XLink is used to create hyperlinks within XML documentsAny element in an XML document can behave as a linkXLink supports simple links (like HTML) and extended links (for linking multiple resources together)With XLink, the links can be defined outside the linked filesXLink is a W3C RecommendationXML - XLinkT2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.com43XPointer allows the links to point to specific parts of an XML documentXPointer uses XPath expressions to navigate in the XML documentXPointer is a W3C RecommendationXML - XPointerT2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.com44The End XML Part-IIT2-Lecture-4Thank You45T2-Lecture-4 Ahmed Mumtaz Mustehsan www.w3schools.com

Các file đính kèm theo tài liệu này:

  • pptt2_lecture_04_8262_2027093.ppt
Tài liệu liên quan