1 / 35

COS 381

COS 381. Day 17. Agenda. Questions?? Resources Source Code Available for examples in Text Book in Blackboard Also @ http://perleybrook.umfk.maine.edu/SourceCode/ Html and XHTML examples http://perleybrook.umfk.maine.edu/samples/ In Class Work

evonne
Download Presentation

COS 381

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COS 381 Day 17

  2. Agenda • Questions?? • Resources • Source Code Available for examples in Text Book in Blackboard • Also @ http://perleybrook.umfk.maine.edu/SourceCode/ • Html and XHTML examples • http://perleybrook.umfk.maine.edu/samples/ • In Class Work • http://perleybrook.umfk.maine.edu/SourceCode/inclassWork/ • Assignment 3 Not corrected • Assignment 4 is posted • Due April 4 • Quiz 2 is not corrected • One student has yet to take quiz • Finish Discussion on XML

  3. XML Schemas - Problems with DTDs: 1. Syntax is different from XML - cannot be parsed with an XML parser 2. It is confusing to deal with two different syntactic forms 3. DTDs do not allow specification of particular kinds of data XML Schemas is one of the alternatives to DTD - Two purposes: 1. Specify the structure of its instance XML documents 2. Specify the data type of every element and attribute of its instance XML documents DTD can be converted to schemas http://www.hitsw.com/xml_utilites/ Resource http://www.w3.org/XML/Schema

  4. XML Schemas (continued) • - Schemas are written using a namespace: • http://www.w3.org/2001/XMLSchema • - Every XML schema has a single root, schema • The schema element must specify the namespace for schemas as its xmlns:xsd attribute • - Every XML schema itself defines a tag set, which must be named • targetNamespace = • http://cs.uccs.edu/planeSchema • For this class • http://perleybrook.umfk.maine.edu/ “home directory” • If we want to include nested elements, we must set the elementFormDefault attribute to qualified • - The default namespace must also be specified • xmlns = "http://cs.uccs.edu/planeSchema"

  5. XML Schemas (continued) - - A complete example of a schema element: <xsd:schema <!-- Namespace for the schema itself --> <xmlns:xsd = "http://www.w3.org/2001/XMLSchema" <!-- Namespace where elements defined here will be placed --> <targetNamespace ="http://cs.uccs.edu/planeSchema" <!-- Default namespace for this document --> xmlns = "http://cs.uccs.edu/planeSchema" <!-- Next, specify non-top-level elements to be in the target namespace --> elementFormDefault = "qualified">

  6. XML Schemas (continued) - - A complete example of a schema element: <schema <!-- Namespace for the schema itself --> <xmlns = "http://www.w3.org/2001/XMLSchema" <!-- Namespace where elements defined here will be placed --> <targetNamespace ="http://cs.uccs.edu/planeSchema" <!-- Default namespace for this document --> xmlns:planes = "http://cs.uccs.edu/planeSchema" <!-- Next, specify non-top-level elements to be in the target namespace --> elementFormDefault = "qualified">

  7. XML Schemas (continued) - Defining an instance document - The root element must specify the namespaces it uses 1. The default namespace 2. The standard namespace for instances (XMLSchema-instance) 3. The location where the default namespace is defined, using the schemaLocation attribute, which is assigned two values <planes xmlns = "http://cs.uccs.edu/planeSchema" xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://cs.uccs.edu/planeSchema planes.xsd" > Data Type Categories 1. Simple (strings only, no attributes and no nested elements) 2. Complex (can have attributes and nested elements)

  8. XML Schemas (continued) - XMLS defines over 40 data types http://www.w3.org/TR/xmlschema-2/#built-in-datatypes - Primitive: string, Boolean, float, … - Derived: byte, decimal, positiveInteger, … - User-defined (derived) data types – specify constraints on an existing type (the base type) - Constraints are given in terms of facets (totalDigits, maxInclusive, etc.) - Both simple and complex types can be either named or anonymous - DTDs define global elements (context is irrelevant) - With XMLS, context is essential, and elementscan be either: 1. Local, which appears inside an element that is a child of schema, or 2. Global, which appears as a child of schema

  9. XML Schemas (continued) - Defining a simple type: - Use the element tag and set the name and type attributes <xsd:element name = "bird" type = "xsd:string" /> - An instance could have: <bird> Yellow-bellied sap sucker </bird> - Element values can be constant, specified with the fixed attribute fixed = "three-toed" - User-Defined Types - Defined in a simpleType element, using facets specified in the content of a restriction element - Facet values are specified with the value attribute

  10. XML Schemas (continued) <xsd:simpleType name = "middleName" > <xsd:restriction base = "xsd:string" > <xsd:maxLength value = "20" /> </xsd:restriction> </xsd:simpleType> - Categories ofComplex Types 1. Element-only elements (elements no text) 2. Text-only elements (text no elements) 3. Mixed-content elements (both text and elements 4. Empty elements - Element-only elements - Defined with the complexType element - Use the sequence tag for nested elements that must be in a particular order - Use the all tag if the order is not important

  11. XML Schemas (continued) <xsd:complexType name = "sports_car" > <xsd:sequence> <xsd:element name = "make" type = "xsd:string" /> <xsd:element name = "model " type = "xsd:string" /> <xsd:element name = "engine" type = "xsd:string" /> <xsd:element name = "year" type = "xsd:string" /> </xsd:sequence> </xsd:complexType> - Nested elements can include attributes that give the allowed number of occurrences (minOccurs, maxOccurs, unbounded) - We can define nested elements elsewhere <xsd:element name = "year" > <xsd:simpleType> <xsd:restriction base = "xsd:decimal" > <xsd:minInclusive value = "1990" /> <xsd:maxInclusive value = "2003" /> </xsd:restriction> </xsd:simpleType> </xsd:element>

  12. XML Schemas (continued) - The global element can be referenced in the complex type with the ref attribute <xsd:element ref = "year" /> - Validating Instances of XML Schemas - Can be done with several different tools - One of them is xsv, which is available from: http://www.ltg.ed.ac.uk/~ht/xsv-status.html - Note: If the schema is incorrect (bad format), xsv reports that it can find the schema planes.xsd Plane.xml

  13. Modified Plane example from text <?xml version = "1.0"?> <!-- plane.xml A simple XML document for illustrating a schema The schema is in planes.xsd --> <?xml-stylesheet type="text/css" href="planes.css"?> <plane xmlns = "http://perleybrook.umfk.maine.edu/examples/planes" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://perleybrook.umfk.maine.edu/examples/planes planes.xsd"> <make> Cessna </make> <make> Piper </make> <make> Beechcraft </make> </plane>

  14. Con’t <?xml version = "1.0"?> <!-- planes.xsd A simple schema for plane.xml --> <xsd:schema xmlns:xsd = "http://www.w3.org/2001/XMLSchema" targetNamespace = "http://perleybrook.umfk.maine.edu/examples/planes" xmlns = "http://perleybrook.umfk.maine.edu/examples/planes" elementFormDefault = "qualified"> <xsd:element name = "planes"> <xsd:complexType> <xsd:all> <xsd:element name = "make" type = "xsd:string" minOccurs = "1" maxOccurs = "unbounded" /> </xsd:all> </xsd:complexType> </xsd:element> </xsd:schema>

  15. Validating Instances of Schemas • Various systems for validating instances against schemas • Online http://www.w3.org/2001/03/webdata/xsv • XML support libraries include validation: Xerces from Apache, Saxon, Altova XML tools • Some IDE’s have automatic validation: Altova Spy, Eclipse with Oxygen, Eclipse with XML Buddy Pro • Certain IDE’s will use schemas to provide support for XML file creation

  16. Displaying Raw XML Documents - There is no presentation information in an XML document - An XML browser should have a default style sheet for an XML document that does not specify one - You get a stylized listing of the XML

  17. Displaying XML Documents with CSS - A CSS style sheet for an XML document is just a list of its tags and associated styles - The connection of an XML document and its style sheet is made through an xml-stylesheet processing instruction <?xml-stylesheet type = "text/css" href = "mydoc.css"?> --> SHOW planescss.xml and planes.css

  18. 7.9 Overview of XSLT • A functional style programming language • Basic syntax is XML • There is some similarity to LISP and Scheme • An XSLT processor takes an XML document as input and produces output based on the specifications of an XSLT document

  19. 8.9 XSLT Style Sheets Resources http://www.w3.org/Style/XSL/ http://www.vbxml.com/xsl/tutorials/intro/default.asp http://www.w3.org/TR/xsl/ -An XSLT processor merges an XML document into an XSLT style sheet - This merging is a template-driven process - An XSLT style sheet can specify page layout, page orientation, writing direction, margins, page numbering, etc.

  20. 7.9 XSLT Style Sheets • A family of specifications for transforming XML documents • XSLT: specifies how to transform documents • XPath: specifies how to select parts of a document and compute values • XSL-FO: specifies a target XML language describing the printed page • XSLT describes how to transform XML documents into other XML documents such as XHTML • XSLT can be used to transform to non-XML documents as well

  21. Figure 7.5 XSLT processing

  22. 7.9 XSLT Structure • An XSLT document contains templates • XPath is used to specify patterns of elements to which the templates should apply • The content of a template specifies how the matched element should be processed • The XSLT processor will look for parts of the input document that match a template and apply the content of the template when a match is found • Two models • Template-driven works with highly regular data • Data-driven works with more loosely structured data with a recursive structure (like XHTML documents)

  23. XSLT Style Sheets (continued) - The processing instruction we used for connecting a CSS style sheet to an XML document is used to connect an XSLT style sheet to an XML document <?xml-stylesheet type = "text/xsl“ href = "XSLT style sheet"?> - An example: <?xml version = "1.0"?> <!-- xslplane.xml --> <?xml-stylesheet type = "text/xsl" href = "xslplane.xsl" ?> <plane> <year> 1977 </year> <make> Cessna </make> <model> Skyhawk </model> <color> Light blue and white </color> </plane>

  24. XSLT Style Sheets (continued) - An XSLT style sheet is an XML document with a single element, stylesheet, which defines namespaces <xsl:stylesheet xmlns:xsl = http://www.w3.org/1999/XSL/Format xmlns = “http://www.w3.org/1999/xhtml”> - If a style sheet matches the root element of the XML document, it is matched with the template: <xsl:template match = "/"> - A template can match any element, just by naming it (in place of /) <xsl:template match = “plane"> - XSLT elements include two different kinds of elements, those with content and those for which the content will be merged from the XML doc - Elements with content often represent xHTML elements <span style = "font-size: 14"> Happy Easter! </span>

  25. XML Transformations and Style Sheets (continued) • - XSLT elements that represent xHTML elements are simply copied to the merged document • - The XSLT value-of element • - Has no content • - Uses a select attribute to specify part of the XML data to be merged into the XSLT document • <xsl:value-of select = ”CAR/ENGINE" /> • - The value of select can be any branch of the document tree • SHOW xslplane.xmlxslplane.xsl • - The XSLT for-each element • - Used when an XML document has a sequence of • the same elements • --> SHOW xslplanes.xml • --> SHOW xslplanes.xsl

  26. 7.9 Producing Transformation Output • Elements not belonging to XSLT and other text will be copied to the output when the containing template is applied • The value-of tag causes the select attribute value to be evaluated and the result is put into the output • The value of an element is the text contained in it and in sub-elements • The value of an attribute is the value • Example xslplane1.xsl transforms the xslplane.xml file into XHTML for display purposes • If the style sheet is in the same directory as the XML file, some browsers will pick up the transformation and apply it • This works with Firefox and Internet Explorer but not Opera

  27. 7.9 Processing Repeated Elements • File xslplanes.xml contains data about multiple airplanes • The style sheet xslplanes.xsl uses a for-each element to process each plane element in the source document • A sort element could be included to sort output • The element <xsl:sort select=“year” data-type=“number”/> • Specifies sorting by year

  28. Overview

  29. 7.10 XML Processors • XML processors provide tools in programming languages to read in XML documents, manipulate them and to write them out

  30. 7.10 Purposes of XML Processors • Four purposes • Check the basic syntax of the input document • Replace entities • Insert default values specified by schemas or DTD’s • If the parser is able and it is requested, validate the input document against the specified schemas or DTD’s • The basic structure of XML is simple and repetitive, so providing library support is reasonable • Examples • Xerces-J from the Apache foundation provides library support for Java • Command line utilities are provided for checking well-formedness and validity • Two different standards/models for processing • SAX • DOM

  31. 7.10 Parsing • The process of reading in a document and analyzing its structure is called parsing • The parser provides as output a structured view of the input document

  32. 7.10 The SAX Approach • In the SAX approach, an XML document is read in serially • As certain conditions, called events, are recognized, event handlers are called • The program using this approach only sees part of the document at a time

  33. 7.10 The DOM Approach • In the DOM approach, the parser produces an in-memory representation of the input document • Because of the well-formedness rules of XML, the structure is a tree • Advantages over SAX • Parts of the document can be accessed more than once • The document can be restructured • Access can be made to any part of the document at any time • Processing is delayed until the entire document is checked for proper structure and, perhaps, validity • One major disadvantage is that a very large document may not fit in memory entirely

  34. 7.11 Web Services • Allow interoperation of software components on different systems written in different languages • Servers that provide software services rather than documents • Remote Procedure Call • DCOM and CORBA provide implementations • DCOM is Microsoft specific • CORBA is cross-platform

  35. 7.11 Web Service Protocols • Three roles in web services • Service providers • Service requestors • Service registry • The Web Services Definition Language provides a standard way to describe services • The Universal Description, Discovery and Integration service provides a standard way to provide information about services in response to a query • SOAP is used to specify requests and responses

More Related