Click here to load reader

Xml session

Embed Size (px)

DESCRIPTION

 

Citation preview

2. Agenda Introduction XML vs. HTML XML basic rules XML overall structure & building blocks XML document validation XML related technologies. XML parsing (JAXP) JAXB 3. Introduction XML stands for eXtensible Markup Language. XML document describes the structure of data. XML has no mechanism to specify the format forpresenting data to the user.(you specify yourown tags and structure). XML document resides in its own file with an.xml extension. XML derived from SGML(Standard GeneralizedMarkup Language). 4. XML vs. HTMLHTMLXMLUsed to mark up data Mark up text (displayed to users)(processed by computer)Describes content(meaning) Describes bothonly structure(

,

, ) and appearance(
,,)Define your own tags Uses fixed, unchangeable set of tagsWell formedNot 5. XML basic rules XML is case sensitive All start tags must have end tags. Elements must be properly nested. XML declaration is the first statement. Every document must contain a root element. Attribute values must have quotation marks. Certain characters are reserved for parsing ( as,&,,) Documents that follow these basic rules are well-formed xml documents 6. XML overall structure & buildingblocks Document may start with one or more processinginstructions(PI) or directives- - PI provides application-specific documentinformation After PI there must be one root element containing allrest xml. XML building blocks1. Element 2. Tags 3. Attributes 4. Entities (special characters) Sunny & hot5. Character data Sunny & hot6. Empty element has no body 7. XML document PI Root Element ElementCornel Sub Element1240$ ElementCornel835$ 8. XML document tree 9. XML document validation DTD (Document Type Definition)- Defines the structure constraints for XMLdocuments.- Documents that conform to DTD are Validdocuments. XML Schema- Same as DTD, more powerful because itincludes facilities to specify the data type of elements and it isbased on XML.- Documents that conform to Schema areSchema valid 10. XML document validation(DTD) Can be categorized as 1. Internal subsetsElements declarations inside the document.-DTD-Instructions--> 2. External subsetsElements declarations are outside the document in file with .dtd extension 3. External subsets in Internet URL/book.dtd" > 11. XML document validation(DTD)(cont.) DTD file XML fileCore JAVACornell 12. DTD limitations Not written in XML syntax, DTD has its ownsyntax. So it is hard to learn. Certain number of element repetitions cant beachieved. XML document can reference only 1 DTD. Do not support namespaces. No constraints on character data.- PCDATA, CDATA allows any permutations ofcharacters.- But if we need to limit element value to int Not in DTD8 required 13. XML doc. validation(XMLSchema) Provide more powerful and flexible schemalanguage than DTD. It has 44 enhanced data types. You can create your own data types (ComplexData types). Written in xml. 14. XML Schema Data types Simple type1. Dont have sub-element.2. Dont have attributeEx. Complex type(your own data type)either have one of the following or all of them.1. sub-element.2. attributes. 15. Example (Schema) 16. Example(XML Document)Core Java1235 17. XML related technologies XPath XSLT (eXtensible Stylesheet Language Transformations) Used to translate from one form of XML to another. XPointeridentify the particular point in or part of an XMLdocument that an XLink links to. XQuery 18. XML related technologies(XPath) XPath is a W3C Standard. Expression language for locating particular parts ofXML documents. XPath is a major element in XSLT XQuery and XPointer are both built on XPathexpressions. XML documents are viewed as a tree of nodes.1. The root element node.2. Element nodes.3. Text nodes.4. Attribute nodes.5. Comment nodes.6. Processing Instruction nodes.7. Namespace nodes. 19. XPath (cont.) XPath expression evaluates to one of four types1. Node setcollection of nodes returned from location pathexpressions2. Boolean3. Number4. String Location path expressions- Form is Axis:: nodetest [predicate]- Each location step composed of1. Axis defines a Node-Set relative to thecurrent node2. Node test Consists of the Node name ORNode type3. Predicate optional and used to filter the node-set. 20. Ancester-or-self axis 21. Parent axis 22. Child axis 23. Ancestor axis 24. Descendant axis 25. Following 26. Following-sibling 27. Preceding 28. Preceding-sibling 29. XPath (cont.) Node test Consists of the Node name OR Node typeEx. Ex: Element, attribute --- etc Node test by type1. node() selects all nodes regardless oftheir type.2. text() selects all text nodes.3. comment() selects all comment nodes.4. processing-instruction() Selects allprocessing- instruction nodes 30. XPath (cont.) Node testCore Java1235 If you are at the root element bookChild::* selects 3 elements name, chapters,price If you are at chapters elementchild::text() selects 3 elements1. text node containing text before 122. text node with the value 123. text node containing text after 12 31. XPath (cont.) predicates Used to filter the node-set. Used to find a specific node or a node thatcontains a specific value. They are always embedded in square brackets. Predicate types.1. Numeric predicates.2. Boolean predicates.3. String predicates.4. Node-set predicates. 32. XPath (cont.) predicates Numeric predicates(+,-,*, div, mod) and the following functionsceiling(), floor(), round(), sum()/book/name[1] selects the name of the first book. Boolean predicatesall of us know Boolean operators/book[price < 40] selects all books whose price is less than 40 String predicates Strings in XPath is made up ofUnicode characters.Work with = and != operatorsstarts-with(str1, str2), contains(str1,str2), string-length(str),substring(str, offset, length), concat(str1, str2,..) The previous predicates cannot be used in match patternof xsl:template 33. XPath (cont.) predicates Node-set predicates. last() the last position of the current node in the node-set position() position of the current node in the node-set. count() number of nodes in node-set 34. XPath Abbreviated location pathAbbreviationExpanded Form@Name Attribute::Name///descendant-or-self::Node()/. self::node()..parent::node()* Matches any element@*Matches any attribute elementNode()Matches any node of any kind 35. XML related technologies(XSLT) W3C standard for XML transformation Made of two parts.1. XSL Transformation (XSLT).2. XSL Formatting Objects (XSL-FO). Transforms XML document into1. Another XML Document (XHTML or WML).2. HTML document.3. Text 36. XML related technologies(XSLT) 37. XML related technologies(XSLT) template value-of apply-templates for-each if when, choose, otherwise sort filtering 38. XML related technologies(XSLT) templateIt is a container for a set of rules to apply actionsagainst the source tree to produce a result tree General form match uses XPath expression to match elements 39. XML related technologies(XSLT) value-ofUsed inside template element to extract valuefrom the source tree and insert it in the resulttree. General form 40. XML related technologies(XSLT) apply-templatesExecutes templates based on the current contextand passes control over to the other template. The apply-template has a select attribute, whichtells the XSLT processor which nodes to applytemplates to. If there is no select attribute the XSLTprocessor collects all the children of the currentnode and applies template to them. 41. XML related technologies(XSLT) call-templatecall template by name as function calling. Used as followingSyntax: Ex. 42. XML related technologies(XSLT) if conditional processingPerform conditional processing such as ifstatement in java some output if the expression is true Ex. 43. XML related technologies(XSLT) Iterationiteration through node set using element for-each action insert here Ex. 44. XML related technologies(XSLT) Sortingvalue of attribute order can beascending A-Z defaultdescending Z-A Ex. 45. XML related technologies(XSLT) Choose - perform conditional processing - has child elements when and otherwise Ex. ... some output ... ... some output ... ----------------------- ... some output .... 46. XML related technologies(XSLT) Creating Elements and Attributes Creating Elements- Dynamic wayAttribute Value string 47. JAXP