23
Accessing XML Documents Using DOM ©NIIT eXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In 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

Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Embed Size (px)

DESCRIPTION

Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 3 of 23 Task 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.

Citation preview

Page 1: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 1 of 23

ObjectivesIn 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

Page 2: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 2 of 23

Problem Statement 8.D.1The 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.

Page 3: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 3 of 23

Task 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.

Page 4: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 4 of 23

Task 1: Identify a mechanism to validate an XML document 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.

Page 5: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 5 of 23

Task 2: Identify the objects required to access an XML 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.

Page 6: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 6 of 23

Task 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.

Page 7: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 7 of 23

Task 2: Identify the … scripting language. (Contd.) The following table describes some methods provided

by this object: Method Description

add(namespaceURI, variable) This method adds a new schema to the collection and also associates the specified namespaceURI with the schema.

addCollection(XMLSchemaCollection object)

This method adds schemas from other schema collections. It also 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.

Page 8: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 8 of 23

Task 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

Page 9: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 9 of 23

Task 3: Write the code to validate the XML document 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.

Page 10: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 10 of 23

Task 4: Execute the code.

Page 11: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 11 of 23

Just a Minute… Write the code to add an XML schema called “Products.xsd” to a schema collection.

Page 12: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 12 of 23

Problem Statement 8.D.2CyberShoppe 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.

Page 13: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 13 of 23

Task 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.

Page 14: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 14 of 23

Task 1: Identify a mechanism to access an XML document 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.

Page 15: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 15 of 23

Task 2: Identify the objects required to apply the style sheet 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.

Page 16: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 16 of 23

Task 2: Identify the objects … runtime.(Contd.)The IXSLProcessor Object

To process an XML document by using a style sheet, you must create an instance of the IXSLProcessor object.

This object is used to apply a style sheet to an XML document and then process that document.

Page 17: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 17 of 23

Task 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 IXSLProcessor objects.

Page 18: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 18 of 23

Task 3: Create the XML document.

Task 4: Create the style sheets.

Task 5: Write the code to apply a style sheet dynamically to an XML document.

Task 6: Execute the code.

Page 19: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 19 of 23

Just a Minute… The following JavaScript code is used to create XSLTemplate and DOMDocument objects. Identify the errors in the code, if any.

custss= new activexObject(MSXML.XSLTemplate.4)

custdomdoc= activexobject( "MSXML.ThreadedDOMdocument.4.0);

Page 20: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 20 of 23

Just a Minute… Identify the errors in the following JavaScript code:

xslProcObject.output=xmlDocObject;

xslProcObject.Transform

alert("xslProcObject");

Page 21: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 21 of 23

Problem Statement 8.P.1CyberShoppe 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.

Page 22: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 22 of 23

SummaryIn 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 IXSLProcessor object is used to apply style sheets on a given XML document.

Page 23: Accessing XML Documents Using DOM ©NIITeXtensible Markup Language/Lesson 8/Slide 1 of 23 Objectives In this lesson, you will learn to: * Use XML DOM objects

Accessing XML Documents Using DOM

©NIIT eXtensible Markup Language/Lesson 8/Slide 23 of 23

Summary (Contd.) The IXSLProcessor object is created using the

createProcessor() method.

The IXSLProcessor object is associated with an XML document by using the input property of the IXSLProcessor object.

The IXSLProcessor object provides the transform() method to transform an XML document according to the information provided in an XSLT style sheet.