1 / 46

CREATING AN XML DOCUMENT

CREATING AN XML DOCUMENT. INTRODUCING XML. XML stands for Extensible Markup Language. A markup language specifies the structure and content of a document. Because it is extensible, XML can be used to create a wide variety of document types. INTRODUCING XML.

ryann
Download Presentation

CREATING AN XML DOCUMENT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CREATING AN XML DOCUMENT

  2. INTRODUCING XML • XML stands for Extensible Markup Language. A markup language specifies the structure and content of a document. • Because it is extensible, XML can be used to create a wide variety of document types.

  3. INTRODUCING XML • XML is a subset of the Standard Generalized Markup Language (SGML) which was introduced in the 1980s. SGML is very complex and can be costly. These reasons led to the creation of Hypertext Markup Language (HTML), a more easily used markup language. XML can be seen as sitting between SGML and HTML – easier to learn than SGML, but more robust than HTML.

  4. THE LIMITS OF HTML • HTML was designed for formatting text on a Web page. It was not designed for dealing with the content of a Web page. Additional features have been added to HTML, but they do not solve data description or cataloging issues in an HTML document. • Because HTML is not extensible, it cannot be modified to meet specific needs. Browser developers have added features making HTML more robust, but this has resulted in a confusing mix of different HTML standards.

  5. XML VOCABULARIES

  6. WELL-FORMED AND VALID XML DOCUMENTS • An XML document is well-formed if it contains no syntax errors and fulfills all of the specifications for XML code as defined by the W3C. • An XML document is valid if it is well-formed and also satisfies the rules laid out in the DTD or schema attached to the document.

  7. THE STRUCTURE OF AN XML DOCUMENT • XML documents consist of three parts • Prolog: optional and information about the document itself • Document body: the document’s content in a hierarchical tree structure • Epilog: optional and any final comments or processing instructions

  8. THE STRUCTURE OF AN XML DOCUMENT: THE XML DECLARATION • The XML declaration is always the first line of code in an XML document. It tells the processor what follows is written using XML. It can also provide any information about how the parser should interpret the code. • The complete syntax is: <?xml version=“version number” encoding=“encoding type” standalone=“yes | no” ?> • A sample declaration might look like this: <?xml version=“1.0” encoding=“UTF-8” standalone=“yes” ?>

  9. THE STRUCTURE OF AN XML DOCUMENT: INSERTING COMMENTS • Comments or miscellaneous statements go after the declaration. Comments may appear anywhere after the declaration. • The syntax for comments is: <!- - comment text- -> • This is the same syntax for HTML comments

  10. ELEMENT • Element names are case sensitive • Elements can be nested, as follows: <tracks>Kind of Blue <track>So What ((:22)</track> <track>Blue in Green (5:37)</track> </tracks>

  11. WORKING WITH ATTRIBUTES • An attribute is a feature or characteristic of an element. Attributes are text strings and must be placed in single or double quotes. The syntax is: <element_name attribute=“value”> … </element_name>

  12. ELEMENTS AND ATTRIBUTES: ADDING ELEMENTS TO THE JAZZ.XML FILE { document elements

  13. CHARACTER REFERENCES This figure shows commonly used character reference numbers

  14. CDATA SECTIONS • A CDATA section is a large block of text the XML processor will interpret only as text. • The syntax to create a CDATA section is: <! [CDATA [ Text Block ] ]>

  15. CDATA SECTIONS • In this example, a CDATA section stores several HTML tags within an element named HTMLCODE: <htmlcode> <![CDATA[ <h1>The Jazz Warehouse</h1> <h2>Your Online Store for Jazz Music</h2> ] ]> </htmlcode>

  16. CDATA SECTIONS This figure shows the revised Jazz.XML file CDATA section

  17. PARSING AN XML DOCUMENT

  18. LINKING TO A STYLE SHEET • Link the XML document to a style sheet to format the document. The XML processor will combine the style sheet with the XML document and apply any formatting codes defined in the style sheet to display a formatted document. • There are two main style sheet languages used with XML: • Cascading Style Sheets (CSS) and Extensible Style Sheets (XSL)

  19. LINKING TO A STYLE SHEET • There are some important benefits to using style sheets: • By separating content from format, you can concentrate on the appearance of the document • Different style sheets can be applied to the same XML document • Any style sheet changes will be automatically reflected in any Web page based upon the style sheet

  20. APPLYING A STYLE TO AN ELEMENT • To apply a style sheet to a document, use the following syntax: selector {attribute1:value1; attribute2:value2; …} • selector is an element (or set of elements) from the XML document. • attribute and value are the style attributes and attribute values to be applied to the document.

  21. CREATING PROCESSING INSTRUCTIONS • The link from the XML document to a style sheet is created using a processing statement. • A processing instruction is a command that gives instructions to the XML parser.

  22. CREATING PROCESSING INSTRUCTIONS • For example: <?xml-stylesheet type=“style” href=“sheet” ?> • Style is the type of style sheet to access and sheet is the name and location of the style sheet.

  23. THE JW.CSS STYLE SHEET This figure shows the cascading style sheet stored in the jw.css file

  24. LINKING TO THE JW.CSS STYLE SHEET This figure shows how to link the JW.css style sheet to the Jazz.xml file processing instruction to access the jw.css style sheet

  25. THE JAZZ.XML DOCUMENT FORMATTED WITH THE JW.CSS STYLE SHEET This figure shows the formatted jazz.xml file

  26. WORKING WITH XSLT

  27. GENERATING A RESULT DOCUMENT • An XSLT style sheet converts a source document of XML content into a result document by using the XSLT processor

  28. CREATING AN XSLT STYLE SHEET • To create an XSLT style sheet, the general structure: • <?xml version =“1.0”> • <xsl:stylesheet version = “1.0” • xmlns:xsl =“http://www.w3.org/1999/XSL/Transform”> • Content of the style sheet • </xsl:stylesheet> • The <xsl:stylesheet> tag can be substituted for the <xsl:transform> tag

  29. Under XPath, each component in the document is referred to as a node, and the entire structure of the document is a node tree The node tree consists of the following objects: the source document itself comments processing instructions namespaces elements, element text element attributes WORKING WITH DOCUMENT NODES

  30. NODE TREE EXAMPLE

  31. With a relative path, the location of the node is indicated relative to a specific node in the tree called the context node RELATIVE PATHS

  32. For absolute path, XPath begins with the root node, identified by a forward slash and proceeds down the levels of the node tree An absolute path: /child1/child2/child3/… To reference an element without regard to its location in the node tree, use a double forward slash with the name of the descendant node A relative path : //descendant USING XPATH TO REFERENCE A NODE

  33. XPath allows you to refer to groups of nodes by using the wildcard character (*) To select all of the nodes in the node tree, you can use the path: //* The (*) symbol matches any node, and the (//)symbol matches any level of the node tree Example: /portfolio/stock/* REFERENCING GROUPS OF ELEMENTS

  34. XPath uses different notation to refer to attribute nodes The syntax for attribute node is: @attribute where attribute is the name of the attribute Example: /portfolio/stock/name/@symbol REFERENCING ATTRIBUTE NODES

  35. The text contained in an element node is treated as a text node The syntax for referencing a text node is: text() To match all text nodes in the document, use: //text() WORKING WITH TEXT NODES

  36. A template is a collection of elements that define how a particular section of the source document should be transformed in the result document The root template sets up the initial code for the result document CREATING THE ROOT TEMPLATE

  37. CREATING A TEMPLATE • To create a template, the syntax is: <xsl:template match=“node set”> styles </xsl:template> • where node set is an XPath expression that references a node set from the source document and styles are the XSLT styles applied to those nodes

  38. CREATING A ROOT TEMPLATE • To create a root template, the syntax is: <xsl:template match=“/”> styles </xsl:template>

  39. EXTRACTING ELEMENT VALUES • To insert a node’s value into the result document, the syntax is: • <xsl:value-of> select=“expression” /> • where expression is an expression that identifies the node from the source document’s node tree • If the node contains child elements in addition to text content, the text in those child nodes appears as well

  40. INSERTING A NODE VALUE EXAMPLE

  41. To process a batch of nodes, the syntax is: <xsl:for-each select=“expression” /> styles </xsl:for-each> where expression is an expression that defines the group of nodes to which the XSLT and literal result elements are applied PROCESSING SEVERAL ELEMENTS

  42. PROCESSING SEVERAL ELEMENTS

  43. To apply a template in the result document, use the XSLT element <xsl:apply-templates select=“expression” /> where expression indicates the node template to be applied WORKING WITH TEMPLATES

  44. CREATING THE STOCK TEMPLATE EXAMPLE

  45. SORTING NODE SETS • By default, nodes are processed in document order, by their appearance in the document • To specify a different order, XSLT provides the <xsl:sort> element • This element can be used with either the <xsl:apply-templates> or the <xsl:for-each> element

  46. SORTING NODE SETS • The <xsl:sort> element contains several attributes to control how the XSLT process sorts the nodes in the source document • The select attribute determines the criteria under which the context node is sorted • The data-type attribute indicates the type of data • The order attribute indicates the direction of the sorting (ascending or descending)

More Related