Presentation is loading. Please wait.

Presentation is loading. Please wait.

IDK0040 Võrgurakendused I XML Deniss Kumlander. XML intro XML stands for EXtensible Markup Language XML is a markup language much like HTML and was invented.

Similar presentations


Presentation on theme: "IDK0040 Võrgurakendused I XML Deniss Kumlander. XML intro XML stands for EXtensible Markup Language XML is a markup language much like HTML and was invented."— Presentation transcript:

1 IDK0040 Võrgurakendused I XML Deniss Kumlander

2 XML intro XML stands for EXtensible Markup Language XML is a markup language much like HTML and was invented to describe data XML tags are not predefined, so developers can define own tags. XML uses either a Document Type Definition (DTD) or an XML Schema to describe the structure of the documents’ tags and restrictions XML is a W3C Recommendation

3 Use Exchange data Store data Make it platform independent, i.e. Have a broader “client”

4 Syntax..........

5 Example IDK0040 TTU Reminder Don't forget to be at lectures! IDK0040 TTU Reminder 2 Exams are close!

6 Attributes IDK0040 TTU Reminder Don't forget to be at lectures!

7 Avoid using attributes? Should we avoid using attributes? Some of the problems with using attributes are: attributes cannot contain multiple values (child elements can) attributes are not easily expandable (for future changes) attributes cannot describe structures (child elements can) attributes are more difficult to manipulate by program code attribute values are not easy to test against a Document Type Definition (DTD) - which is used to define the legal elements of an XML document

8 Initial validation 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 always be quoted

9 XML and CSS Empire Burlesque Bob Dylan USA Columbia 10.90 1985 Hide your heart Bonnie Tyler UK CBS Records 9.90 1988 CATALOG { background-color: #ffffff; width: 100%; } CD { display: block; margin-bottom: 30pt; margin-left: 0; } TITLE { color: #FF0000; font-size: 20pt; } ARTIST { color: #0000FF; font-size: 20pt; } COUNTRY,PRICE,YEAR,COMPANY { display: block; color: #000000; margin-left: 20pt; }

10 XML Data Embedded in HTML (“XML Data Island”) IE only Tove Jani Reminder Don't forget me this weekend!...... Just inform browser (i.e. Link xml) – the actual use is later

11 XML Namespaces Since element names in XML are not predefined, a name conflict will occur when two different documents use the same element names or tags are the same as for HTML

12 XML Namespaces Work Desk 700 1200 Where f should mean a “furniture” to differenciate from something else

13 XML Namespaces African Coffee Table 80 120 African Coffee Table 80 120 Instead of using only prefixes, we have added an xmlns attribute to the tag to give the prefix a qualified name associated with a namespace. When a namespace is defined in the start tag of an element, all child elements with the same prefix are associated with the same namespace. Note that the address used to identify the namespace is not used by the parser to look up information. The only purpose is to give the namespace a unique name. However, very often companies use the namespace as a pointer to a real Web page containing information about the namespace.

14 XML schema description DTD – Document type definition XML Schemas - an XML-based alternative to DTD.

15 DTD If the DTD is included in your XML source file, it should be wrapped in a DOCTYPE definition with the following syntax: internal external

16 Internal DTD Example <!DOCTYPE note [ ]> Tove Jani Reminder Don't forget me this weekend

17 Why use a DTD? With DTD, each of your XML files can carry a description of its own format with it. With a DTD, independent groups of people can agree to use a common DTD for interchanging data. Your application can use a standard DTD to verify that the data you receive from the outside world is valid. You can also use a DTD to verify your own data.

18 DTD: The building blocks Elements - Elements are the main building blocks of both XML and HTML documents, i.e. tags Attributes - Attributes provide extra information about elements. Entities - Entities are variables used to define common text. Entity references are references to entities. Most of you will know the HTML entity reference: " “ PCDATA - PCDATA means parsed character data. CDATA - CDATA also means character data. CDATA is text that will NOT be parsed by a parser. Tags inside the text will NOT be treated as markup and entities will not be expanded.

19 DTD: Elements Declared or Empty element: Character data: Doesn’t contain any content – for example see a html tag called br

20 DTD: Elements With children: or example: When children are declared in a sequence separated by commas, the children must appear in the same sequence in the document. In a full declaration, the children must also be declared, and the children can also have children.

21 DTD: Element Only one occurrence (must occur and only once): – Minimum one occurrence (can be more than 1) – Zero or more occurrences – Zero or one – Either one or another: –

22 DTD: Attributes Declaration – Attribute-type can be: Default-value can be

23 DTD: Entity Entity can be seen as a defined constant Syntax: – DTD Example: –Define –Use &writer; &copyright;

24 XML Schemas: XSD Another, modern way to describe xml structure Why instead of DTD: –XML Schemas are extensible to future additions –XML Schemas are richer and more powerful than DTDs –XML Schemas are written in XML –XML Schemas support data types –XML Schemas support namespaces

25 XSD: Example Example <xs:schema xmlns:xs="http://.../XMLSchema" targetNamespace="http://..." xmlns=“...." elementFormDefault="qualified">

26 XSD: Simple elements A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes (but the text can be of any type!) Declaration – Build-in types: –xs:string –xs:decimal –xs:integer –xs:boolean –xs:date (YYYY-MM-DD +zone) –xs:time –xs:dateTime –xs:time –xs:hexBinary –xs:base64Binary –xs:anyURI Value definer

27 XSD: Simple element example XML: – Võhandu – 36 – 1974-01-02 XSD: –

28 XSD: Attributes Declaration – Note: simple elements cannot have attributes

29 XSD: Restrictions defines a value range for a number – – –

30 XSD: Restrictions (set) defines a value range for the string – – –

31 XSD: Restrictions pattern – – * : one or more + : at least one | : one or another {x} : exactly x elements (characters): ="[a-zA-Z0-9]{8}"

32 XSD: Restriction length – – –

33 XSD: Complex type There are 4 kinds of complex elements: –empty elements –elements that contain only other elements –elements that contain only text –elements that contain both other elements and text Note: complex elements may contain attributes.

34 Examples or xml to be described as complex types Empty element ( in the example the value is defined via an attribute ) – Element "employee“ that contains only other elements: – Deniss Kumlander Software Architect – Element module that contains only text: – Allocation

35 XSD: Complex element description example for an element containing others Declaration: direct – – » – – Declaration using a “type” – – – Means ordered occurance of elements Deniss Kumlander Software Architect

36 XSD: Complex element description example for the text only element – –........ – – or – –.... – – Using either an extension or a restriction

37 XSD: Complex element Means that can be something like: Dear Mr. Carlsson. Your order 123... in other words a mix of tags and text, where tags appear inside the text to give somekind extra information

38 XSD: Indicators Indicator do allow to control how elements are used –Declaration: –Order indicators sequence – child elements should occur in the specific order all – any order, but all child elements should occur at least once choice – either one or another element should occur –Occurance (part of the element declaration) minOccurance maxOccurance Example:

39 XSD: Extensions The element enables us to extend the XML document with elements not specified by the schema. The element enables us to extend the XML document with attributes not specified by the schema.

40 XSL XSL is an acronym for EXtensible Stylesheet Language. i.e. something like CSS for XML 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

41 XPath XPath is a syntax allowing navigating inside an XML document. It uses tree-like structure of XML documents and process nodes and parents. It is very similar to navigating in any folders (we have seen that in some css and html tag like href for example)

42 XPath nodes There are seven kinds of nodes: element, attribute, text, namespace, processing- instruction, comment, and document (root) nodes.

43 XPath relationship Parent: Each element and attribute has one parent. Deniss Kumlander Software Architect “employee” is a parent for the “firstname”, “lastname” and “position”

44 XPath relationship Children: Each element can have 0, 1 or many children. Deniss Kumlander Software Architect “firstname”, “lastname” and “position” are children for the “employee”

45 XPath relationship Siblings: are nodes having the same parent. Deniss Kumlander Software Architect “firstname”, “lastname” and “position” are siblings

46 XPath relationship Ancestors: nodes’ parent, parent's parent, etc.. Descendants: node’s children, children’s children, etc..

47 XPath expressions StatementExplanation nodenameSelects all child nodes of the node, for example “employee” select all children, i.e. lastname, firstname etc. /Selects from the root node //Selects nodes in the document from the current node that match the selection no matter where they are, for example “//lastname” selects all lastnames, wherever those are.Selects the current node..Selects the parent of the current node @Selects attributes personnel/employee selects all “employee”s that are children of “personnel”

48 XPath expressions

49 /personnel/employee[last()-1] //employee[@branch=‘CODA Eesti'] –Selects all employees where attribute branch of the employee is CODA Eesti /personnel/employee[salary>10000]/lastname –Selects personnel children employees with salary more than 10000 and returns only lastnames

50 XPath Notice that it was just a short introducation!!!

51 XSLT 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.

52 XSLT template It is possible to say that HTML (XHTML) is a style sheet for XML !

53 XSLT Deniss Kumlander Software Architect CODA Personnel Name Position coda.xsl

54 XML connected to XSLT “coda.xsl”XML document should contain the following string to be associated with a template, where “coda.xsl” is a user/defined name of the xslt file

55 XSLT file: template The element is used to build a template. The match attribute is used to associate a template with an XML element from the “source” file. The value of the match attribute is an XPath expression (note: match="/" connects to the whole document).

56 XSLT file: “for-each” The XSL element is used to select each XML element of a specified set of nodes. Notice that “select” is nothing else than an XPath defining the level to start selection from (elements to iterate). …..

57 XSLT file: “value-of” The element is used to get a value of an XML element and put it to the output stream. Notice that “select” is again an XPath defining an element to get

58 XSLT Deniss Kumlander Software Architect CODA Personnel Name Position coda.xsl

59 XSLT advance: filtering It is possible to filter the output from the XML file by adding a criterion to the select attribute in the element. Filter operators are: = (equal) != (not equal) < less than > greater than

60 XSLT advance: “sort” The element is used to sort output (as XML is ordered list of items, so it is a possibility to re-order items). It is added after tag appears. … “select” indicates element to sort

61 XSLT advance: “if” The element is used to put a conditional if test against the content of the XML file. It is added after the tag appears. … “salary > 20000”

62 XSLT advance: “choose” Elements, and are used similar to “if.. then …else” construction of major programming languages... an output...... an output....

63 XSLT advance: choose

64 XSLT advance: choose

65 XSLT advance: copy-of and variables property of CODA Lower salary employees High salary employees 100000"> Copies with children. There is also just a “copy” function that copy only the xml element without children

66 XSLT advance: apply-templates The element applies a template to the current element or to the current element's child nodes. The select attribute is used to define the order in which the child nodes are processed.

67 XSLT advance: apply-template Show each article title as an header 1...

68 XSLT advance: apply-template Deniss Kumlander Software Architect Veiko Laev Developer <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> CODA Personnel Position: Name:


Download ppt "IDK0040 Võrgurakendused I XML Deniss Kumlander. XML intro XML stands for EXtensible Markup Language XML is a markup language much like HTML and was invented."

Similar presentations


Ads by Google