Presentation is loading. Please wait.

Presentation is loading. Please wait.

Internet Technologies1 XML Schema The main source for these slides is “The XML Companion” by Bradley Other resources:

Similar presentations


Presentation on theme: "Internet Technologies1 XML Schema The main source for these slides is “The XML Companion” by Bradley Other resources:"— Presentation transcript:

1 Internet Technologies1 XML Schema The main source for these slides is “The XML Companion” by Bradley Other resources: http://www.w3.org/TR/xmlschema-2/

2 Internet Technologies2 XML Schema “XML Schema” is the official name XSDL (XML Schema Definition Language) is the language used to create schema definitions Can be used to more tightly constrain a document instance Supports namespaces Permits type derivation

3 Internet Technologies3 XSDL Alternatives Include DTD’s RELAX TREX (James Clark - Tree Regular Expressions for XML) RELAX NG (RELAX and TREX combined to Relax Next Generation) Schematron (“Rule based” rather than “grammar based” see www.ascc.net/xml/schematron) Based on XSLT and XPathwww.ascc.net/xml/schematron

4 Internet Technologies4 XSDL - A Simple Purchase Order <purchaseOrder orderDate="07.23.2001" xmlns="http://www.cds-r-us.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.cds-r-us.com po.xsd" >

5 Internet Technologies5 Dennis Scannel 175 Perry Lea Side Road Waterbury VT 15216

6 Internet Technologies6 Purchase Order XSDL <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.cds-r-us.com" targetNamespace="http://www.cds-r-us.com" >

7 Internet Technologies7

8 Internet Technologies8

9 Internet Technologies9

10 Internet Technologies10

11 Internet Technologies11 Three Major Uses – Same as DTD’s 1. Validation 2.Code Generation 3.Communication

12 Internet Technologies12 Better than DTD’s Good support for namespaces Type Checking XML Syntax But Harder than DTD’s

13 Internet Technologies13 Validate.java // Validate.java using Xerces import java.io.*; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; import org.xml.sax.InputSource; import org.xml.sax.helpers.XMLReaderFactory; import org.xml.sax.helpers.DefaultHandler; import java.io.*;

14 Internet Technologies14 import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.SAXException; import org.xml.sax.InputSource; import org.xml.sax.SAXParseException;

15 Internet Technologies15 public class Validate extends DefaultHandler { public static boolean valid = true; public void error(SAXParseException exception) { System.out.println("Received notification of a recoverable error." + exception); valid = false; } public void fatalError(SAXParseException exception) { System.out.println("Received notification of a non-recoverable error."+ exception); valid = false; } public void warning(SAXParseException exception) { System.out.println("Received notification of a warning."+ exception); }

16 16 public static void main (String argv []) { if (argv.length != 1) { System.err.println ("Usage: java Validate filename.xml"); System.exit (1); } try { // get a parser XMLReader reader = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser"); // request validation reader.setFeature("http://xml.org/sax/features/validation",true); reader.setFeature( "http://apache.org/xml/features/validation/schema",true); reader.setErrorHandler(new Validate()); // associate an InputSource object with the file name InputSource inputSource = new InputSource(argv[0]); // go ahead and parse reader.parse(inputSource);

17 Internet Technologies17 } catch(org.xml.sax.SAXException e) { System.out.println("Error in parsing " + e); valid = false; } catch(java.io.IOException e) { System.out.println("Error in I/O " + e); System.exit(0); } System.out.println("Valid Document is " + valid); }

18 Internet Technologies18 XML Document <itemList xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation="itemList.xsd"> pen 5 eraser 7 stapler 2

19 Internet Technologies19 XSDL Grammar itemList.xsd <xsd:element ref="item" minOccurs="0" maxOccurs="3"/>

20 Internet Technologies20

21 Internet Technologies21 D:..95-733\examples\XSDL\testing>ant run Buildfile: build.xml run: Running Validate.java on itemList-xsd.xml Valid Document is true

22 Internet Technologies22 Purchase again (with Prefixes) <myns:purchaseOrder orderDate="07.23.2001" xmlns:myns="http://www.cds-r-us.com" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.cds-r-us.com po.xsd" >

23 Internet Technologies23 Dennis Scannel 175 Perry Lea Side Road Waterbury VT 05675A Note that there is a problem with this document.

24 Internet Technologies24

25 Internet Technologies25 XSDL Grammar po.xsd <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.cds-r-us.com" targetNamespace="http://www.cds-r-us.com" >

26 Internet Technologies26

27 Internet Technologies27

28 Internet Technologies28 <xs:attribute name="artist" type="xs:string" />

29 Internet Technologies29 Running Validate D:..\examples\XSDL\testing>ant run Buildfile: build.xml run: Running Validate.java on po.xml Received notification of a recoverable error.org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: '05675A' is not a valid 'integer' value. Received notification of a recoverable error.org.xml.sax.SAXParseException: cvc-type.3.1.3: The value '05675A' of element 'myns:postalCode' is not valid. Valid Document is false

30 Internet Technologies30 Fix the error and run again D:\..\XSDL\testing>ant run Buildfile: build.xml run: Running Validate.java on po.xml Valid Document is true

31 Internet Technologies31 From W3C XML Schema Part 2 DataTypes

32 Internet Technologies32 Introduce a Namespace Error <myns:purchaseOrder orderDate="07.23.2001" xmlns:myns="http://www.cds-r-us.edu" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.cds-r-us.com po.xsd" >

33 Internet Technologies33 Dennis Scannel 175 Perry Lea Side Road Waterbury VT 05675

34 Internet Technologies34

35 Internet Technologies35 And run validate run: Running Validate.java on po.xml Received notification of a recoverable error.org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'myns:purchaseOrder'. Valid Document is false

36 Internet Technologies36 Code Generation Run JAXB against the.xsd file Code generated will present an API allowing us to process that style of document

37 Internet Technologies37 itemList.xsd again <xsd:element ref="item" minOccurs="0" maxOccurs="3"/>

38 Internet Technologies38

39 Internet Technologies39 Run xjc D:..XSDL\testing>xjc itemList.xsd D:\McCarthy\www\95-733\examples\XSDL\testing>java -jar D:\jwsdp-1.1\jaxb-1.0\lib \jaxb-xjc.jar itemList.xsd parsing a schema... compiling a schema... generated\impl\ItemImpl.java generated\impl\ItemListImpl.java generated\impl\ItemListTypeImpl.java generated\impl\ItemTypeImpl.java generated\impl\NameImpl.java

40 Internet Technologies40 generated\impl\QuantityImpl.java generated\Item.java generated\ItemList.java generated\ItemListType.java generated\ItemType.java generated\Name.java generated\ObjectFactory.java generated\Quantity.java generated\bgm.ser generated\jaxb.properties Write Java Code That uses NEW the api

41 Internet Technologies41 The Ant build script used for these examples is also XML

42 Internet Technologies42 <fileset dir="D:/jwsdp-1.1/jaxp-1.2.2/lib/endorsed" includes="*.jar"/>

43 Internet Technologies43

44 Internet Technologies44 More details on XML Schemas

45 Internet Technologies45 Comments in XSDL <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.cds-r-us.com" targetNamespace="http://www.cds-r-us.com" > This is a comment. :

46 Internet Technologies46 Element Definitions The Element element is used to define an element The Element element may be empty Or, may contain content

47 Internet Technologies47 Element Definitions The Element element may be empty and contain no other attributes This purchaseOrder element may contain anything (more elements and text) DTD

48 Internet Technologies48 Simple Content An element may be defined to hold only a number, word or text DTD

49 Internet Technologies49 Complex Content The element may contain child elements or attributes An indicator on how these elements are to be combined sequence =>predefined order

50 Internet Technologies50 Place Holder Element Definitions No content is permitted DTD

51 Internet Technologies51 Namespace Issues All element definitions belong to a target document-type namespace If a prefix is used for schema elements (as we have done above) then we need to specify that prefix on datatypes to distinguish those defined in XSDL from those the author may define.

52 Internet Technologies52 Occurrence options MinOccurs and MaxOccurs Default(if not mentioned) MinOccurs = MaxOccurs = “1” MinOccurs = “0” element is optional MaxOccurs = “unbounded” infinity

53 Internet Technologies53 Choices And a New Example <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:co="http://www.myCompany.com" targetNamespace="http://www.myCompany.com" > This is the company address XSDL document. comment

54 Internet Technologies54 Downtown or uptown

55 Internet Technologies55

56 Internet Technologies56

57 Internet Technologies57 <addr xmlns="http://www.myCompany.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.myCompany.com address.xsd" > Fifth Avenue java Validate addr.xml Valid document is true

58 Internet Technologies58 Choices Choose 1 or none DTD <!ELEMENT addr (downtown | uptown)? >

59 Internet Technologies59 Choices Choose 1 and then as many as you like from the same group DTD <!ELEMENT addr (downtown | uptown)+ >

60 Internet Technologies60 Embedded Groups Required Choose as many as you like including none DTD (title, (para | list)*)

61 Internet Technologies61 Mixed Content DTD Text is always optional.

62 Internet Technologies62 Attributes May never contain element tags or sub- elements So, attribute type values will always be simple types By default, attributes are optional

63 Internet Technologies63 Attributes Can be required, prohibited, optional After removing the attribute …. D:..\95-733\examples\XSDL\testing>java Validate po.xml Received notification of a recoverable error.org.xml.sax.SAXParseException: cvc complex-ty pe.4: Attribute 'orderDate' must appear on element 'purchaseOrder'. Valid Document is false

64 Internet Technologies64 Attribute Types Does not validate <purchaseOrder orderDate="07.23.2001" Validates <purchaseOrder orderDate="1955-12-17"


Download ppt "Internet Technologies1 XML Schema The main source for these slides is “The XML Companion” by Bradley Other resources:"

Similar presentations


Ads by Google