Internet Technologies1 More XML Schema The main source for these slides is “The XML Companion” by Bradley Other resources:
Internet Technologies2 Three Major Uses – Same as DTD’s 1. Validation 2.Code Generation 3.Communication
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
Internet Technologies4 From W3C XML Schema Part 2 DataTypes
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;
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
Internet Technologies7 XMLReader reader = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser"); // request validation reader.setFeature(" true); reader.setFeature( " 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); }
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; }
Internet Technologies9 A Simple Purchase Order <purchaseOrder orderDate=" " xmlns=" xmlns:xsi=" xsi:schemaLocation=" po.xsd" >
Internet Technologies10 Dennis Scannel 175 Perry Lea Side Road Waterbury VT 15216
Internet Technologies11 Purchase Order XSDL <xs:schema xmlns:xs=" xmlns=" targetNamespace=" > XSDL Standard namespace Target namespace
Internet Technologies12
Internet Technologies13
Internet Technologies14
Internet Technologies15
Internet Technologies16 D:..\95-733\examples\XSDL\testing>java Validate po.xml Valid Document is true
Internet Technologies17 Comments in XSDL <xs:schema xmlns:xs=" xmlns=" targetNamespace=" > This is a comment. :
Internet Technologies18 Element Definitions The Element element is used to define an element The Element element may be empty Or, may contain content
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
Internet Technologies20 Simple Content An element may be defined to hold only a number, word or text DTD
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
Internet Technologies22 Place Holder Element Definitions No content is permitted DTD
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.
Internet Technologies24 Occurrence options MinOccurs and MaxOccurs Default(if not mentioned) MinOccurs = MaxOccurs = “1” MinOccurs = “0” element is optional MaxOccurs = “unbounded” infinity
Internet Technologies25 Choices And a New Example <schema xmlns=" xmlns:co=" targetNamespace=" > This is the company address XSDL document. comment
Internet Technologies26 Downtown or uptown
Internet Technologies27
Internet Technologies28
Internet Technologies29 <addr xmlns=" xmlns:xsi=" xsi:schemaLocation=" address.xsd" > Fifth Avenue java Validate addr.xml Valid document is true
Internet Technologies30 Choices Choose 1 or none DTD <!ELEMENT addr (downtown | uptown)? >
Internet Technologies31 Choices Choose 1 and then as many as you like from the same group DTD <!ELEMENT addr (downtown | uptown)+ >
Internet Technologies32 Embedded Groups Required Choose as many as you like including none DTD (title, (para | list)*)
Internet Technologies33 Mixed Content <choice minOccurs=“0” maxOccurs=“unbounded”? DTD Text is always optional.
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
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
Internet Technologies36 Attribute Types Does not validate <purchaseOrder orderDate=" " Validates <purchaseOrder orderDate=" "