XML_Session08

Embed Size (px)

Citation preview

  • 8/13/2019 XML_Session08

    1/31

    Slide 1 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    In this session, you will learn to:

    Identify the XML Document Object Model

    Validate an XML document against an XML schema using the

    Document Object Model

    Apply a Style Sheet to an XML document

    Objectives

  • 8/13/2019 XML_Session08

    2/31

    Slide 2 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    DOM defines the logical structure of documents.

    DOM provides an Application Programming Interface (API)

    for dynamically accessing and manipulating a document.

    The DOM objects have associated methods and properties

    to access and manipulate a document.A DOM-enabled parser is required to use the features

    provided by DOM.

    A DOM-enabled parser:

    Parses an XML document to ascertain its validity.

    Creates an in-memory representation of the XML document asa tree structure.

    Identifying the XML Document Object Model

  • 8/13/2019 XML_Session08

    3/31

  • 8/13/2019 XML_Session08

    4/31

    Slide 4 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Following are the key DOM objects:

    Document

    Element

    Node

    NodeListAttr

    Text

    CDataSection

    ParseError

    XML DOM Objects and Methods

  • 8/13/2019 XML_Session08

    5/31

    Slide 5 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Following are the key DOM objects:

    Document

    Element

    Node

    NodeListAttr

    Text

    CDataSection

    ParseError

    XML DOM Objects and Methods (Contd.)

    It is the top-level object that implements all the

    basic DOM methods.

    It also has methods that support XSLT.

    It has methods that can be used to navigate,

    query, and modify the content and structure of

    an XML document.

    Some of the methods provided by this objectare createElement(), createAttribute(),

    createComment(), and createTextNode().

    Some of the properties provided by this object that

    help in manipulating the information contained inthe object are async, childNodes, firstChild,

    documentElement, xml, and readyState.

  • 8/13/2019 XML_Session08

    6/31

    Slide 6 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Following are the key DOM objects:

    Document

    Element

    Node

    NodeListAttr

    Text

    CDataSection

    ParseError

    It represents all the element nodes in an XML

    document.

    The attributes associated with the elements are

    considered to be the properties of the elementsrather than their child elements.

    Some of the methods of this object arealso inherited from the Nodeobject.

    Some of the methods provided by this object aregetAttribute(),

    getElementsByTagName(),

    normalize(),

    andremoveAttributeNS().

    XML DOM Objects and Methods (Contd.)

  • 8/13/2019 XML_Session08

    7/31Slide 7 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Following are the key DOM objects:

    Document

    Element

    Node

    NodeListAttr

    Text

    CDataSection

    ParseError

    It represents a single node in the XML document

    tree structure.

    It provides methods to work with child elements.

    Some of the methods of this object are

    appendChild(newChild),

    insertBefore(newNode,refNode),

    and removeChild(nodeName).

    XML DOM Objects and Methods (Contd.)

  • 8/13/2019 XML_Session08

    8/31Slide 8 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Following are the key DOM objects:

    Document

    Element

    Node

    NodeListAttr

    Text

    CDataSection

    ParseError

    It provides a list of nodes present in an XMLdocument for manipulation.

    This object enables you to iterate through a

    collection of nodes.

    Some of the method of this object are item()

    andnextNode().

    XML DOM Objects and Methods (Contd.)

  • 8/13/2019 XML_Session08

    9/31Slide 9 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Following are the key DOM objects:

    Document

    Element

    Node

    NodeListAttr

    Text

    CDataSection

    ParseError

    It represents an attribute of the Elementobject.

    It is also a Node and inherits various attributesand methods of Nodeobject.

    An attribute is not considered by the DOM to be

    a child node of an element, but rather a property.

    XML DOM Objects and Methods (Contd.)

  • 8/13/2019 XML_Session08

    10/31Slide 10 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Following are the key DOM objects:

    Document

    Element

    Node

    NodeListAttr

    Text

    CDataSection

    ParseError

    It represents the text inside an XML element inthe node tree.

    The splitText()method is associated withthis object.

    XML DOM Objects and Methods (Contd.)

  • 8/13/2019 XML_Session08

    11/31Slide 11 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Following are the key DOM objects:

    Document

    Element

    Node

    NodeListAttr

    Text

    CDataSection

    ParseError

    It represents the CDATA sections in the nodetree.

    This node is used to escape the parts of textthat normally would be recognized as markup.

    XML DOM Objects and Methods (Contd.)

  • 8/13/2019 XML_Session08

    12/31Slide 12 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Following are the key DOM objects:

    Document

    Element

    Node

    NodeListAttr

    Text

    CDataSection

    ParseError It returns information about the most recentparse error.

    It provides properties to retrieve information,such as the error code, the error text, and theline that caused the error.

    Some of the properties of this object areerrorCode, reason, line, and srcText.

    XML DOM Objects and Methods (Contd.)

  • 8/13/2019 XML_Session08

    13/31Slide 13 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    The DOM objects can be used within scripting languages

    such as JavaScript and VBScript.

    Using DOM objects in scripts allow dynamically applying a

    style sheet to an XML document.

    The code for using DOM objects for accessing an XMLdocument needs to be used as an HTML page.

    XML DOM Objects in Scripts

  • 8/13/2019 XML_Session08

    14/31Slide 14 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    DOM:

    Used to validate XML documents against XML schemas.

    Ensures proper grammar and proper formation of the XML

    document.

    DOMDocumentobject:

    Main object in a DOM.

    Represents the top node in every document tree.

    Used to load an XML document, parse it, and validate it.

    XMLSchemaCacheobject:

    Loads the schema document associated with the XMLdocument.

    Validating an XML Document Against an XML Schema Using DOM

  • 8/13/2019 XML_Session08

    15/31Slide 15 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Used to hold a collection of schema documents that

    specifies the rules to which XML documents must conform.

    Following table describes some methods provided by theXMLSchemaCacheobject.

    Accessing an XML Document by Using the XMLSchemaCache Object

    Method Description

    add(namespaceURI,

    variable)

    This method adds a new schema to the collection and alsoassociates the specified namespaceURIwith the schema.

    addCollection(XMLSch

    emaCollection

    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

    element.

    namespaceURI(index

    number)

    This method returns the namespace that is associated with

    the schema at the specified index number.

    remove(namespaceURI) This method removes a schema from a collection.

  • 8/13/2019 XML_Session08

    16/31Slide 16 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    To write a script that validates an XML document against

    the schema, you need to follow the given tasks:

    1. Create the user interface to accept the name of the XML

    document and the XML schema.

    2. Write the code to load the XML document.

    3. Write the code to add the XML schema in the

    XMLSchemaCache object.

    4. Write the code to validate the XML document against the

    schema.

    Validating an XML Document Against the Schema

  • 8/13/2019 XML_Session08

    17/31Slide 17 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Problem Statement:

    The head office of CyberShoppe receives data in the form of

    XML documents from its branches. In order to ensure the

    consistency of data sent by the branches, the head office

    maintains the definitions for the structures of documents in

    schemas. After receiving data from the branches, the headoffice needs to verify that the data conforms to the schema of

    the respective document. For this, the head office needs to

    write a script that validates the data stored in an XML

    document against a schema. You will initially do the testing forthe product.xmlfile.

    Demo: Validating an XML Document

  • 8/13/2019 XML_Session08

    18/31

  • 8/13/2019 XML_Session08

    19/31Slide 19 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Is a DOM object that is used to access an XSLT style sheet.

    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.XSLT tree structure is loaded into the memory of the

    computer and used to process the XML document.

    The XSLTemplate Object

  • 8/13/2019 XML_Session08

    20/31Slide 20 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Used to apply a style sheet to an XML document and then

    process that document.

    Applies the given XSLT document to a specific XML

    document.

    Transforms an XML document by using the XSLT stylesheet.

    JavaScript code to create an XSLProcessorobject is as

    follows:

    var xslprocobj= xsltobj.createProcessor();

    The XSLProcessor Object (Contd.)

  • 8/13/2019 XML_Session08

    21/31Slide 21 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Problem Statement:

    CyberShoppe sells its products through an e-commerce Web

    site. Product details, such as product name, description, price,

    and quantity, need to be displayed.

    A customer can choose to view the product details either as a

    table or as a bulleted list.

    Demo: Applying a Style Sheet to an XML Document During Run Time

  • 8/13/2019 XML_Session08

    22/31

    Slide 22 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Problem Statement:

    The following XML document, customer.xmlcontains

    customer details:

    Harold Johnson56, Regent Road

    London

    UK

    444-425-2355

    Create a Web page that accepts the name of the XML file in atext box. When you click the Submit hyperlink, a JavaScript

    code should load the XML document and extract theCUSTOMERNAMEelement. The address and phone number

    should be changed to 94, McFarlane Avenue and 412-233-

    2344, respectively, by using XML DOM objects.

    Exercises

  • 8/13/2019 XML_Session08

    23/31

    Slide 23 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Which of the following methods of the DOMDocumentobject

    will you use to create an element node that has a

    namespace prefix associated with it?

    a. createElement()

    b. createTextNode()

    c. createNode()

    d. createAttribute()

    Practice Questions

    Answer:

    c. createNode()

  • 8/13/2019 XML_Session08

    24/31

    Slide 24 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    You have written a script to load an XML document and

    validate it against a schema. Which of the followingproperties of the DOMDocumentobject will you use to

    ensure that the script waits till the XML document is loaded

    before executing the rest of the code?

    a. async

    b. readyState

    c. documentElement

    d. xml

    Practice Questions

    Answer:

    a. async

  • 8/13/2019 XML_Session08

    25/31

    Slide 25 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Which of the following properties of the IXMLDOMNode

    object will you use to return the text associated with a node?

    a. nodeName

    b. xml

    c. nodeType

    d. nodeValue

    Answer:

    d. nodeValue

    Practice Questions

  • 8/13/2019 XML_Session08

    26/31

    Slide 26 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Which of the following properties of theIXMLDOMParseErrorobject will you use to retrieve the

    string containing the line that caused an error while parsing

    an XML document?

    a. srcText

    b. linePos

    c. reason

    d. line

    Answer:

    a. srcText

    Practice Questions

  • 8/13/2019 XML_Session08

    27/31

    Slide 27 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    Which one of the following is an object that represents the

    complete XML document map?

    a. IXMLDOMNode

    b. DOMDocument

    c. IXMLDOMNodeList

    d. IXMLDOMParseError

    Answer:

    b. DOMDocument

    Practice Questions

  • 8/13/2019 XML_Session08

    28/31

    Slide 28 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    In this session, you learned that:

    The XMLSchemaCacheobject is used to associate an XML

    document with an XSD document.

    The XMLSchemaCacheobject is used to hold a collection of

    schema documents that specifies the rules to which XML

    documents must conform.The XSLTemplateobject is used to access an XSLT style

    sheet.

    The XSLProcessorobject is used to apply style sheets on a

    given XML document.

    The XSLProcessorobject is created using thecreateProcessor()method.

    The XSLProcessorobject is associated with an XML

    document by using the input property of the XSLProcessor

    object.

    Summary

  • 8/13/2019 XML_Session08

    29/31

    Slide 29 of 31Ver. 1.0

    Extensible Markup Language

    Session 8

    The XSLProcessorobject provides the transform()

    method to transform an XML document according to the

    information provided in an XSLT style sheet.

    DOM is an application-programming interface that allows an

    application to access the contents of an XML document.

    DOM objects allow you to access and manipulate XMLdocuments.

    The MSXML parser loads an XML document and creates a

    tree structure that represents the various components of the

    XML document.

    The basic building block of the tree structure is a node. A nodeis a container that holds information about the elements,

    attributes, and content stored in an XML document.

    Summary (Contd.)

  • 8/13/2019 XML_Session08

    30/31

  • 8/13/2019 XML_Session08

    31/31

    Extensible Markup Language

    The Nodeobject represents a node in the XML document

    structure. This object provides methods to work with the child

    elements.

    The NodeListobject allows you to iterate through a collection

    of nodes.

    The Attrobject represents an attribute of an Elementobject.The Textobject represents the text inside an XML element in

    the node tree.

    The ParseErrorobject returns information about the most

    recent parse error.

    Summary (Contd.)