Optional XML declaration. Element article is the root element. Article.xml Elements title, date, author, summary and content are child elements of article.">
Download presentation
Presentation is loading. Please wait.
1
Chapter 20 - Extensible Markup Language (XML)
Outline Introduction Structuring Data XML Namespaces Document Type Definitions (DTDs) and Schemas Document Type Definitions W3C XML Schema Documents XML Vocabularies MathML Chemical Markup Language (CML) Other Markup Languages Document Object Model (DOM) DOM Methods Simple API for XML (SAX) Extensible Stylesheet Language (XSL) Microsoft BizTalk Simple Object Access Protocol (SOAP) Internet and World Wide Web Resources
2
Optional XML declaration. Element article is the root element.
1 <?xml version = "1.0"?> 2 3 <!-- Fig. 20.1: article.xml > 4 <!-- Article structured with XML --> 5 6 <article> 7 <title>Simple XML</title> 9 <date>September 19, 2001</date> 11 <author> <firstName>Tem</firstName> <lastName>Nieto</lastName> </author> 16 <summary>XML is pretty easy.</summary> 18 <content>Once you have mastered XHTML, XML is easily learned. You must remember that XML is not for displaying information but for managing information. </content> 23 24 </article> Optional XML declaration. Element article is the root element. Article.xml Elements title, date, author, summary and content are child elements of article.
3
20.2 Structuring Data Fig IE5.5 displaying article.xml.
4
Element flag is an empty element and does not contain any text.
1 <?xml version = "1.0"?> 2 3 <!-- Fig. 20.3: letter.xml > 4 <!-- Business letter formatted with XML --> 5 6 <!DOCTYPE letter SYSTEM "letter.dtd"> 7 8 <letter> 9 <contact type = "from"> <name>John Doe</name> <address1>123 Main St.</address1> <address2></address2> <city>Anytown</city> <state>Anystate</state> <zip>12345</zip> <phone> </phone> <flag gender = "M"/> </contact> 20 <contact type = "to"> <name>Joe Schmoe</name> <address1>Box 12345</address1> <address2>15 Any Ave.</address2> <city>Othertown</city> <state>Otherstate</state> <zip>67890</zip> <phone> </phone> <flag gender = "M"/> </contact> 31 Element flag is an empty element and does not contain any text. Letter.xml Information for the person writing the letter. Information for the person receiving the letter.
5
Letter.xml Program Output
<salutation>Dear Sir:</salutation> 33 <paragraph>It is our privilege to inform you about our new database managed with XML. This new system allows you to reduce the load of your inventory list server by having the client machine perform the work of sorting and filtering the data.</paragraph> <closing>Sincerely</closing> <signature>Mr. Doe</signature> 41 42 </letter> Letter.xml Program Output Body of letter. Author’s signature.
6
Program Output
7
Keyword xmlns creates two namespace prefixes, text and image.
1 <?xml version = "1.0"?> 2 3 <!-- Fig : namespace.xml --> 4 <!-- Demonstrating Namespaces --> 5 6 <text:directory xmlns:text = "urn:deitel:textInfo" xmlns:image = "urn:deitel:imageInfo"> 8 <text:file filename = "book.xml"> <text:description>A book list</text:description> </text:file> 12 <image:file filename = "funny.jpg"> <image:description>A funny picture</image:description> <image:size width = "200" height = "100"/> </image:file> 17 18 </text:directory> Keyword xmlns creates two namespace prefixes, text and image. Namespace.xml URIs ensure that a namespace is unique.
8
Element file uses the default namespace.
1 <?xml version = "1.0"?> 2 3 <!-- Fig : defaultnamespace.xml --> 4 <!-- Using Default Namespaces > 5 6 <directory xmlns = "urn:deitel:textInfo" xmlns:image = "urn:deitel:imageInfo"> 8 <file filename = "book.xml"> <description>A book list</description> </file> 12 <image:file filename = "funny.jpg"> <image:description>A funny picture</image:description> <image:size width = "200" height = "100"/> </image:file> 17 18 </directory> Default namespace. Defaultnamespace.xml Element file uses the default namespace. Element file uses the namespace prefix image.
9
The ELEMENT element type declaration defines the rules for element letter.
1 <!-- Fig. 20.4: letter.dtd > 2 <!-- DTD document for letter.xml --> 3 4 <!ELEMENT letter ( contact+, salutation, paragraph+, closing, signature )> 6 7 <!ELEMENT contact ( name, address1, address2, city, state, zip, phone, flag )> 9 <!ATTLIST contact type CDATA #IMPLIED> 10 11 <!ELEMENT name ( #PCDATA )> 12 <!ELEMENT address1 ( #PCDATA )> 13 <!ELEMENT address2 ( #PCDATA )> 14 <!ELEMENT city ( #PCDATA )> 15 <!ELEMENT state ( #PCDATA )> 16 <!ELEMENT zip ( #PCDATA )> 17 <!ELEMENT phone ( #PCDATA )> 18 <!ELEMENT flag EMPTY> 19 <!ATTLIST flag gender (M | F) "M"> 20 21 <!ELEMENT salutation ( #PCDATA )> 22 <!ELEMENT closing ( #PCDATA )> 23 <!ELEMENT paragraph ( #PCDATA )> 24 <!ELEMENT signature ( #PCDATA )> The contact element definition specifies that element contact contains child elements name, address1, address2, city, state, zip, phone and flag— in that order. The plus sign (+) occurrence indicator specifies that the DTD allows one or more occurrences of an element. The ATTLIST element type declaration defines an attribute (i.e., type) for the contact element. Letter.dtd Keyword #IMPLIED specifies that if the parser finds a contact element without a type attribute, the parser can choose an arbitrary value for the attribute or ignore the attribute and the document will be valid. Flag #PCDATA specifies that the element can contain parsed character data (i.e., text).
10
Book.xml Program Output
1 <?xml version = "1.0"?> 2 <!-- Fig : book.xml > 3 <!-- Book list marked up as XML --> 4 5 <deitel:books xmlns:deitel = " <book> <title>XML How to Program</title> </book> <book> <title>C How to Program</title> </book> <book> <title>Java How to Program</title> </book> <book> <title>C++ How to Program</title> </book> <book> <title>Perl How to Program</title> </book> 21 </deitel:books> Book.xml Program Output java -classpath .;..\lib\xmlparserv2.jar;..\lib\xschema.jar XSDSetSchema book.xsd book.xml The input file <book.xml> parsed without errors
11
Namespace URI. Namespace prefix.
1 <?xml version = "1.0"?> 2 3 <!-- Fig : book.xsd > 4 <!-- Simple W3C XML Schema document --> 5 6 <xsd:schema xmlns:xsd = " xmlns:deitel = " targetNamespace = " 9 <xsd:element name = "books" type = "deitel:BooksType"/> 11 <xsd:complexType name = "BooksType"> <xsd:element name = "book" type = "deitel:BookType" minOccurs = "1" maxOccurs = "unbounded"/> </xsd:complexType> 16 <xsd:complexType name = "BookType"> <xsd:element name = "title" type = "xsd:string"/> </xsd:complexType> 20 21 </xsd:schema> Namespace URI. Namespace prefix. Attributes name and type specify the element’s name and data type, respectively. Book.xsd Element element defines an element to be included in the XML document structure. Attribute maxOccurs, with value unbounded specifies that books may have any number of book child elements. Element complexType defines an element type that has a child element named book. Attribute minOccurs specifies that books must contain a minimum of one book element.
12
Mathml1.html Program Output
1 <?xml version="1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " 4 5 <!-- Fig. 20.9: mathml1.html --> 6 <!-- Simple MathML > 7 8 <html xmlns=" 9 <head><title>Simple MathML Example</title></head> 11 <body> 13 <math xmlns = " 15 <mrow> <mn>2</mn> <mo>+</mo> <mn>3</mn> <mo>=</mo> <mn>5</mn> </mrow> 23 </math> 25 </body> 27 </html> Mathml1.html Program Output Element mrow behaves like parentheses, which allows the document author to group related elements properly.
13
The msup element represents a superscript.
1 <?xml version="1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " 4 <!-- Fig : mathml2.html --> 5 <!-- Simple MathML > 6 7 <html xmlns=" 8 <head><title>Algebraic MathML Example</title></head> 10 <body> 12 <math xmlns = " <mrow> 15 <mrow> <mn>3</mn> <mo>⁢</mo> 19 <msup> <mi>x</mi> <mn>2</mn> </msup> 24 </mrow> 26 <mo>+</mo> <mi>x</mi> <mo>-</mo> 30 <mfrac> <mn>2</mn> <mi>x</mi> </mfrac> 35 Mathml2.html entity reference ⁢ to indicate a multiplication operation without a symbolic representation. The msup element represents a superscript. Element mfrac display a fraction.
14
Mathml2.html Program Output
<mo>=</mo> <mn>0</mn> 38 </mrow> </math> 41 </body> 43 </html> Mathml2.html Program Output
15
The entity ∫ represents the integral symbol.
1 <?xml version="1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " 4 <!-- Fig mathml3.html > 5 <!-- Calculus example using MathML --> 6 7 <html xmlns=" 8 <head><title>Calculus MathML Example</title></head> 10 <body> 12 <math xmlns = " <mrow> <msubsup> 16 <mo>∫</mo> <mn>0</mn> 19 <mrow> <mn>1</mn> <mo>-</mo> <mi>y</mi> </mrow> 25 </msubsup> 27 <msqrt> <mrow> 30 <mn>4</mn> <mo>⁢</mo> 33 Mathml3.html The entity ∫ represents the integral symbol. Element mo marks up the integral operator. Element mn marks up the number (i.e., 0) that represents the subscript. The msubsup element specifies the superscript and subscript. Element msqrt represents a square root expression.
16
Mathm13.html Program Output
<msup> <mi>x</mi> <mn>2</mn> </msup> 38 <mo>+</mo> <mi>y</mi> 41 </mrow> </msqrt> 44 <mo>δ</mo> <mi>x</mi> </mrow> </math> </body> 50 </html> Mathm13.html Program Output Entity δ represents a delta symbol.
17
Element bondArray defines the bonds between atoms.
1 <?jumbo:namespace ns = " prefix = "C" java = "jumbo.cmlxml.*Node" ?> 3 4 <!-- Fig : ammonia.xml --> 5 <!-- Structure of ammonia > 6 7 <C:molecule id = "Ammonia"> 8 <C:atomArray builtin = "elsym"> N H H H </C:atomArray> 12 <C:atomArray builtin = "x2" type = "float"> </C:atomArray> 16 <C:atomArray builtin = "y2" type = "float"> </C:atomArray> 20 <C:bondArray builtin = "atid1"> </C:bondArray> 24 <C:bondArray builtin = "atid2"> </C:bondArray> 28 <C:bondArray builtin = "order" type = "integer"> </C:bondArray> 32 33 </C:molecule> A processing instruction (PI is application-specific information embedded in an XML document. Ammonia.xml A value of x2 and type float specifies that the element contains a list of floating-point numbers, each of which indicates the x-coordinate of an atom. A value of y2 specifies that the element contains a list of y-coordinate values. Element bondArray defines the bonds between atoms.
18
Program Output
19
20.7 DOM Methods
20
20.7 DOM Methods Fig. 20.14 Tree structure for article.xml. article
title date author firstName summary lastName contents Fig Tree structure for article.xml.
21
method load loads article.xml (Fig. 20.1) into memory.
1 <?xml version="1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " 4 <html xmlns=" 5 6 <!-- Fig : DOMExample.html --> 7 <!-- DOM with JavaScript > 8 <head> <title>A DOM Example</title> </head> 12 <body> 14 <script type = "text/javascript" language = "JavaScript"> <!-- var xmlDocument = new ActiveXObject( "Microsoft.XMLDOM" ); 18 xmlDocument.load( "article.xml" ); 20 // get the root element var element = xmlDocument.documentElement; 23 document.writeln( "<p>Here is the root node of the document: " + "<strong>" + element.nodeName + "</strong>" + "<br />The following are its child elements:" + "</p><ul>" ); 29 // traverse all child nodes of root element for ( var i = 0; i < element.childNodes.length; i++ ) { var curNode = element.childNodes.item( i ); 33 DOMExample.html Instantiate a Microsoft XML Document Object Model object and assign it to reference xmlDocument. method load loads article.xml (Fig. 20.1) into memory. Property documentElement corresponds to the root element in the document (e.g., article). Property nodeName corresponds to the name of an element, attribute, etc. Method item to obtain the child node at index i. Iterates through the root node’s children using property childNodes.
22
Property parentNode returns a node’s parent node.
// print node name of each child element document.writeln( "<li><strong>" + curNode.nodeName "</strong></li>" ); } 38 document.writeln( "</ul>" ); 40 // get the first child node of root element var currentNode = element.firstChild; 43 document.writeln( "<p>The first child of root node is: " + "<strong>" + currentNode.nodeName + "</strong>" + "<br />whose next sibling is:" ); 47 // get the next sibling of first child var nextSib = currentNode.nextSibling; 50 document.writeln( "<strong>" + nextSib.nodeName + "</strong>.<br />Value of <strong>" + nextSib.nodeName + "</strong> element is: " ); 54 var value = nextSib.firstChild; 56 // print the text value of the sibling document.writeln( "<em>" + value.nodeValue + "</em>" + "<br />Parent node of <strong>" + nextSib.nodeName + "</strong> is: <strong>" + nextSib.parentNode.nodeName + "</strong>.</p>" ); > </script> 64 </body> 66 </html> Retrieve the root node’s first child node (i.e., title) using property firstChild. DOMExample.html Property parentNode returns a node’s parent node.
23
Program Output
24
20.7 DOM Methods
25
20.7 DOM Methods
26
20.7 DOM Methods
27
20.7 DOM Methods
28
A processing instruction that references the XSL stylesheet games.xsl.
1 <?xml version = "1.0"?> 2 <?xml:stylesheet type = "text/xsl" href = "games.xsl"?> 3 4 <!-- Fig : games.xml --> 5 <!-- Sports Database > 6 7 <sports> 8 <game id = "783"> <name>Cricket</name> 11 <paragraph> More popular among commonwealth nations. </paragraph> </game> 16 <game id = "239"> <name>Baseball</name> 19 <paragraph> More popular in America. </paragraph> </game> 24 <game id = "418"> <name>Soccer (Football)</name> 27 <paragraph> Most popular sport in the world. </paragraph> </game> 32 33 </sports> A processing instruction that references the XSL stylesheet games.xsl. Value type specifies that games.xsl is a text/xsl file. Games.xml
29
Program Output
30
The stylesheet start tag—which begins the XSL stylesheet.
1 <?xml version = "1.0"?> 2 3 <!-- Fig : elements.xsl > 4 <!-- A simple XSLT transformation --> 5 6 <!-- reference XSL stylesheet URI --> 7 <xsl:stylesheet version = "1.0" xmlns:xsl = " 9 <xsl:output method = "html" omit-xml-declaration = "no" doctype-system = " doctype-public = "-//W3C//DTD XHTML 1.0 Strict//EN"/> 14 15 <xsl:template match = "/"> 16 <html xmlns=" 18 <head> <title>Sports</title> </head> 22 <body> 24 <table border = "1" bgcolor = "cyan"> 26 <thead> 28 <tr> <th>ID</th> <th>Sport</th> <th>Information</th> </tr> 34 </thead> The stylesheet start tag—which begins the XSL stylesheet. Elements.xsl Element xsl:output writes an XHTML document type declaration to the result tree. The match attribute to select the document root of the source document (i.e., game.xml).
31
36 <!-- insert each name and paragraph element value --> <!-- into a table row > <xsl:for-each select = "sports/game"> 40 <tr> <td><xsl:value-of select = <td><xsl:value-of select = "name"/></td> <td><xsl:value-of select = "paragraph"/></td> </tr> 46 </xsl:for-each> 48 </table> 50 </body> 52 </html> 54 55 </xsl:template> 56 57 </xsl:stylesheet> Elements.xsl Element xsl:for-each iterates through the source XML document and search for game elements. Element value-of retrieves attribute id’s value and place it in a td element in the result tree.
32
Reference to the XSL stylesheet sorting.xsl.
1 <?xml version = "1.0"?> 2 3 <!-- Fig : sorting.xml > 4 <!-- Usage of elements and attributes --> 5 6 <?xml:stylesheet type = "text/xsl" href = "sorting.xsl"?> 7 8 <book isbn = " X"> <title>Deitel's XML Primer</title> 10 <author> <firstName>Paul</firstName> <lastName>Deitel</lastName> </author> 15 <chapters> <frontMatter> <preface pages = "2"/> <contents pages = "5"/> <illustrations pages = "4"/> </frontMatter> 22 <chapter number = "3" pages = "44"> Advanced XML</chapter> <chapter number = "2" pages = "35"> Intermediate XML</chapter> <appendix number = "B" pages = "26"> Parsers and Tools</appendix> <appendix number = "A" pages = "7"> Entities</appendix> <chapter number = "1" pages = "28"> XML Fundamentals</chapter> </chapters> Sorting.xml Reference to the XSL stylesheet sorting.xsl.
33
34 35 <media type = "CD"/> 36 </book>
Sorting.xml
34
Create the title for the XHTML document.
1 <?xml version = "1.0"?> 2 3 <!-- Fig : sorting.xsl > 4 <!-- Transformation of Book information into XHTML --> 5 6 <xsl:stylesheet version = "1.0" xmlns:xsl = " 8 <xsl:output method = "html" omit-xml-declaration = "no" doctype-system = " doctype-public = "-//W3C//DTD XHTML 1.0 Strict//EN"/> 13 <xsl:template match = "/"> 15 <html xmlns = " <xsl:apply-templates/> </html> </xsl:template> 20 <xsl:template match = "book"> <head> <title>ISBN <xsl:value-of select = - <xsl:value-of select = "title"/></title> </head> 26 <body> <h1><xsl:value-of select = "title"/></h1> 29 <h2>by <xsl:value-of select = "author/lastName"/>, <xsl:value-of select = "author/firstName"/></h2> 32 Sorting.xsl Specify that the msxml processor should apply xsl:templates to the document root’s children. Create the title for the XHTML document. Create a header element that displays the book’s author.
35
Element xsl:sort sorts chapters by number in ascending order.
<table border = "1"> <xsl:for-each select = "chapters/frontMatter/*"> <tr> <td align = "right"> <xsl:value-of select = "name()"/> </td> 39 <td> ( <xsl:value-of select = pages ) </td> </tr> </xsl:for-each> 45 <xsl:for-each select = "chapters/chapter"> <xsl:sort select = data-type = "number" order = "ascending"/> <tr> <td align = "right"> Chapter <xsl:value-of select = </td> 53 <td> ( <xsl:value-of select = pages ) </td> </tr> </xsl:for-each> 59 <xsl:for-each select = "chapters/appendix"> <xsl:sort select = data-type = "text" order = "ascending"/> <tr> <td align = "right"> Appendix <xsl:value-of select = </td> 67 Select each element (indicated by an asterisk) that is a child of frontMatter. Sorting.xsl Call node-set function name to retrieve the current node’s element name (e.g., preface). Element xsl:sort sorts chapters by number in ascending order. Attribute select selects the value of attribute number in context node chapter. Attribute data-type specifies a numeric sort and attribute order specifies ascending order.
36
<td> ( <xsl:value-of select = pages ) </td> </tr> </xsl:for-each> </table> 74 <br />Pages: <xsl:variable name = "pagecount" select = <xsl:value-of select = "$pagecount"/> <br />Media Type: <xsl:value-of select = </body> </xsl:template> 82 83 </xsl:stylesheet> Use an XSL variable to store the value of the book’s page count and output the page count to the result tree. Sorting.xsl
37
Program Output
38
20.10 Microsoft BizTalk
39
Element From specifies the document’s source.
1 <?xml version = "1.0"?> 2 <BizTalk 3 xmlns = "urn:schemas-biztalk-org:BizTalk/biztalk-0.81.xml"> 4 5 <!-- Fig : BizTalkexample.xml --> 6 <!-- BizTalk example > 7 <Route> <From locationID = " " locationType = "DUNS" handle = "23" /> 10 <To locationID = " " locationType = "DUNS" handle = "45" /> 13 </Route> 14 15 <Body> 16 <Offers xmlns = 17 "x-schema: <Offer> <Model>12-a-3411d</Model> <Manufacturer>ExComp, Inc.</Manufacturer> <ManufacturerModel>DCS-48403</ManufacturerModel> <MerchantCategory>Clothes | Sports wear</MerchantCategory> <MSNClassId></MSNClassId> <StartDate> T13:12:00</StartDate> <EndDate> T13:12:00</EndDate> <RegularPrice>89.99</RegularPrice> <CurrentPrice>25.99</CurrentPrice> <DisplayPrice value = "3" /> <InStock value = "15" /> <ReferenceImageURL> </ReferenceImageURL> <OfferName>Clearance sale</OfferName> <OfferDescription>This is a clearance sale</OfferDescription> <PromotionalText>Free Shipping</PromotionalText> Element Route contains the routing information, which is mandatory for all BizTalk documents. Element From specifies the document’s source. Defines a default namespace for the BizTalk framework elements. BizTalkexample.xml Element To specifies the document’s destination. Element Body contains the actual message, whose schema the businesses define.
40
36 <Comments>Clothes that you would love to wear
<Comments>Clothes that you would love to wear.</Comments> <IconType value = "BuyNow" /> <ActionURL> <AgeGroup1 value = "Infant" /> <AgeGroup2 value = "Adult" /> <Occasion1 value = "Birthday" /> <Occasion2 value = "Anniversary" /> <Occasion3 value = "Christmas" /> </Offer> </Offers> 46 </Body> 47 </BizTalk> BizTalkexample.xml
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.