Chapter 18 Extensible Markup Language XML Outline 18
Chapter 18 – Extensible Markup Language (XML) Outline 18. 1 18. 2 18. 3 18. 4 18. 5 18. 6 18. 7 Introduction XML Documents XML Namespaces Document Object Model (DOM) Document Type Definitions (DTDs), Schemas and Validation 18. 5. 1 Document Type Definitions 18. 5. 2 Microsoft XML Schemas Extensible Stylesheet Language and Xsl. Transform Microsoft Biz. Talk 2002 Prentice Hall. All rights reserved. 1
2 18. 1 Introduction • Extensible Markup Language (XML) – Open technology – Data storage –. NET Framework provides an extensive set of XML-related class 2002 Prentice Hall. All rights reserved.
3 18. 2 XML Documents • Data markup by tags • Root element • XML parser – Role of style sheet 2002 Prentice Hall. All rights reserved.
Optional XML declaration 1 <? xml version = "1. 0"? > 2 identifying document type 3 <!-- Fig. 18. 1: article. xml --> version information parameter 4 <!-- Article structured with XML --> specify version of XML being used 5 Example of start tag Root element 6 <article> 7 8 <title>Simple XML</title> Examples of end tag 9 10 <date>December 6, 2001</date> 11 12 <author> Example of character data 13 <first. Name>Tem</first. Name> 14 <last. Name>Nieto</last. Name> 15 </author> 16 17 <summary>XML is pretty easy. </summary> 18 19 <content>In this chapter, we present a wide variety of examples 20 that use XML 21 </content> 22 23 </article> Outline article. xml 2002 Prentice Hall. All rights reserved. 4
5 18. 2 XML Documents minus sign Fig. 18. 2 article. xml displayed by Internet Explorer (part 1). 2002 Prentice Hall. All rights reserved.
6 18. 2 XML Documents plus sign Fig. 18. 2 article. xml displayed by Internet Explorer (part 2). 2002 Prentice Hall. All rights reserved.
1 <? xml version = "1. 0"? > 2 element has attribute type 3 <!-- Fig. 18. 3: letter. xml --> Child element of root 4 <!-- Business letter formatted with XML --> with value “from” 5 element letter 6 <letter> 7 <contact type = "from"> The person who sent the letter 8 <name>Jane Doe</name> 9 <address 1>Box 12345</address 1> 10 <address 2>15 Any Ave. </address 2> 11 <city>Othertown</city> 12 <state>Otherstate</state> 13 <zip>67890</zip> 14 <phone>555 -4321</phone> Empty element flag 15 <flag gender = "F" /> 16 </contact> 17 18 <contact type = "to"> The recipient of the letter 19 <name>John Doe</name> 20 <address 1>123 Main St. </address 1> 21 <address 2></address 2> 22 <city>Anytown</city> 23 <state>Anystate</state> 24 <zip>12345</zip> 25 <phone>555 -1234</phone> 26 <flag gender = "M" /> 27 </contact> 28 Outline letter. xml 2002 Prentice Hall. All rights reserved. 7
29 <salutation>Dear Sir: </salutation> 30 31 <paragraph>It is our privilege to inform you about our new 32 database managed with <technology>XML</technology>. This 33 new system allows you to reduce the load on 34 your inventory list server by having the client machine 35 perform the work of sorting and filtering the data. 36 </paragraph> 37 38 <paragraph>Please visit our Web site for availability 39 and pricing. 40 </paragraph> Other expected parts of a 41 <closing>Sincerely</closing> 42 <signature>Ms. Doe</signature> typical letter 43 </letter> Outline letter. xml 2002 Prentice Hall. All rights reserved. 8
9 18. 3 XML Namespaces • Naming collision • XML-based vocabularies – XML Schema – Extensible Stylesheet Language – Biz. Talk • Namespace prefixes 2002 Prentice Hall. All rights reserved.
Outline 1 <? xml version = "1. 0"? > 2 3 <!-- Fig. 18. 4: namespace. xml --> Universal Resource Identifier that Prefix text to qualify elements file 4 <!-- Demonstrating namespaces --> uniquely identifies the namespace and description as belonging to Attribute xmlns to create 5 6 <text: directory xmlns: text = namespace "urn: deitel: text. Info" two namespace prefixes 7 xmlns: image = "urn: deitel: image. Info"> urn: deitel: text. Info by 8 Parser never visits these URI’s, they applying namespace prefix text 9 <text: filename = "book. xml"> Apply namespace prefix image to are used to differentiate names 10 <text: description> A book list</text: description> elements file, description namespace. xml 11 </text: file> and size 12 13 <image: filename = "funny. jpg"> 14 <image: description> A funny picture</image: description> 15 <image: size width = "200" height = "100" /> 16 </image: file> 17 18 </text: directory> Program Output 2002 Prentice Hall. All rights reserved. 10
Outline 11 1 <? xml version = "1. 0"? > 2 file prefixed with image to 3 <!-- Fig. 18. 5: defaultnamespace. xml --> indicate it is in the namespace 4 <!-- Using default namespaces --> Declare a default namespace urn: deitel: image. Info and 5 using attribute xmlns with 6 <directory xmlns = "urn: deitel: text. Info" not the default namespace No longer prefix file or URI as its value 7 xmlns: image = "urn: deitel: image. Info"> description with text 8 9 <filename = "book. xml"> defaultnamespace. 10 <description>A book list</description> 11 </file> xml 12 13 <image: filename = "funny. jpg"> 14 <image: description>A funny picture</image: description> 15 <image: size width = "200" height = "100" /> 16 </image: file> 17 18 </directory> Program Output 2002 Prentice Hall. All rights reserved.
12 18. 4 Document Object Model (DOM) • Store document data as tree structure – DOM tree – DOM parser – Each component is a node in the tree • Root node • Parent or sibling nodes • Descendant or ancestor node 2002 Prentice Hall. All rights reserved.
13 18. 4 Document Object Model (DOM) article title date author first. Name summary last. Name contents Fig. 18. 6 Tree structure for Fig. 18. 1. 2002 Prentice Hall. All rights reserved.
Outline 14 1 // Fig. 18. 7: Xml. Reader. Test. cs 2 // Reading an XML document. 3 4 using System; 5 using System. Windows. Forms; Reference namespace that 6 using System. Xml; contains the XML classes Create a new 7 Xml. Document object used for this example 8 public class Xml. Reader. Test : System. Windows. Form 9 { 10 private System. Windows. Forms. Text. Box output. Text. Box; 11 private System. Component. Model. Container components = null; 12 13 public Xml. Reader. Test() Create an Xml. Node. Reader 14 { that can read one node at a 15 Initialize. Component(); time from document 16 17 // reference to "XML document" 18 Xml. Document document = new Xml. Document(); Xml. Reader. Test. cs 19 document. Load( "article. xml" ); 20 21 // create Xml. Node. Reader for document 22 Xml. Node. Reader reader = new Xml. Node. Reader( document ); 23 Parse and load article. xml into 24 // show form before output. Text. Box is populated 25 this. Show(); variable document by calling 26 Xml. Document method Load 27 // tree depth is -1, no indentation 28 int depth = -1; 29 Variable depth keeps track of the number of tab characters needed to display the current element 2002 Prentice Hall. All rights reserved.
Outline 15 30 // display each node's content Loop to read each node in the 31 while ( reader. Read() ) 32 { DOM tree switch statement to 33 switch ( reader. Node. Type ) process the node type 34 { 35 // if Element, display its name 36 case Xml. Node. Type. Element: 37 38 // increase tab depth If the node is a comment, use Value 39 depth++; property to display the comment 40 Tab. Output( depth ); between comment notation tags 41 output. Text. Box. Text += "<" + reader. Name + ">" + 42 "rn"; 43 44 // if empty element, decrease depth If the node is an element, use Name property to display it 45 if ( reader. Is. Empty. Element ) 46 depth--; between angle brackets 47 Xml. Reader. Test. cs 48 break; If the node contains text, use 49 Value property to display it 50 // if Comment, display it 51 case Xml. Node. Type. Comment: 52 Tab. Output( depth ); 53 output. Text. Box. Text += 54 "<!--" + reader. Value + "-->rn"; 55 break; 56 57 // if Text, display it 58 case Xml. Node. Type. Text: 59 Tab. Output( depth ); 60 output. Text. Box. Text += "t" + reader. Value + 61 "rn" ; 62 break; 63 2002 Prentice Hall. All rights reserved.
Outline 16 64 // if XML declaration, display it 65 case Xml. Node. Type. Xml. Declaration: 66 Tab. Output( depth ); 67 output. Text. Box. Text += "<? " + reader. Name + " " 68 + reader. Value + " ? >rn"; 69 break; 70 71 // if End. Element, display it and decrement depth 72 case Xml. Node. Type. End. Element: 73 Tab. Output( depth ); 74 output. Text. Box. Text += "</" + reader. Name 75 + ">rn"; 76 depth--; 77 break; If the node is an XML declaration, 78 } // end switch statement use Name and Value properties to 79 } // end while loop display the declaration 80 } // End Xml. Reader. Test constructor 81 Xml. Reader. Test. cs 82 // insert tabs 83 private void Tab. Output( int number ) 84 { 85 for ( int i = 0; i < number; i++ ) 86 output. Text. Box. Text += "t"; If the node is the end of an element, 87 } // end Tab. Output display the close tag using Name 88 property and decrement depth 89 // Windows Form Designer generated code 90 91 [STAThread] 92 static void Main() 93 { Method Tab. Output outputs a 94 Application. Run( new Xml. Reader. Test() ); 95 } // end Main specified number of tab characters 96 } // end Xml. Reader. Test 2002 Prentice Hall. All rights reserved.
Outline 17 Xml. Reader. Test. cs Program Output 2002 Prentice Hall. All rights reserved.
1 // Fig. 18. 8: Xml. Dom. cs 2 // Demonstrates DOM tree manipulation. 3 4 using System; 5 using System. Windows. Forms; 6 using System. Xml; 7 using System. IO; 8 using System. Code. Dom. Compiler; // contains Temp. File. Collection 9 10 // Class Xml. Dom demonstrates the DOM 11 public class Xml. Dom : System. Windows. Form 12 { 13 private System. Windows. Forms. Button build. Button; 14 private System. Windows. Forms. Button print. Button; 15 private System. Windows. Forms. Tree. View xml. Tree. View; 16 private System. Windows. Forms. Text. Box console. Text. Box; 17 private System. Windows. Forms. Button reset. Button; 18 private System. Component. Model. Container components = null; 19 20 private Xml. Document source; // reference to "XML document" 21 22 // reference copy of source's "XML document" 23 private Xml. Document copy; 24 25 private Tree. Node tree; // Tree. Node reference Object tree of type 26 27 public Xml. Dom() Tree. Node to represent 28 { 29 Initialize. Component(); each node in the tree 30 31 // create Xml. Document and load letter. xml Assign new Xml. Document 32 source = new Xml. Document(); Method Load to parse object to reference source 33 source. Load( "letter. xml" ); and load letter. xml 34 Outline Xml. Dom. cs 2002 Prentice Hall. All rights reserved. 18
Outline 35 // initialize references to null 36 copy = null; 37 tree = null; 38 } // end Xml. Dom 39 40 [STAThread] 41 static void Main() 42 { 43 Application. Run( new Xml. Dom() ); 44 } 45 46 // event handler for build. Button click event 47 private void build. Button_Click( object sender, 48 System. Event. Args e ) Assign to tree’s Text property 49 { 50 // determine if copy has been built already the name of the root in source Insert tree into Tree. View 51 if ( copy != null ) control’s node list via method Add 52 return; // document already exists Xml. Dom. cs 53 54 // instantiate Xml. Document and Tree. Node 55 copy = new Xml. Document(); 56 tree = new Tree. Node(); 57 58 // add root node name to Tree. Node and add 59 // Tree. Node to Tree. View control 60 tree. Text = source. Name; // assigns #root 61 xml. Tree. View. Nodes. Add( tree ); 62 63 // build node and tree hierarchy Copy XMLDocument referenced by 64 Build. Tree( source, copy, tree ); source and update Tree. View 65 66 print. Button. Enabled = true; 67 reset. Button. Enabled = true; 68 } // end build. Button_Click 2002 Prentice Hall. All rights reserved. 19
Outline 69 70 // event handler for print. Button click event 71 private void print. Button_Click( object sender, 72 System. Event. Args e ) Write all the contents 73 { of copy to writer 74 // exit if copy does not reference an Xml. Document 75 if ( copy == null ) Create an Xml. Text. Writer that writes 76 return; to the file whose name is in the first 77 Read all the data 78 // create temporary XML file element of filename array (the one file 79 Temp. File. Collection file = new Temp. File. Collection(); added to the collection on line 82) 80 Create an Xml. Text. Reader 81 // create file that is deleted at program termination for the temporary file 82 file. Add. Extension( "xml", false ); 83 string[] filename = new string[ 1 ]; If copy does not reference an 84 file. Copy. To( filename, 0 ); Xml. Document, exit the method 85 86 // write XML data to disk Print a slash if the node is an Xml. Dom. cs 87 Xml. Text. Writer writer = new Xml. Text. Writer( filename[ 0 ], the end of an element 88 System. Text. Encoding. UTF 8 ); Create a Temp. File. Collection that 89 copy. Write. To( writer ); 90 writer. Close(); will be used to store one temporary file 91 92 // parse and load temporary XML document 93 Xml. Text. Reader reader = new Xml. Text. Reader( filename[ 0 ] ); 94 95 // read, format and display data Add a file with the. xml 96 while( reader. Read() ) extension and specify that it Use method Copy. To to 97 { should be deleted after use 98 if ( reader. Node. Type == Xml. Node. Type. End. Element ) copy the names of all the 99 console. Text. Box. Text += "/"; files in the collection to the string array filename 2002 Prentice Hall. All rights reserved. 20
100 101 if ( reader. Name != String. Empty ) 102 console. Text. Box. Text += reader. Name + "rn"; 103 104 if ( reader. Value != String. Empty ) 105 console. Text. Box. Text += "t" + reader. Value + 106 "rn"; 107 } // end while 108 109 reader. Close(); 110 } // end print. Button_Click Print the Name of the Set copy and tree 111 node if there is one references to null 112 // handle reset. Button click event 113 private void reset. Button_Click( object sender, Print the Value of the 114 System. Event. Args e ) node if there is one 115 { 116 // remove Tree. View nodes 117 if ( tree != null ) 118 xml. Tree. View. Nodes. Remove( tree ); 119 120 xml. Tree. View. Refresh(); // force Tree. View update 121 122 // delete Xml. Document and tree If the Tree. View object 123 copy = null; 124 tree = null; has been created, remove 125 tree from its set of nodes 126 console. Text. Box. Text = ""; // clear text box 127 128 print. Button. Enabled = false; 129 reset. Button. Enabled = false; 130 Force the Tree. View to 131 } // end reset. Button_Click refresh its display 132 Outline Xml. Dom. cs 2002 Prentice Hall. All rights reserved. 21
Outline 133 // construct DOM tree 134 private void Build. Tree( Xml. Node xml. Source. Node, 135 Xml. Node document, Tree. Node tree. Node ) 136 { Reference to current 137 // create Xml. Node. Reader to access XML document location in tree 138 Xml. Node. Reader node. Reader = new Xml. Node. Reader( 139 xml. Source. Node ); 140 Reference to source node 141 // represents current node in DOM tree Reference to empty node 142 Xml. Node current. Node = null; Create an Xml. Node. Reader to which will contain the copy of 143 read from xml. Source. Node the source XML document 144 // tree. Node to add to existing tree 145 Tree. Node new. Node = new Tree. Node(); 146 147 // references modified node type for Create. Node 148 Xml. Node. Type modified. Node. Type; 149 Loop through each 150 while ( node. Reader. Read() ) Xml. Dom. cs node in the tree 151 { 152 // get current node type Create node type containing a copy of 153 modified. Node. Type = node. Reader. Node. Type; the current node. Reader node type 154 155 // check for End. Element, store as Element 156 if ( modified. Node. Type == Xml. Node. Type. End. Element ) Create an Xml. Node via 157 modified. Node. Type = Xml. Node. Type. Element; method Create. Node 158 of Xml. Document 159 // create node copy 160 current. Node = copy. Create. Node( modified. Node. Type, 161 node. Reader. Name, node. Reader. Namespace. URI ); 162 Set the node type to be Xml. Node. Type. Element if it is of type Xml. Node. Type. End. Element 2002 Prentice Hall. All rights reserved. 22
Switch statement to determine Outline node type, create and add node and update DOM tree 23 163 // build tree based on node type 164 switch ( node. Reader. Node. Type ) 165 { 166 // if Text node, add its value to tree 167 case Xml. Node. Type. Text: If a text node then assign 168 new. Node. Text = node. Reader. Value; current node’s value 169 tree. Nodes. Add( new. Node ); 170 171 // append Text node value to current. Node data Downcast current. Node 172 ( ( Xml. Text ) current. Node ). Append. Data( to Xml. Text and append 173 node. Reader. Value ); For element node types, if the 174 document. Append. Child( current. Node ); the node’s value current element contains 175 break; 176 content, add a node containing 177 // if End. Element, move up tree the content as a child to the 178 case Xml. Node. Type. End. Element: current tree node 179 document = document. Parent. Node; 180 tree. Node = tree. Node. Parent; If it is an end element, set Xml. Dom. cs 181 break; document to its Parent. Node 182 property and the current node in the 183 // if new element, add name and traverse tree to its Parent property 184 case Xml. Node. Type. Element: 185 186 // determine if element contains content 187 if ( !node. Reader. Is. Empty. Element ) 188 { 189 // assign node text, add new. Node as child 190 new. Node. Text = node. Reader. Name; 191 tree. Nodes. Add( new. Node ); 192 193 // set tree. Node to last child 194 tree. Node = new. Node; 2002 Prentice Hall. All rights reserved.
Outline 195 196 document. Append. Child( current. Node ); 197 document = document. Last. Child; 198 } 199 else // do not traverse empty elements For all other node types, 200 { 201 // assign Node. Type string to new. Node display the type of the node 202 new. Node. Text = and update the copy 203 node. Reader. Node. Type. To. String(); 204 205 tree. Nodes. Add( new. Node ); 206 document. Append. Child( current. Node ); 207 } Add the current node as a child 208 Expand all the nodes and refresh 209 break; of document and set the Tree. View display 210 document to its last child 211 // all other types, display node type 212 default: Xml. Dom. cs 213 new. Node. Text = node. Reader. Node. Type. To. String(); 214 tree. Nodes. Add( new. Node ); 215 document. Append. Child( current. Node ); 216 break; If the node has no content, set the 217 } // end switch new. Node Text property to be the 218 219 new. Node = new Tree. Node(); type of the node. 220 } // end while Add new. Node to tree. Node and 221 add current. Node to document 222 // update the Tree. View control 223 xml. Tree. View. Expand. All(); 224 xml. Tree. View. Refresh(); 225 226 } // end Build. Tree 227 } // end Xml. Dom 2002 Prentice Hall. All rights reserved. 24
Outline Xml. Dom. cs Program Output 2002 Prentice Hall. All rights reserved. 25
1 // Fig. 18. 9: Path. Navigator. cs 2 // Demonstrates Class XPath. Navigator. 3 4 using System; 5 using System. Windows. Forms; 6 using System. Xml. XPath; // contains XPath. Navigator 7 8 public class Path. Navigator : System. Windows. Form 9 { 10 private System. Windows. Forms. Button first. Child. Button; 11 private System. Windows. Forms. Button parent. Button; 12 private System. Windows. Forms. Button next. Button; 13 private System. Windows. Forms. Button previous. Button; 14 private System. Windows. Forms. Button select. Button; 15 private System. Windows. Forms. Tree. View path. Tree. Viewer; 16 private System. Windows. Forms. Combo. Box select. Combo. Box; 17 private System. Component. Model. Container components = null; 18 private System. Windows. Forms. Text. Box select. Tree. Viewer; 19 private System. Windows. Forms. Group. Box navigate. Box; 20 private System. Windows. Forms. Group. Box locate. Box; 21 22 // navigator to traverse document 23 private XPath. Navigator xpath; 24 25 // references document for use by XPath. Navigator 26 private XPath. Document document; 27 28 // references Tree. Node list used by Tree. View control 29 private Tree. Node tree; 30 31 public Path. Navigator() 32 { 33 Initialize. Component(); 34 Outline 26 Path. Navigator. cs 2002 Prentice Hall. All rights reserved.
Outline 27 35 // load XML document 36 document = new XPath. Document( "sports. xml" ); 37 Initialize Tree. View to 38 // create navigator contain the root element 39 xpath = document. Create. Navigator(); 40 41 // create root node for Tree. Nodes 42 tree = new Tree. Node(); 43 44 tree. Text = xpath. Node. Type. To. String(); // #root 45 path. Tree. Viewer. Nodes. Add( tree ); // add tree 46 47 // update Tree. View control Parse and load sports. xml into document 48 path. Tree. Viewer. Expand. All(); 49 path. Tree. Viewer. Refresh(); 50 path. Tree. Viewer. Selected. Node = tree; // highlight root 51 } // end constructor 52 Path. Navigator. cs Use method Create. Navigator to 53 [STAThread] 54 static void Main() create an XPath. Navigator that 55 { will allow navigation of document 56 Application. Run( new Path. Navigator() ); 57 } 58 59 // traverse to first child 60 private void first. Child. Button_Click( object sender, 61 System. Event. Args e ) 62 { 63 Tree. Node new. Tree. Node; 64 2002 Prentice Hall. All rights reserved.
Outline 28 65 // move to first child 66 if ( xpath. Move. To. First. Child() ) 67 { 68 new. Tree. Node = new Tree. Node(); // create new node 69 70 // set node's Text property to either 71 // navigator's name or value If the node has no children 72 Determine. Type( new. Tree. Node, xpath ); display error message 73 74 // add node to Tree. Node node list If move to parent was successful 75 tree. Nodes. Add( new. Tree. Node ); (via method Move. To. Parent) 76 tree = new. Tree. Node; // assign tree new. Tree. Node 77 If move to first child was Set count to the number of 78 // update Tree. View control successful (via method 79 path. Tree. Viewer. Expand. All(); children the node has Move. To. First. Child) 80 path. Tree. Viewer. Refresh(); 81 path. Tree. Viewer. Selected. Node = tree; 82 } Path. Navigator. cs 83 else // node has no children 84 Message. Box. Show( "Current Node has no children. " , 85 "", Message. Box. Buttons. OK, 86 Message. Box. Icon. Information ); Call method Determine. Type to 87 } set the new node’s Text property to 88 either the navigator’s name (if an 89 // traverse to node's parent on parent. Button click event 90 private void parent. Button_Click( object sender, element) or value (otherwise) 91 System. Event. Args e ) 92 { 93 // move to parent Add the new child node to 94 if ( xpath. Move. To. Parent() ) tree’s node collection and set 95 { tree to reference the child node 96 tree = tree. Parent; 97 98 // get number of child nodes, not including sub trees 2002 Prentice Hall. 99 int count = tree. Get. Node. Count( false ); All rights reserved.
Outline 29 100 101 // remove all children 102 tree. Nodes. Clear(); 103 104 // update Tree. View control 105 path. Tree. Viewer. Expand. All(); 106 path. Tree. Viewer. Refresh(); 107 path. Tree. Viewer. Selected. Node = tree; 108 } Iterate through loop count time and 109 else // if node has no parent (root node) 110 Message. Box. Show( "Current node has no parent. " , "", remove the first node (accessible via 111 Message. Box. Buttons. OK, First. Node property), thus removing 112 Message. Box. Icon. Information ); all the node’s children 113 } 114 If move to next sibling is successful 115 // find next sibling on next. Button click event (via method Move. To. Next) 116 private void next. Button_Click( object sender, Create new node for the sibling 117 System. Event. Args e ) and add it to the parent node Path. Navigator. cs 118 { Retrieve reference to parent node 119 Tree. Node new. Tree. Node = null, new. Node = null; 120 121 // move to next sibling 122 if ( xpath. Move. To. Next() ) 123 { 124 new. Tree. Node = tree. Parent; // get parent node 125 126 new. Node = new Tree. Node(); // create new node 127 Determine. Type( new. Node, xpath ); 128 new. Tree. Nodes. Add( new. Node ); 129 130 // set current position for display 131 tree = new. Node; 132 2002 Prentice Hall. All rights reserved.
Outline 30 133 // update Tree. View control 134 path. Tree. Viewer. Expand. All(); 135 path. Tree. Viewer. Refresh(); 136 path. Tree. Viewer. Selected. Node = tree; 137 } 138 else // node has no additional siblings 139 Message. Box. Show( "Current node is last sibling. " , 140 "", Message. Box. Buttons. OK, 141 Message. Box. Icon. Information ); 142 } // end next. Button_Click If move to previous sibling is successful 143 (via method Move. To. Previous) 144 // get previous sibling on previous. Button click 145 private void previous. Button_Click( object sender, 146 System. Event. Args e ) Retrieve reference to parent node 147 { 148 Tree. Node parent. Tree. Node = null; Remove current node (the last child) 149 150 // move to previous sibling Set tree to reference the last child Path. Navigator. cs 151 if ( xpath. Move. To. Previous() ) 152 { 153 parent. Tree. Node = tree. Parent; // get parent node 154 155 // delete current node 156 parent. Tree. Nodes. Remove( tree ); 157 158 // move to previous node 159 tree = parent. Tree. Node. Last. Node; 160 161 // update Tree. View control 162 path. Tree. Viewer. Expand. All(); 163 path. Tree. Viewer. Refresh(); 164 path. Tree. Viewer. Selected. Node = tree; 165 } 2002 Prentice Hall. All rights reserved.
Outline 31 166 else // if current node has no previous siblings 167 Message. Box. Show( "Current node is first sibling. " , 168 "", Message. Box. Buttons. OK, 169 Message. Box. Icon. Information ); Call method Display. Iterator 170 } // end previous. Button_Click 171 to update select. Tree. Viewer 172 // process select. Button click event Set the XPath. Node. Iterator to be 173 private void select. Button_Click( object sender, the iterator for the node with the text 174 System. Event. Args e ) the user selected from the Combo. Box 175 { 176 XPath. Node. Iterator iterator ; // enables node iteration 177 178 // get specified node from Combo. Box 179 try 180 { 181 iterator = xpath. Select( select. Combo. Box. Text ); 182 Display. Iterator ( iterator ); // print selection 183 } Path. Navigator. cs 184 185 // catch invalid expressions 186 catch ( System. Argument. Exception argument. Exception ) 187 { 188 Message. Box. Show( argument. Exception. Message, 189 "Error", Message. Box. Buttons. OK, 190 Message. Box. Icon. Error ); 191 } 192 } // end select. Button_Click 193 194 // print values for XPath. Node. Iterator 195 private void Display. Iterator( XPath. Node. Iterator iterator ) 196 { 197 select. Tree. Viewer. Text = ""; 198 2002 Prentice Hall. All rights reserved.
199 // prints selected node's values 200 while ( iterator. Move. Next() ) 201 select. Tree. Viewer. Text += 202 iterator. Current. Value. Trim() 203 + "rn"; If the node is an element, set the 204 } // end Display. Iterator 205 Text property of node to 206 // determine if Tree. Node should display current node contain the name of the element 207 // name or value 208 private void Determine. Type( Tree. Node node, 209 XPath. Navigator x. Path ) If the node type is anything 210 { else, set the Text property of 211 // determine Node. Type 212 switch ( x. Path. Node. Type ) node to the Value of the 213 { XPath. Navigator 214 // if Element, get its name 215 case XPath. Node. Type. Element: 216 217 // get current node name, and remove whitespace 218 node. Text = x. Path. Name. Trim(); 219 break; 220 221 // obtain node values 222 default: 223 224 // get current node value and remove whitespace 225 node. Text = x. Path. Value. Trim(); 226 break; 227 228 } // end switch 229 } // end Determine. Type 230 } // end Path. Navigator Outline 32 Path. Navigator. cs 2002 Prentice Hall. All rights reserved.
Outline 33 Path. Navigator. cs Program Output 2002 Prentice Hall. All rights reserved.
Outline 34 Path. Navigator. cs Program Output 2002 Prentice Hall. All rights reserved.
1 <? xml version = "1. 0"? > 2 3 <!-- Fig. 18. 10: games. xml --> 4 <!-- Sports Database --> 5 6 <sports> 7 8 <game id = "783"> 9 <name>Cricket</name> 10 11 <paragraph> 12 More popular among commonwealth nations. 13 </paragraph> 14 </game> 15 16 <game id = "239"> 17 <name>Baseball</name> 18 19 <paragraph> 20 More popular in America. 21 </paragraph> 22 </game> 23 24 <game id = "418"> 25 <name>Soccer(Futbol)</name> 26 <paragraph>Most popular sport in the world </paragraph> 27 </game> 28 </sports> Outline games. xml 2002 Prentice Hall. All rights reserved. 35
36 18. 4 Document Object Model (DOM) 2002 Prentice Hall. All rights reserved.
18. 5 Document Type Definitions (DTDs), Schemas and Validation • Optional documents to present proper XML structure 2002 Prentice Hall. All rights reserved. 37
38 18. 5. 1 Document Type Definition • Provide means for type checking XML documents – Check validity of document – Uses EBNF grammar – Require validating parsers 2002 Prentice Hall. All rights reserved.
Outline 1 <!-- Fig. 18. 12: letter. dtd --> 2 <!-- DTD document for letter. xml --> 3 Keyword EMPTY specifies that the 4 <!ELEMENT letter ( contact+, salutation, paragraph+, 5 closing, signature )> element cannot contain character 6 data (EMPTY elements are 7 <!ELEMENT contact ( name, address 1, address 2, city, state, commonly used for their attributes) 8 zip, phone, flag )> 9 <!ATTLIST contact type CDATA #IMPLIED> 10 11 <!ELEMENT name ( #PCDATA )> 12 <!ELEMENT address 1 ( #PCDATA )> 13 <!ELEMENT address 2 ( #PCDATA )> 14 <!ELEMENT city ( #PCDATA )> This ELEMENT element type 15 <!ELEMENT state ( #PCDATA )> declaration defines the rules 16 <!ELEMENT zip ( #PCDATA )> for an element letter 17 <!ELEMENT phone ( #PCDATA )> 18 <!ELEMENT flag EMPTY> letter. dtd 19 <!ATTLIST flag gender (M | F) "M"> Element letter is defined to have one or more 20 contact elements, one salutation element, 21 <!ELEMENT salutation ( #PCDATA )> one or more paragraph elements, a closing 22 <!ELEMENT closing ( #PCDATA )> 23 <!ELEMENT paragraph ( #PCDATA )> element and a signature element 24 <!ELEMENT signature ( #PCDATA )> Definition for the contact element Flag #PCDATA specifies This ATTLIST element type declaration defines the type attribute for contact elements. that the element consists The type attribute is of type CDATA (string not processed by of parsed character data the parser but passed to the application). Flag #IMPLIED (text with no markup) specifies that the type attribute is not required. 2002 Prentice Hall. All rights reserved. 39
1 <? xml version = "1. 0"? > 2 3 <!-- Fig. 18. 13: letter 2. xml --> 4 <!-- Business letter formatted with XML --> 5 6 <!DOCTYPE letter SYSTEM "letter. dtd"> 7 8 <letter> 9 <contact type = "from"> 10 <name>Jane Doe</name> 11 <address 1>Box 12345</address 1> 12 <address 2>15 Any Ave. </address 2> 13 <city>Othertown</city> 14 <state>Otherstate</state> 15 <zip>67890</zip> 16 <phone>555 -4321</phone> 17 <flag gender = "F" /> 18 </contact> Specifies the location of the DTD 19 file and the element (letter) to 20 <contact type = "to"> 21 <name>John Doe</name> which the DTD is applied 22 <address 1>123 Main St. </address 1> 23 <address 2></address 2> 24 <city>Anytown</city> 25 <state>Anystate</state> 26 <zip>12345</zip> 27 <phone>555 -1234</phone> 28 <flag gender = "M" /> 29 </contact> 30 Outline letter 2. xml 2002 Prentice Hall. All rights reserved. 40
31 <salutation>Dear Sir: </salutation> 32 33 <paragraph>It is our privilege to inform you about our new 34 database managed with XML. This new system 35 allows you to reduce the load on your inventory list 36 server by having the client machine perform the work of 37 sorting and filtering the data. 38 </paragraph> 39 40 <paragraph>Please visit our Web site for availability 41 and pricing. 42 </paragraph> 43 <closing>Sincerely</closing> 44 <signature>Ms. Doe</signature> 45 </letter> Outline letter 2. xml 2002 Prentice Hall. All rights reserved. 41
18. 5 Document Type Definitions (DTDs) Schemas and Validation Fig. 18. 14 XML Validator used to validate an XML document. 2002 Prentice Hall. All rights reserved. 42
43 18. 5. 2 Microsoft XML Schemas • Alternative to DTDs • Require validating parsers • Do not use EBNF grammar 2002 Prentice Hall. All rights reserved.
18. 5 Document Type Definitions (DTDs) Schemas and Validation Fig. 18. 15 XML Validator displaying an error message. 2002 Prentice Hall. All rights reserved. 44
1 <? xml version = "1. 0"? > 2 3 <!-- Fig. 18. 16: bookxdr. xml --> 4 <!-- XML file that marks up book data --> 5 6 <books xmlns = "x-schema: book. xdr"> 7 <book> 8 <title>C# How to Program</title> 9 </book> 10 11 <book> 12 <title>Java to Program, 4/e</title> 13 </book> 14 15 <book> 16 <title>Visual Basic. NET How to Program </title> 17 </book> 18 19 <book> 20 <title>Advanced Java 2 Platform How to Program </title> 21 </book> 22 23 <book> 24 <title>Python How to Program</title> 25 </book> 26 </books> Outline bookxdr. xml 2002 Prentice Hall. All rights reserved. 45
1 <? xml version = "1. 0"? > 2 3 <!-- Fig. 18. 17: book. xdr --> 4 <!-- Schema document to which book. xml conforms --> 5 6 <Schema xmlns = "urn: schemas-microsoft-com: xml-data" > 7 <Element. Type name = "title" content = "text. Only" 8 model = "closed" /> 9 10 <Element. Type name = "book" content = "elt. Only" model = "closed"> 11 <element type = "title" min. Occurs = "1" max. Occurs = "1" /> 12 </Element. Type> 13 14 <Element. Type name = "books" content = "elt. Only" model = "closed"> 15 <element type = "book" min. Occurs = "0" max. Occurs = "*" /> 16 </Element. Type> 17 </Schema> Setting the model attribute to Outline “closed” specifies that a conforming book. xdr XML document can contain only Namespace URI used by elements defined in this Schema Microsoft Schemas Keyword Schema signifies the Attributes min. Occurs and beginning of the schema markup max. Occurs are set to 1, indicating Element. Type used that the title element must occur to define element title exactly once in a book element The element within the Element. Type element specifies that title is a Setting content to “elt. Only” The content attribute specifies child element of book specifies that this element can contain that this element is text-only Indicates that a books element can mixed content (text and other elements) have any number of book elements 2002 Prentice Hall. All rights reserved. 46
47 18. 5. 3 W 3 C XML Schema • Schema created by W 3 C • Only a recommendation 2002 Prentice Hall. All rights reserved.
1 <? xml version = "1. 0"? > 2 3 <!-- Fig. 18: bookxsd. xml --> 4 <!-- Document that conforms to W 3 C XML Schema --> 5 6 <deitel: books xmlns: deitel = "http: //www. deitel. com/booklist"> 7 <book> 8 <title>e-Business and e-Commerce How to Program </title> 9 </book> 10 <book> 11 <title>Python How to Program</title> 12 </book> 13 </deitel: books> Outline bookxsd. xml 2002 Prentice Hall. All rights reserved. 48
Outline 1 <? xml version = "1. 0"? > 2 W 3 C XML Schema uses namespace URI 3 <!-- Fig. 18. 19: book. xsd --> http: //www. w 3. org/2001/XMLSchema 4 <!-- Simple W 3 C XML Schema document --> 5 6 <xsd: schema xmlns: xsd = "http: //www. w 3. org/2001/ XMLSchema" 7 xmlns: deitel = "http: //www. deitel. com/booklist" Namespace for elements and 8 target. Namespace = "http: //www. deitel. com/booklist"> attributes that this schema defines 9 10 <xsd: element name = "books" type = "deitel: Books. Type"/> 11 An example of 12 <xsd: complex. Type name = "Books. Type"> 13 <xsd: sequence> complex type 14 <xsd: element name = "book" type = "deitel: Book. Type" 15 min. Occurs = "1" max. Occurs = "unbounded"/> 16 </xsd: sequence> Simple type prohibited from containing 17 </xsd: complex. Type> 18 attributes and child element book. xsd 19 <xsd: complex. Type name = "Book. Type"> 20 <xsd: sequence> 21 <xsd: element name = "title" type = "xsd: string"/> 22 </xsd: sequence> 23 </xsd: complex. Type> 24 25 </xsd: schema> 2002 Prentice Hall. All rights reserved. 49
50 18. 5. 4 Schema Validation in C# • Use instance of Xml. Validating. Reader to perform validation 2002 Prentice Hall. All rights reserved.
Outline 51 1 // Fig. 18. 20: Validation. Test. cs 2 // Validating XML documents against Schemas. 3 4 using System; 5 using System. Windows. Forms; 6 using System. Xml; 7 using System. Xml. Schema; // contains Schema classes 8 Instantiate an Xml. Schema. Collection schemas 9 // determines XML document Schema validity 10 public class Validation. Test : System. Windows. Form 11 { 12 private System. Windows. Forms. Combo. Box files. Combo. Box; Use method Add to add a schema named 13 private System. Windows. Forms. Button validate. Button; book located in book. xdr to schema 14 private System. Windows. Forms. Label console. Label; 15 private System. Component. Model. Container components = null; 16 17 private Xml. Schema. Collection schemas; // Schemas Validation. Test. c 18 private bool valid; // validation result 19 s 20 public Validation. Test() 21 { 22 Initialize. Component(); 23 24 valid = true; // assume document is valid 25 26 // get Schema(s) for validation Add a W 3 C XML Schema 27 schemas = new Xml. Schema. Collection(); 28 schemas. Add( "book", "book. xdr" ); 29 schemas. Add( “http: //www. deitel. com/booklist", "book. xsd" ); 30 } // end constructor 31 32 // Visual Studio. NET generated code 33 2002 Prentice Hall. All rights reserved.
Outline 52 34 [STAThread] 35 static void Main() 36 { 37 Application. Run( new Validation. Test() ); 38 } // end Main 39 40 // handle validate. Button click event 41 private void validate. Button_Click( object sender, 42 System. Event. Args e ) 43 { Create Xml. Reader for the 44 // get XML document file that the user selected 45 Xml. Text. Reader reader = 46 new Xml. Text. Reader( files. Combo. Box. Text ); from files. Combo. Box 47 48 // get validator 49 Xml. Validating. Reader validator = 50 new Xml. Validating. Reader( reader ); Validation. Test. c 51 Adds the Schema collection 52 // assign Schema(s) s referenced by Schemas to 53 validator. Schemas. Add( schemas ); 54 the Schemas property 55 // Microsoft XDR validation 56 validator. Validation. Type = Validation. Type. XDR; 57 58 // register event handler for validation error(s) 59 validator. Validation. Event. Handler += 60 new Validation. Event. Handler( Validation. Error ); 61 62 // validate document node-by-node 63 while ( validator. Read() ) ; // empty body Each call to Read validates 64 65 // check validation result the next node in the document 66 if ( valid ) 67 console. Label. Text = "Document is valid"; 2002 Prentice Hall. 68 All rights reserved.
69 valid = true; // reset variable 70 71 // close reader stream 72 validator. Close(); 73 } // end validate. Button_Click 74 75 // event handler for validation error 76 private void Validation. Error( object sender, 77 Validation. Event. Args arguments ) 78 { 79 console. Label. Text = arguments. Message; 80 valid = false; // validation failed 81 } // end Validation. Error 82 } // end Validation. Test Outline 53 Called if document is invalid or an error occurs Validation. Test. c s Program Output 2002 Prentice Hall. All rights reserved.
Outline 1 <? xml version = "1. 0"? > 2 3 <!-- Fig. 18. 22: bookxsdfail. xml --> 4 <!-- Document that does not conforms to W 3 C Schema --> 5 6 <deitel: books xmlns: deitel = "http: //www. deitel. com/booklist"> 7 <book> 8 <title>e-Business and e-Commerce How to Program </title> 9 <title>C# How to Program</title> Bookxsdfail. cs 10 </book> Extra title element in book 11 <book> invalidate the document 12 <title>Python How to Program</title> 13 </book> 14 </deitel: books> Program output 2002 Prentice Hall. All rights reserved. 54
Outline 1 <? xml version = "1. 0"? > 2 3 <!-- Fig. 18. 22: bookxdrfail. xml --> 4 <!-- XML file that does not conform to Schema book. xdr --> 5 6 <books xmlns = "x-schema: book. xdr"> bookxdrfail. xml 7 <book> 8 <title>XML How to Program</title> 9 </book> 10 11 <book> Too many titles causes 12 <title>Java How to Program, 4/e </title> document to be invalid 13 </book> 14 15 <book> 16 <title>Visual Basic. NET How to Program </title> 17 </book> 18 19 <book> 20 <title>C++ How to Program, 3/e</title> 21 <title>Python How to Program</title> 22 </book> 23 24 <book> 25 <title>C# How to Program</title> 26 </book> 27 </books> Program output 2002 Prentice Hall. All rights reserved. 55
18. 6 Extensible Stylesheet Language and Xsl. Transform • Extensible Stylesheet Language – Formatting XML data – XLS Transformations • Creates formatted text-based documents – Source tree – Result tree 2002 Prentice Hall. All rights reserved. 56
Outline 1 <? xml version = "1. 0"? > 2 Specify name and location Application-specific information 3 <!-- Fig. 18. 23: sorting. xml --> Processing instruction target of style sheet to apply embedded into XML document 4 <!-- XML document containing book information --> 5 6 <? xml: stylesheet type = "text/xsl" href = "sorting. xsl"? > 7 8 <book isbn = "999 -99999 -9 -X"> Processing instruction value Processing instruction 9 <title>Deitel' s XML Primer</title> 10 11 <author> 12 <first. Name>Paul</first. Name> 13 <last. Name>Deitel</last. Name> 14 </author> 15 16 <chapters> 17 <front. Matter> 18 <preface pages = "2" /> Sorting. xml 19 <contents pages = "5" /> 20 <illustrations pages = "4" /> 21 </front. Matter> 22 23 <chapter number = "3" pages = "44"> 24 Advanced XML</chapter> 25 26 <chapter number = "2" pages = "35"> 27 Intermediate XML </chapter> 28 29 <appendix number = "B" pages = "26"> 30 Parsers and Tools </appendix> 31 2002 Prentice Hall. All rights reserved. 57
32 <appendix number = "A" pages = "7"> 33 Entities</appendix> 34 35 <chapter number = "1" pages = "28"> 36 XML Fundamentals </chapter> 37 </chapters> 38 39 <media type = "CD" /> 40 </book> Outline Sorting. xml 2002 Prentice Hall. All rights reserved. 58
XSL document is an XML document 1 <? xml version = "1. 0"? > 2 3 <!-- Fig. 18. 24: sorting. xsl --> 4 <!-- Transformation of book information into XHTML --> Root element 5 6 <xsl: stylesheet version = "1. 0" 7 xmlns: xsl = "http: //www. w 3. org/1999/XSL/Transform" > 8 9 <!-- write XML declaration and DOCTYPE DTD information --> 10 <xsl: output method = "xml" omit-xml-declaration = "no" 11 doctype-system = 12 "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -strict. dtd" 13 doctype-public = "-//W 3 C//DTD XHTML 1. 0 Strict//EN" /> 14 15 <!-- match document root --> 16 <xsl: template match = "/"> 17 <html xmlns = "http: //www. w 3. org/1999/ xhtml"> 18 <xsl: apply-templates/> Calls for all template that 19 </html> 20 </xsl: template> match children of the 21 document root to be applied 22 <!-- match book --> Specifies a template that 23 <xsl: template match = "book"> Title for XHTML document matches element book 24 <head> 25 <title>ISBN <xsl: value-of select = "@isbn" /> - 26 <xsl: value-of select = "title" /></title> 27 </head> 28 29 <body> 30 <h 1 style = "color: blue"> 31 <xsl: value-of select = "title"/></h 1> 32 Outline Write document type declaration to the result tree Sorting. xsl 2002 Prentice Hall. All rights reserved. 59
Outline 33 <h 2 style = "color: blue">by <xsl: value-of 34 select = "author/last. Name" />, 35 <xsl: value-of select = "author/first. Name" /></h 2> 36 37 <table style = 38 "border-style: groove; background-color: wheat" > 39 40 <xsl: for-each select = "chapters/front. Matter/*"> 41 <tr> 42 <td style = "text-align: right"> Select each element that is a 43 <xsl: value-of select = "name()" /> child of element front. Matter 44 </td> name called to retrieve 45 Node-set function current node’s element name 46 <td> 47 ( <xsl: value-of select = "@pages" /> pages ) Attribute selects value of context 48 </td> node chapter’s attribute number Sort chapters by number 49 </tr> 50 </xsl: for-each> in ascending order Sorting. xsl 51 52 <xsl: for-each select = "chapters/chapter"> 53 <xsl: sort select = "@number" data-type = "number" 54 order = "ascending" /> Specify numeric search 55 <tr> 56 <td style = "text-align: right"> 57 Chapter <xsl: value-of select = "@number" /> 58 </td> 59 60 <td> 61 ( <xsl: value-of select = "@pages" /> pages ) 62 </td> 63 </tr> 64 </xsl: for-each> 65 2002 Prentice Hall. All rights reserved. 60
Outline 66 <xsl: for-each select = "chapters/appendix"> 67 <xsl: sort select = "@number" data-type = "text" 68 order = "ascending" /> 69 <tr> 70 <td style = "text-align: right"> 71 Appendix <xsl: value-of select = "@number" /> 72 </td> 73 74 <td> 75 ( <xsl: value-of select = "@pages" /> pages ) 76 </td> 77 </tr> 78 </xsl: for-each> 79 </table> XSL variable to store value of book’s 80 page count and output to result tree 81 <br /><p style = "color: blue"> Pages: 82 <xsl: variable name = "pagecount" 83 select = "sum(chapters//*/@pages)" /> Sorting. xsl 84 <xsl: value-of select = "$pagecount" /> 85 <br />Media Type: 86 <xsl: value-of select = "media/@type" /></p> 87 </body> 88 </xsl: template> 89 90 </xsl: stylesheet> 2002 Prentice Hall. All rights reserved. 61
Outline sorting. xsl Program Output 2002 Prentice Hall. All rights reserved. 62
Outline 1 // Fig. 18. 25: Transform. Test. cs 2 // Applying a style sheet to an XML document. 3 4 using System; 5 using System. Windows. Forms; 6 using System. Xml; 7 using System. Xml. XPath; // contains XPath classes 8 using System. Xml. Xsl; // contains style sheet classes 9 using System. IO; // contains stream classes 10 11 // transforms XML document to XHTML 12 public class Transform. Test : System. Windows. Form 13 { 14 private System. Windows. Forms. Text. Box console. Text. Box; 15 private System. Windows. Forms. Button transform. Button; 16 private System. Component. Model. Container components = null; 17 18 private Xml. Document document; // Xml document root Transform. Test. cs 19 private XPath. Navigator navigator; // navigate document 20 private Xsl. Transform transformer; // transform document 21 private String. Writer output; // display document Necessary for transforming 22 23 public Transform. Test() XML data to another format 24 { 25 Initialize. Component (); 26 27 // load XML data 28 document = new Xml. Document(); XML document parsed 29 document. Load( ". . \sports. xml" ); and loaded into memory 30 Create an Xpath. Navigator 31 // create navigator object to navigate document 32 navigator = document. Create. Navigator(); 33 through transformation 2002 Prentice Hall. All rights reserved. 63
Style sheet sports. xsl to be used on sports. xml Outline 34 // load style sheet 35 transformer = new Xsl. Transform(); 36 transformer. Load( ". . \sports. xsl" ); 37 } // end constructor 38 39 // Windows Form Designer generated code 40 41 [STAThread] 42 static void Main() 43 { 44 Application. Run( new Transform. Test() ); 45 } // end Main 46 47 // transform. Button click event 48 private void transform. Button_Click( object sender, Result of transformation An XPath. Navigator created from An instance of class 49 System. Event. Args e ) stored in String. Writer 50 { sports. xml’s Xml. Document Xslt. Argument. List object referenced by output 51 // transform XML data Transform. Test. cs 52 output = new String. Writer(); 53 transformer. Transform( navigator, null, output ); To apply style sheet 54 55 // display transformation in text box Method Transform of 56 console. Text. Box. Text = output. To. String(); class Xsl. Transform 57 58 // write transformation result to disk 59 File. Stream stream = new File. Stream( ". . \sports. html", 60 File. Mode. Create ); Write output to a file 61 Stream. Writer writer = new Stream. Writer( stream ); 62 writer. Write( output. To. String() ); 63 64 // close streams 65 writer. Close(); 66 output. Close(); 67 } // end transform. Button_Click 2002 Prentice Hall. 68 } // end Transform. Test All rights reserved. 64
Outline 65 Transform. Test. cs Program Output Result rendered in IE 2002 Prentice Hall. All rights reserved.
66 18. 7 Microsoft Biz. Talk • To facilitate flow of information – Between different businesses – XML-based technology • No problem for different platform or application – Biz. Talk Server • Parses and translate messages – Biz. Talk Framework • Schema structure – Biz. Talk Schema Library • Collection of framework 2002 Prentice Hall. All rights reserved.
67 18. 7 Microsoft Biztalk 2002 Prentice Hall. All rights reserved.
Outline 1 <? xml version = "1. 0"? > All Biz. Talk documents have this root 2 <Biz. Talk xmlns = 3 "urn: schemas-biztalk-org: Biz. Talk/biztalk-0. 81. xml"> 4 5 <!-- Fig. 18. 27: biztalkmarkup. xml --> Default namespace for 6 <!-- Example of standard Biz. Talk markup --> Biz. Talk framework element 7 Contain routing information 8 <Route> 9 <From location. ID = "8888888" location. Type = "DUNS" Specify type of business 10 handle = "23" /> that sends or receive the 11 Indicate source and information 12 <To location. ID = "454545445" location. Type = "DUNS" destination, respectively 13 handle = "45" /> 14 </Route> Provide information to 15 Specify the unique 16 <Body> routing applications identifier for a business 17 <Offers xmlns = 18 "x. Biztalkmarkup. xml schema: http: //schemas. biztalk. org/eshop_msn_com/t 7 ntoqnq. xml"> 19 <Offer> 20 <Model>12 -a-3411 d</Model> 21 <Manufacturer> Ex. Comp, Inc. </Manufacturer> 22 <Manufacturer. Model>DCS-48403</Manufacturer. Model> 23 24 <Merchant. Category> 25 Clothes | Sports wear Recognizable 26 </Merchant. Category> business-related 27 28 <MSNClass. Id></MSNClass. Id> elements 29 30 <Start. Date>2001 -06 -05 T 13: 12: 00</Start. Date> 31 <End. Date>2001 -12 -05 T 13: 12: 00</End. Date> 32 33 <Regular. Price>89. 99</Regular. Price> 2002 Prentice Hall. 34 <Current. Price>25. 99</Current. Price> All rights reserved. 68
35 <Display. Price value = "3" /> 36 <In. Stock value = "15" /> 37 38 <Reference. Image. URL> 39 http: //www. Example. com/clothes/index. jpg 40 </Reference. Image. URL> 41 42 <Offer. Name>Clearance sale</Offer. Name> 43 44 <Offer. Description> 45 This is a clearance sale 46 </Offer. Description> 47 48 <Promotional. Text>Free Shipping</Promotional. Text> 49 50 <Comments> 51 Clothes that you would love to wear. 52 </Comments> 53 54 <Icon. Type value = "Buy. Now" /> 55 56 <Action. URL> 57 http: //www. example. com/action. htm 58 </Action. URL> 59 60 <Age. Group 1 value = "Infant" /> 61 <Age. Group 2 value = "Adult" /> 62 63 <Occasion 1 value = "Birthday" /> 64 <Occasion 2 value = "Anniversary" /> 65 <Occasion 3 value = "Christmas" /> 66 67 </Offer> 68 </Offers> 69 </Body> </Biz. Talk> Outline Biztalkmarkup. xml 2002 Prentice Hall. All rights reserved. 69
- Slides: 69