Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is XML? XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry data, not to display data XML.

Similar presentations


Presentation on theme: "What is XML? XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry data, not to display data XML."— Presentation transcript:

1 What is XML? XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry 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

2 XML is not a replacement for HTML. XML and HTML were designed with different goals: –XML was designed to transport and store data, with focus on what data is. –HTML was designed to display data, with focus on how data looks.

3 XML does not DO anything. XML was created to structure, store, and transport information. But still, this XML document does not DO anything. It is just pure information wrapped in tags.

4 Example xml1.xml 5 points for someone Chetan B. India

5 XML trees An xml document is a hierarchical structure called an xml tree which consist of various kinds arranged as a tree.

6 CATLOG BOOK TITLEAUTHORCOUNTRYPRICEYEAR

7 An XML document contains XML Elements. Harry Potter J K. Rowling Learning XML Erik T. Ray

8 XML (tag) Naming Rules XML elements must follow these 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.

9 Best Naming Practices Make names descriptive. Names with an underscore separator are nice:,. Names should be short and simple, like this: not like this:. Avoid "-" characters. If you name something "first-name," some software may think you want to subtract name from first. Avoid "." characters. If you name something "first.name," some software may think that "name" is a property of the object "first.“ Avoid ":" characters. Colons are reserved to be used for something called namespaces (more later). XML documents often have a corresponding database. A good practice is to use the naming rules of your database for the elements in the XML documents. Non-English letters like éòá are perfectly legal in XML, but watch out for problems if your software vendor doesn't support them.

10 XML Elements are Extensible Harry Potter J K. Rowling st Learning XML Erik T. Ray

11 Xml syntax All tags tag should have closing tag. All tags are case sensitive. Xml elements must be properly nested Xml document must have root element is must. Attributes must be in double quotes.

12 Entity References Some characters have a special meaning in XML. If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element.

13 This will generate an XML error: if salary To avoid this error, replace the "<" character with an entity reference: if salary < 1000 then

14 There are 5 predefined entity references in XML: < greater than & ampersand ' 'apostrophe " "quotation mark

15 Comments in XML The syntax for writing comments in XML is similar to that of HTML. White-space is Preserved in XML HTML truncates multiple white-space characters to one single white-space: HTML: Hello my name is Tove Output: Hello my name is Tove.

16 Xml elements attributes XML elements can have attributes in the start tag, just like HTML. Attributes provide additional information about elements.

17

18 or like this: If the attribute value itself contains double quotes you can use single quotes, like in this example: Or you can use character entities:

19 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.

20 Id Tove Jani Reminder Don't forget me this weekend! Jani Tove Re: Reminder I will not

21 Xml and css With css you can add display information to xml. Eg Cd_catlog.css Cd_catlog.xml Cd_catlog1.xml

22 Other way is XSLT

23 Xsl : extensible stylesheet language. Xslt : xsl transform. (language for xml transformation ) XSLT is a language for transforming XML documents into XHTML documents or to other XML documents.

24 CSS = Style Sheets for HTML –HTML uses predefined tags, and the meaning of each tag is well understood. –The tag in HTML defines a table - and a browser knows how to display it. –Adding styles to HTML elements are simple. Telling a browser to display an element in a special font or color, is easy with CSS. XSL = Style Sheets for XML –XML does not use predefined tags (we can use any tag-names we like), and therefore the meaning of each tag is not well understood. –A tag could mean an HTML table, a piece of furniture, or something else - and a browser does not know how to display it. –XSL describes how the XML document should be displayed!

25 XSLT = XSL Transformations XSLT is the most important part of XSL. XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XML element into an (X)HTML element. 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. A common way to describe the transformation process is to say that XSLT transforms an XML source-tree into an XML result-tree.

26 Correct Style Sheet Declaration The root element that declares the document to be an XSL style sheet is or.

27 XSLT Element 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

28 The element is used to build templates. The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).

29 My CD Collection Title Artist..

30 XSLT Element The element is used to extract the value of a selected node.

31 Book.xml Empire Burlesque Bob Dylan USA Columbia

32 My Collection Title author

33 The Element The XSL element can be used to select every XML element of a specified node-set:

34 My CD Collection Title Artist

35 The Element To put a conditional if test against the content of the XML file, add an element to the XSL document. Syntax...some output if the expression is true...

36 My CD Collection Title Artist

37

38 The element is used to sort the output.

39 My CD Collection Title Artist

40

41 A JavaScript Solution We have explained how XSLT can be used to transform a document from XML to XHTML. We did this by adding an XSL style sheet to the XML file and let the browser do the transformation. Even if this works fine, it is not always desirable to include a style sheet reference in an XML file (e.g. it will not work in a non XSLT aware browser.) A more versatile solution would be to use a JavaScript to do the transformation. By using a JavaScript, we can: do browser-specific testing use different style sheets according to browser and user needs

42 function loadXMLDoc(dname) { if (window.XMLHttpRequest) { xhttp=new XMLHttpRequest(); } else { xhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.open("GET",dname,false); xhttp.send(""); return xhttp.responseXML; } xml=loadXMLDoc("cd_catalog.xml"); path="/CATALOG/CD[PRICE > 9]/TITLE" // code for IE if (window.ActiveXObject) {var nodes=xml.selectNodes(path); for (i=0;i

43 // code for Mozilla, Firefox, Opera, etc. else if (document.implementation && document.implementation.createDocument) { var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null); var result=nodes.iterateNext(); while (result) { document.write(result.childNodes[0].nodeValue); document.write(" "); result=nodes.iterateNext(); }

44 Try2.html Try3.html

45 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses path expressions to navigate in XML documents XPath contains a library of standard functions XPath is a major element in XSLT XPath is a W3C recommendation

46 XPATH terminologies Parent Children Siblings Ancestors Descendants

47 XPATH Loading the XML Document Using XMLHttpRequest to load XML documents is supported in all modern browsers. Code for most modern browsers: var xmlhttp=new XMLHttpRequest() Code for old Microsoft browsers (IE 5 and 6): var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")

48 Selecting Nodes Internet Explorer uses the selectNodes() method to select nodes from the XML document: xmlDoc.selectNodes(xpath); Firefox, Chrome, Opera and Safari use the evaluate() method to select nodes from the XML document: xmlDoc.evaluate(xpath, xmlDoc, null, XPathResult.ANY_TYPE,null); var xpathResult = document.evaluate( xpathExpression, contextNode, namespaceResolver, resultType, result );

49 xpathExpression: A string containing the XPath expression to be evaluated. contextNode: A node in the document against which the xpathExpression should be evaluated, including any and all of its child nodes. The document node is the most commonly used. namespaceResolver: A function that will be passed any namespace prefixes contained within xpathExpression which returns a string representing the namespace URI associated with that prefix. This enables conversion between the prefixes used in the XPath expressions and the possibly different prefixes used in the document. The function can be either: –null, which can be used for HTML documents or when no namespace prefixes are used. Note that, if the xpathExpression contains a namespace prefix, this will result in a DOMException being thrown with the code NAMESPACE_ERR. –A custom user-defined function. See the Using a User Defined Namespace Resolver section in the appendix for details. resultType: A constant that specifies the desired result type to be returned as a result of the evaluation. The most commonly passed constant is XPathResult.ANY_TYPE which will return the results of the XPath expression as the most natural type. There is a section in the appendix which contains a full list of the available constants. They are explained below in the section "Specifying the Return Type." result: If an existing XPathResult object is specified, it will be reused to return the results. Specifying null will create a new XPathResult object.

50 The following example selects all the title nodes: Example /bookstore/book/title Select the title of the first book The following example selects the title of the first book node under the bookstore element: Example /bookstore/book[1]/title Select title nodes with price>35 The following example selects all the title nodes with a price higher than 35: Example /bookstore/book[price>35]/title Try4.html

51 Expressions Path ExpressionResult bookstoreSelects all the child nodes of the bookstore element /bookstoreSelects the root element bookstore Note: If the path starts with a slash ( / ) it always represents an absolute path to an element! bookstore/bookSelects all book elements that are children of bookstore //bookSelects all book elements no matter where they are in the document bookstore//bookSelects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element all attributes that are named lang

52 Predicates –Finds exact node

53 Path ExpressionResult /bookstore/book[1]Selects the first book element that is the child of the bookstore element. Note: IE5 and later has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!! /bookstore/book[last()]Selects the last book element that is the child of the bookstore element /bookstore/book[last()-1]Selects the last but one book element that is the child of the bookstore element /bookstore/book[position()<3]Selects the first two book elements that are children of the bookstore element all the title elements that have an attribute named lang all the title elements that have an attribute named lang with a value of 'eng' /bookstore/book[price>35.00]Selects all the book elements of the bookstore element that have a price element with a value greater than /bookstore/book[price>35.00]/titleSelects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00

54 Wild card characters Path ExpressionResult /bookstore/*Selects all the child nodes of the bookstore element //*Selects all elements in the document all title elements which have any attribute

55 Selecting several paths Path ExpressionResult //book/title | //book/priceSelects all the title AND price elements of all book elements //title | //priceSelects all the title AND price elements in the document /bookstore/book/title | //priceSelects all the title elements of the book element of the bookstore element AND all the price elements in the document

56 SAX - Simple API for XML Parsing Being the first in the evolution ladder, it obviously had only the basic support for XML processing. It is an event-based technology, which uses callbacks to load the parts of the XML document in a sequential way. This effectively means you can't go back to some part which was read/processed previously - if you do have such a requirement then you would need to store/manage the relevant data yourself. Since this API does require to load the entire XML doc and also because it offers only a sequential processing of the doc hence it is quite fast. Another reason of it being faster is that it does not allow modification of the underlying XML data.

57 DOM - Document Object Model The Java binding for DOM provided a tree-based representation of the XML documents - allowing random access and modification of the underlying XML data. Not very difficult to deduce that it would be slower as compared to SAX. The event-based callback methodology was replaced by an object- oriented in-memory representation of the XML documents.

58 SAXDOM Both SAX and DOM are used to parse the XML document. Both has advantages and disadvantages and can be used in our programming depending on the situation. Parses node by node Stores the entire XML document into memory before processing Doesn’t store the XML in memoryOccupies more memory We cant insert or delete a nodeWe can insert or delete nodes Top to bottom traversingTraverse in any direction. SAX is an event based parserDOM is a tree model parser SAX is a Simple API for XMLDocument Object Model (DOM) API import javax.xml.parsers.*; import org.xml.sax.*; import org.xml.sax.helpers.*; import javax.xml.parsers.*; import org.w3c.dom.*; doesn’t preserve commentspreserves comments SAX generally runs a little faster than DOM If we need to find a node and doesn’t need to insert or delete we can go with SAX itself otherwise DOM provided we have more memory.


Download ppt "What is XML? XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry data, not to display data XML."

Similar presentations


Ads by Google