XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A...

46
XP 1 CREATING AN XML DOCUMENT

Transcript of XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A...

Page 1: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

1

CREATING AN XML DOCUMENT

Page 2: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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.

Page 3: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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.

Page 4: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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.

Page 5: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

5

XML VOCABULARIES

Page 6: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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.

Page 7: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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

Page 8: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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” ?>

Page 9: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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

Page 10: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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>

Page 11: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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>

Page 12: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

12

ELEMENTS AND ATTRIBUTES: ADDING ELEMENTS TO THE

JAZZ.XML FILE

document elements {

Page 13: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

13

CHARACTER REFERENCES

This figure shows commonly used character reference numbers

Page 14: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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

] ]>

Page 15: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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>

Page 16: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

16

CDATA SECTIONS

This figure shows the revised Jazz.XML file

CDATA section

Page 17: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

17

PARSING AN XML DOCUMENT

Page 18: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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)

Page 19: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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

Page 20: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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.

Page 21: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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.

Page 22: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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.

Page 23: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

23

THE JW.CSS STYLE SHEET

This figure shows the cascading style sheet stored in the jw.css file

Page 24: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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

Page 25: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

25

THE JAZZ.XML DOCUMENT FORMATTED WITH THE JW.CSS

STYLE SHEET

This figure shows the formatted jazz.xml file

Page 26: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

26

WORKING WITH XSLT

Page 27: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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

Page 28: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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

Page 29: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

29

WORKING WITH DOCUMENT NODES

• 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

Page 30: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

30

NODE TREE EXAMPLE

Page 31: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

31

RELATIVE PATHS

• With a relative path, the location of the node is indicated relative to a specific node in the tree called the context node

Page 32: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

32

USING XPATH TO REFERENCE A NODE

• 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

Page 33: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

33

REFERENCING GROUPS OF ELEMENTS

• 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/*

Page 34: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

34

REFERENCING ATTRIBUTE NODES

• 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

Page 35: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

35

WORKING WITH TEXT NODES

• 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()

Page 36: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

36

CREATING THE ROOT TEMPLATE

• 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

Page 37: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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

Page 38: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

38

CREATING A ROOT TEMPLATE

• To create a root template, the syntax is:

<xsl:template match=“/”>styles

</xsl:template>

Page 39: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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

Page 40: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

40

INSERTING A NODE VALUE EXAMPLE

Page 41: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

41

PROCESSING SEVERAL ELEMENTS

• 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

Page 42: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

42

PROCESSING SEVERAL ELEMENTS

Page 43: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

43

WORKING WITH TEMPLATES

• 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

Page 44: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

44

CREATING THE STOCK TEMPLATE EXAMPLE

Page 45: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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

Page 46: XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.

XP

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)