Presentation is loading. Please wait.

Presentation is loading. Please wait.

4 Copyright © 2004, Oracle. All rights reserved. Validating XML by Using XML Schema.

Similar presentations


Presentation on theme: "4 Copyright © 2004, Oracle. All rights reserved. Validating XML by Using XML Schema."— Presentation transcript:

1 4 Copyright © 2004, Oracle. All rights reserved. Validating XML by Using XML Schema

2 4-2 Copyright © 2004, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Describe XML Schema Create an XML Schema document Reference an XML Schema in an XML document Create an XML Schema document by using: –Built-in data types –Simple types –Complex types with sequences and choices Validate XML documents with XML Schema by using: –The oraxml command-line utility –Oracle JDeveloper 10g

3 4-3 Copyright © 2004, Oracle. All rights reserved. What Is an XML Schema? XML Schema Is an XML language that defines and validates the structure of XML documents Is stored in an XML Schema Document (XSD) Defines components, such as: –Simple types definitions –Complex type definitions –Element declarations –Attribute declarations Supports XML Namespaces, and built-in, simple, and complex data types XSD Validates References Instance

4 4-4 Copyright © 2004, Oracle. All rights reserved. The Benefits of XML Schemas XML Schemas: Unify both document and data modeling Validate XML documents Are created using XML Support the Namespace Recommendation Allow validation of text elements content based on built-in or user-defined data types Allow modeling of object inheritance and type substitution Allow easy creation of complex and reusable content models

5 4-5 Copyright © 2004, Oracle. All rights reserved. An Example XML Schema Document The simple XML Schema uses: –A required XML Namespace string, with an xs prefix, http://www.w3.org/2001/XMLSchema –The element as its document root –The element to declare an element A valid XML instance document: Finance

6 4-6 Copyright © 2004, Oracle. All rights reserved. Validating an XML Document with an XML Schema Document In the XML instance document, use the http://www.w3.org/2001/XMLSchema-instance XML namespace and reference the XML Schema using: The noNamespaceSchemaLocation attribute, or The schemaLocation attribute XSD validates noNamespaceSchemaLocation="file.xsd" schemaLocation="namespace file.xsd" <departments xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="departments.xsd"> Finance

7 4-7 Copyright © 2004, Oracle. All rights reserved. Referencing an XML Schema with the schemaLocation Attribute The XML Schema defines the targetNamespace value. The XML document references the targetNamespace in the schemaLocation and the default namespace. <departments xmlns="http://www.hr.com/departments" xmlns:xsi= "http://www.w3.org/2001/XMLschema-instance" xsi:schemaLocation= "http://www.hr.com/departments departments.xsd"> Finance <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.hr.com/departments">

8 4-8 Copyright © 2004, Oracle. All rights reserved. Components of an XML Schema Partial component hierarchy

9 4-9 Copyright © 2004, Oracle. All rights reserved. Example of XML Schema Components XML document <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="region_id" type="xsd:int"/> <xsd:element name="region_name" type="xsd:string"/> 1 2 3 4 5 6 1 Europe

10 4-10 Copyright © 2004, Oracle. All rights reserved. The Declaration Is the root element in an XML Schema document Contains: –Namespace information –Defaults –Version Example: <schema targetNamespace="URI" attributeFormDefault="qualified"|"unqualified" elementFormDefault="qualified"|"unqualified" version="version number"> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLschema">...

11 4-11 Copyright © 2004, Oracle. All rights reserved. Notes Only

12 4-12 Copyright © 2004, Oracle. All rights reserved. Global and Local Declarations Global declarations: –Appear as direct children of the element –Can be reused within the XML Schema document Local declarations: –Are valid in the context they are defined –Are not children of the element –Can reference global type declarations by using a namespace prefix <schema... xmlns:t="http://...">

13 4-13 Copyright © 2004, Oracle. All rights reserved. Notes Only

14 4-14 Copyright © 2004, Oracle. All rights reserved. Declaring an Element Declare a simple with: A name attribute to specify the tag name A type attribute to specify the content allowed Full syntax: <element name="name-of-element" type="global-type | built-in-type" ref="global-element-name" form="qualified | unqualified" minOccurs="non-negative number" maxOccurs="non-negative number | unbounded" default="default-value" fixed="fixed-value"> Steven

15 4-15 Copyright © 2004, Oracle. All rights reserved. Built-in XML Schema Data Types The XML Schema Language provides built-in data types, such as: The string type The int type The decimal type Many others ( boolean, float, and so on) Examples: 100 1000.50

16 4-16 Copyright © 2004, Oracle. All rights reserved. Notes Only

17 4-17 Copyright © 2004, Oracle. All rights reserved. Declaring a Component The : Is a derived type extending built-in or other types Provides three primary derived types: –A Has facets (properties), such as maxInclusive

18 4-18 Copyright © 2004, Oracle. All rights reserved. Notes Only

19 4-19 Copyright © 2004, Oracle. All rights reserved. Using and SimpleTypes Declaring a : …... SKING sking@hr.com

20 4-20 Copyright © 2004, Oracle. All rights reserved. Declaring Components The declaration: Must be identified by a name attribute if it is global; otherwise, it is an anonymous complex type Provides a content model that can contain: –Simple content –A declaration –A reference to a global –An declaration Can allow mixed or empty content...

21 4-21 Copyright © 2004, Oracle. All rights reserved. Declaring a Defines an ordered sequence of elements Must be contained within a <xsd:element name="department_id" type="xsd:int"/> <xsd:element name="department_name" type="xsd:string"/> <xsd:element name="manager_id" type="xsd:int" minOccurs="0"/> <xsd:element name="location_id" type="xsd:int" minOccurs="0"/>

22 4-22 Copyright © 2004, Oracle. All rights reserved. Declaring a Defines a choice of alternative elements Must also be contained within a <xsd:element name="full_time" type="xsd:string"/> <xsd:element name="part_time" type="xsd:string"/> <xsd:element name="full_name" type="xsd:string"/> <xsd:element name="contract" type="employeeType"/>

23 4-23 Copyright © 2004, Oracle. All rights reserved. Declaring an Empty Element Using a declaration without any elements or a content model Typically contains attributes <xsd:element name="department" maxOccurs="unbounded"/> <xsd:attribute name="department_id" type="xsd:int"/> <xsd:attribute name="department_name" type="xsd:string"/>

24 4-24 Copyright © 2004, Oracle. All rights reserved. Using Element Wildcards Element wildcard declarations: Allow including elements without much control Provide a way to include elements from: –Within the XML Schema and its namespace –Another namespace Are declared using : <xsd:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>

25 4-25 Copyright © 2004, Oracle. All rights reserved. Declaring Attributes Declare an : Identified by the name attribute With the type attribute restricted to built-in or user-defined simple types Full syntax: <attribute name="name-of-attribute" type="global-type | built-in-type" ref="global-attribute-declaration" form="qualified | unqualified" used="optional | prohibited | required" default="default-value" fixed="fixed-value">

26 4-26 Copyright © 2004, Oracle. All rights reserved. Attribute Declarations: Example 1.Based on a : 2.With a default value: 3.Referencing a global type: <xsd:attribute name="department_id" type="xsd:int" default="10"/>

27 4-27 Copyright © 2004, Oracle. All rights reserved. Declaring and Referencing an Declare an : Identified by the name attribute Containing one or more s, or references to s Reference in a or :

28 4-28 Copyright © 2004, Oracle. All rights reserved. Documenting the XML Schema Document the XML Schema by using: XML comments Attributes from other namespaces An declaration: –Provides an element –Contains a element –Can be included within all other component types A big comment about this Schema

29 4-29 Copyright © 2004, Oracle. All rights reserved. Creating an XML Schema Document in JDeveloper Drag and drop 1 2 3 4 5 6

30 4-30 Copyright © 2004, Oracle. All rights reserved. Using the JDeveloper XML Schema Editor 1 2 3 4

31 4-31 Copyright © 2004, Oracle. All rights reserved. Using the JDeveloper XML Schema-Aware XML Editor To use the XML Schema-aware Editor features: 1.Register an XML Schema with JDeveloper 2.Associate an XML document with an XML Schema 3.Use the XML Schema-aware code-insight, and validate the XML menu features

32 4-32 Copyright © 2004, Oracle. All rights reserved. Registering an XML Schema with JDeveloper 1 2 3 4 5 6

33 4-33 Copyright © 2004, Oracle. All rights reserved. Using the XML Schema-Aware XML Editor Code-insight is invoked when a less than ( < ) character, space, or colon ( : ) is typed. Drag and drop from Component Palette. 1 3 2

34 4-34 Copyright © 2004, Oracle. All rights reserved. To validate an XML document against an XML Schema: On the XML document, right-click one of the following: 1.Navigator 2.XML Editor Select Validate XML from the menu Validating an XML Document with its XML Schema in JDeveloper 1 2

35 4-35 Copyright © 2004, Oracle. All rights reserved. Validating an XML Document with its XML Schema by Using oraxml The oraxml Java command-line utility: Does not require registering the XML Schema Validates an XML instance document against an XML Schema specified in the schemaLocation or noNamespaceSchemaLocation attribute values Requires: –The xmlparserv2.jar in the CLASSPATH –The – schema option –The XML document file name java oracle.xml.parser.v2.oraxml -schema test.xml The input XML file is parsed without errors using schema validation mode.

36 4-36 Copyright © 2004, Oracle. All rights reserved. Applications for XML Schema E-commerce Web publications and syndication Enterprise Application Integration (EAI) Process control and data acquisition XSD XML Schema validates the XML information exchanged.

37 4-37 Copyright © 2004, Oracle. All rights reserved. XML Schema Versus DTD XML Schema: –Is more powerful and flexible than a DTD –Provides better namespace support than a DTD –Is written in XML syntax –Is extensible –Provides data type support DTD: –Provides ENTITY functionality that is not supported by XML Schema –Can be embedded in an XML document –Is written in SGML

38 4-38 Copyright © 2004, Oracle. All rights reserved. Converting a DTD to an XML Schema XML document: DTD: XML Schema: 120 Weiss

39 4-39 Copyright © 2004, Oracle. All rights reserved. Summary In this lesson, you should have learned how to: Describe an XML Schema Create an XML Schema document with: –Element declarations –Attribute declarations –Custom simpleType and complexType declarations –Annotations Use the XML Schema built-in data types Associate an XML Schema with an XML instance document Validate XML documents with an XML Schema, using JDeveloper and oraxml command-line tool.

40 4-40 Copyright © 2004, Oracle. All rights reserved. Practice 4: Overview This practice covers the following topics: Writing an XML Schema document Creating a simple XML Schema document manually, and a valid XML instance document Modifying the XML Schema document with the XML Schema editor in JDeveloper Registering the XML Schema in JDeveloper Modifying and validating the XML document against the updated and registered XML Schema

41 4-41 Copyright © 2004, Oracle. All rights reserved. Full Notes Page

42 4-42 Copyright © 2004, Oracle. All rights reserved. Full Notes Page

43 4-43 Copyright © 2004, Oracle. All rights reserved. Full Notes Page

44 4-44 Copyright © 2004, Oracle. All rights reserved.


Download ppt "4 Copyright © 2004, Oracle. All rights reserved. Validating XML by Using XML Schema."

Similar presentations


Ads by Google