Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Internet Technologies1 More 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 Three Major Uses – Same as DTD’s 1. Validation 2.Code Generation 3.Communication

3 Internet Technologies3 Better than DTD’s Good support for namespaces (namespaces came after DTD’s) Type Checking (DTD’s use #PCDATA) See next slide XML Syntax (DTD’s are not XML) XML tools, like editors, will work with XSDL

4 Internet Technologies4 From W3C XML Schema Part 2 DataTypes

5 Internet Technologies5 // Validate.java using Xerces // Program written by Kunal Kaviraj 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;

6 Internet Technologies6 public class Validate implements ErrorHandler { public static boolean valid = true; public static void main (String argv []) throws SAXException, IOException { if (argv.length != 1) { System.err.println ("Usage: java Validate filename.xml"); System.exit (1); } // get a parser

7 Internet Technologies7 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); System.out.println("Valid Document is " + valid); }

8 Internet Technologies8 public void warning(SAXParseException exception) { System.out.println("Validate:Warning" + exception); valid = false; } public void error(SAXParseException exception) { System.out.println("Validate:Error" + exception); valid = false; } public void fatalError(SAXParseException exception) { System.out.println("Validate:fatalError" + exception); valid = false; }

9 Internet Technologies9 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" >

10 Internet Technologies10 Dennis Scannel 175 Perry Lea Side Road Waterbury VT 15216

11 Internet Technologies11 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" > XSDL Standard namespace Target namespace

12 Internet Technologies12

13 Internet Technologies13

14 Internet Technologies14

15 Internet Technologies15

16 Internet Technologies16 D:..\95-733\examples\XSDL\testing>java Validate po.xml Valid Document is true

17 Internet Technologies17 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. :

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

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

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

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

22 Internet Technologies22 Place Holder Element Definitions No content is permitted DTD

23 Internet Technologies23 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.

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

25 Internet Technologies25 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

26 Internet Technologies26 Downtown or uptown

27 Internet Technologies27

28 Internet Technologies28

29 Internet Technologies29 <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

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

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

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

33 Internet Technologies33 Mixed Content <choice minOccurs=“0” maxOccurs=“unbounded”? DTD Text is always optional.

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

35 Internet Technologies35 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

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


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

Similar presentations


Ads by Google