04 sm3 xml_xp_08

372 views
298 views

Published on

Published in: Technology
2 Comments
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
372
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
0
Comments
2
Likes
0
Embeds 0
No embeds

No notes for slide

04 sm3 xml_xp_08

  1. 1. Accessing XML Documents Using DOMObjectivesIn this lesson, you will learn to: Use XML DOM objects to perform the following tasks: Validate an XML document against an XML schema Provide user-selected view of data stored in an XML document©NIIT eXtensible Markup Language/Lesson 8/Slide 1 of 23
  2. 2. Accessing XML Documents Using DOMProblem Statement 8.D.1 The head office of CyberShoppe receives data in the form of XML documents from its branches. In order to ensure consistency of data sent by the branches, the head office maintains the definitions for structures of documents in schemas. On receiving data from branches, the head office needs to verify that the data conforms to the schema for the respective documents. For this, they need to write a script, which validates the data stored in an XML document against a schema. ©NIIT eXtensible Markup Language/Lesson 8/Slide 2 of 23
  3. 3. Accessing XML Documents Using DOMTask List Identify a mechanism to validate an XML document against a schema. Identify the objects required to access an XML document through a scripting language. Write the code to validate an XML document against the schema. Execute the code.©NIIT eXtensible Markup Language/Lesson 8/Slide 3 of 23
  4. 4. Accessing XML Documents Using DOMTask 1: Identify a mechanism to validate an XMLdocument against a schema. In order to validate an XML document, you need to use a validating parser. MSXML is a DOM-based validating parser. Result You can use the XML DOM objects provided by MSXML in order to ensure that the structure of the XML document conforms to the schema. ©NIIT eXtensible Markup Language/Lesson 8/Slide 4 of 23
  5. 5. Accessing XML Documents Using DOMTask 2: Identify the objects required to access anXML document through a scripting language. You can use the DOMDocument object to load an XML document, parse it, and validate it. You can use the properties of IXMLDOMParseError object to display appropriate messages in case of an error. You require another object called XMLSchemaCache to load the schema document associated with the XML document. ©NIIT eXtensible Markup Language/Lesson 8/Slide 5 of 23
  6. 6. Accessing XML Documents Using DOMTask 2: Identify the … scripting language.(Contd.)The XMLSchemaCache Object The XMLSchemaCache object is used to hold a collection of schema documents that specify the rules to which XML documents must conform. ©NIIT eXtensible Markup Language/Lesson 8/Slide 6 of 23
  7. 7. Accessing XML Documents Using DOM Task 2: Identify the … scripting language. (Contd.) The following table describes some methods provided by this object: Method Descriptionadd(namespaceURI, variable) This method adds a new schema to the collection and also associates the specified namespaceURI with the schema.addCollection(XMLSchemaColle This method adds schemas from other schema collections. It alsoction object) ensures that the namespaceURIs of the different schemas do not clash.get(namespaceURI) This method returns a node that contains the schema element.namespaceURI(index number) This method returns the namespace that is associated with the specified index number.remove(namespaceURI) This method removes a schema from the collection. ©NIIT eXtensible Markup Language/Lesson 8/Slide 7 of 23
  8. 8. Accessing XML Documents Using DOMTask 2: Identify the … scripting language. (Contd.)Result You can use the following XML DOM objects to validate the XML document against a schema: DOMDocument IXMLDOMParseError XMLSchemaCache ©NIIT eXtensible Markup Language/Lesson 8/Slide 8 of 23
  9. 9. Accessing XML Documents Using DOMTask 3: Write the code to validate the XMLdocument against the schema. To write a script that validates an XML document against the schema, you need to follow the steps given below: Create the user interface to accept the name of the XML document and the XML schema. Write the code to load the XML document. Write the code to add the XML schema in the XMLSchemaCache object. Write the code to validate the XML document against the schema. ©NIIT eXtensible Markup Language/Lesson 8/Slide 9 of 23
  10. 10. Accessing XML Documents Using DOMTask 4: Execute the code. ©NIIT eXtensible Markup Language/Lesson 8/Slide 10 of 23
  11. 11. Accessing XML Documents Using DOMJust a Minute…Write the code to add an XML schema called“Products.xsd” to a schema collection. ©NIIT eXtensible Markup Language/Lesson 8/Slide 11 of 23
  12. 12. Accessing XML Documents Using DOMProblem Statement 8.D.2 CyberShoppe sells its products through an e-commerce Web site. Details about the products sold at CyberShoppe need to be displayed. Product details include product name, description, price, and quantity for all products. A customer can choose to view the product details either as a table or as a bulleted list. ©NIIT eXtensible Markup Language/Lesson 8/Slide 12 of 23
  13. 13. Accessing XML Documents Using DOMTask List Identify a mechanism to access an XML document programmatically. Identify the objects required to apply the style sheet to the XML document during runtime. Create the XML document. Create the style sheets. Write the code to apply a style sheet dynamically to an XML document. Execute the code.©NIIT eXtensible Markup Language/Lesson 8/Slide 13 of 23
  14. 14. Accessing XML Documents Using DOMTask 1: Identify a mechanism to access an XMLdocument programmatically.Result In this scenario, the customer can choose to view the product details as a table or as a list. Therefore, you require two style sheets. One of these style sheets needs to be applied to the XML document during runtime. Therefore, XML DOM can be used to access the XML document containing product details. You can also use XML DOM to dynamically apply an XSLT style sheet to an XML document. ©NIIT eXtensible Markup Language/Lesson 8/Slide 14 of 23
  15. 15. Accessing XML Documents Using DOMTask 2: Identify the objects required to apply the stylesheet to the XML document during runtime.The XSLTemplate Object The XSLTemplate object is the DOM object that is used to access an XSLT style sheet. This object is used to hold a cached style sheet that can then be dynamically associated with an XML document. Before a style sheet document can be applied to an XML document, it is converted into a tree structure by the parser. The XSLT tree structure is loaded into the memory of the computer and used to process the XML document. This is because the XSLTemplate object stores the complied XSLT document. Therefore, you must first create an XSLTemplate object. ©NIIT eXtensible Markup Language/Lesson 8/Slide 15 of 23
  16. 16. Accessing XML Documents Using DOMTask 2: Identify the objects … runtime.(Contd.)The XSLProcessor Object To process an XML document by using a style sheet, you must create an instance of the XSLProcessor object. This object is used to apply a style sheet to an XML document and then process that document. The XSLProcessor object is supported only in IE 5.0 and later. ©NIIT eXtensible Markup Language/Lesson 8/Slide 16 of 23
  17. 17. Accessing XML Documents Using DOMTask 2: Identify the objects … runtime.(Contd.)Result In the given scenario, the product details contained in the XML document need to be displayed either as a table or as a list by using an XSLT style sheet. This can be done using the XSLTemplate and XSLProcessor objects. ©NIIT eXtensible Markup Language/Lesson 8/Slide 17 of 23
  18. 18. Accessing XML Documents Using DOMTask 3: Create the XML document.Task 4: Create the style sheets.Task 5: Write the code to apply a style sheetdynamically to an XML document.Task 6: Execute the code. ©NIIT eXtensible Markup Language/Lesson 8/Slide 18 of 23
  19. 19. Accessing XML Documents Using DOMJust a Minute…The following JavaScript code is used to createXSLTemplate and DOMDocument objects. Identify theerrors in the code, if any.custss= newactivexObject(MSXML.XSLTemplate.4)custdomdoc=activexobject( MSXML.ThreadedDOMdocument.4.0); ©NIIT eXtensible Markup Language/Lesson 8/Slide 19 of 23
  20. 20. Accessing XML Documents Using DOMJust a Minute…Identify the errors in the following JavaScript code:xslProcObject.output=xmlDocObject;xslProcObject.Transformalert(xslProcObject); ©NIIT eXtensible Markup Language/Lesson 8/Slide 20 of 23
  21. 21. Accessing XML Documents Using DOMProblem Statement 8.P.1 CyberShoppe sells books through an e-commerce Web site. A customer should be able to view book details, such as the name of the book, the first and last names of the author, the price, and the number of available books. A customer should be given the choice of viewing the book details either as a table or as a list. This choice is to be provided in a drop-down list. Also ensure that the XML document is loaded only once in the browser. Hint: You can create a global xmldoc variable and load the XML document in it. Use the OnLoad event of the BODY element to load the XML document. ©NIIT eXtensible Markup Language/Lesson 8/Slide 21 of 23
  22. 22. Accessing XML Documents Using DOMSummaryIn this lesson, you learned that: The XMLSchemaCache object is used to associate an XML document with an XSD document. The XMLSchemaCache object is used to hold a collection of schema documents that specify the rules to which XML documents must conform. The XSLTemplate object is used to contain a compiled XSL document. The XSLProcessor object is used to apply style sheets on a given XML document. ©NIIT eXtensible Markup Language/Lesson 8/Slide 22 of 23
  23. 23. Accessing XML Documents Using DOMSummary (Contd.) The XSLProcessor object is created using the createProcessor() method. The XSLProcessor object is associated with an XML document by using the input property of the XSLProcessor object. The XSLProcessor object provides the transform() method to transform an XML document according to the information provided in an XSLT style sheet. ©NIIT eXtensible Markup Language/Lesson 8/Slide 23 of 23

×