XML, XSD, and XSL
Total Page:16
File Type:pdf, Size:1020Kb
e- business XML, XSD, and XSL Bryan Hogan IBM Terminology e- business World Wide Web Consortium (W3C) develops interoperable technologies (specifications, guidelines, software, and tools) to lead the Web to its full potential as a forum for information, commerce, communication, and collective understanding. (www.w3.org) Document Object Model (DOM) a W3C standard API which describes mechanisms for software developers and Web script authors to access and manipulate parsed XML (and HTML) content. The DOM is both platform-neutral and language-neutral Document Type Definition (DTD) a specification of the elements and attributes that are permitted in an XML document XML Schema a specification of the elements and attributes that are permitted in an XML document along with the datatypes for these artifacts 1 Terminology (continued) e- business Well-formed XML document an XML document that conforms to basic XML rules Valid XML document a well-formed XML document that conforms to the rules specified in a DTD Simple API for XML (SAX) a standard interface for event-based XML parsing Extensible Stylesheet Language Transformations (XSLT) A language for transforming XML documents via the application of stylesheets What is XML? e- business eXtensible Markup Language Markup language for describing data Simpler than predecessor SGML (Standard Generalized Markup Language) More versatile than HTML (HyperText Markup Language) Self-describing: XML documents can describe other XML documents (ie. XML schema) An open standard for defining and sharing data across diverse network topologies Mark Weitzel (IBM) 2 Why use XML? e- business XML data representation is human-readable, application-neutral, and language-neutral enabling universal interchange of data XML documents provide an intuitive mechanism for initializing structured data within an application. XML standard is open; therefore, costs are nominal HTML and XML side by side e- business <table border cellspacing=0 <football_game> cellpadding=5> <home> <tr> <school>NCSU</school> <th>Team name</th> <score>17</score> <th>Score</th> </home> </tr> <tr> <visitor> <td>Clemson</td> <school>Clemson</school> <td>15</td> <score>15</score> </tr> </visitor> <tr> </football_game> <td>NCSU</td> <td>17</td> </tr> </table> 3 XML document syntax e- business Element start/end tags <tag1></tag1> or <tag1/> <tag1></TAG1> <!– syntax error. XML is case sensitive Attributes <tag1 attribute1=“testValue” /> <tag1 enabled /> <!– syntax error. Allowed in HTML not XML Comments <!– This is an XML comment Entity references <tag1 attr1="&Entity1;"> Processing instructions <?xml version="1.0"?> XML document syntax e- business Character data sections (CDATA) <![CDATA [<tag1>test</tag1>] ]> Document type declarations <!DOCTYPE XmlMappingSpec SYSTEM "abtxmap.dtd" > <!DOCTYPE XmlMappingSpec SYSTEM "abtxmap.dtd" [ <!ENTITY entity1 “testValue1" > <!ENTITY entity2 “testValue2" > ]> 4 DTD syntax and terminology e- business Element type declarations <!ELEMENT Street (#PCDATA) > Usage: <Street>29 Oak Street</Street> Attribute list declarations <!ATTLIST name firstName CDATA #REQUIRED > <!ATTLIST car maker (Ford | GM | BMW) > Usage: <name firstName=“George” /> <car maker=“Ford” /> <!-- Not valid. Validating parser will flag error <car maker=“Mercury” /> DTD syntax and terminology e- business Entity declarations <!ENTITY IBM “International Business Machines” > <!ENTITY testDoc SYSTEM “http://mywebsite/testDoc.xml” > Usage: <Company>&IBM;</Company> <!– Inline the contents of the testDoc ENTITY <root>&testDoc;</root> Parameter entity declaration <!ENTITY % code_format “CDATA”> Notations declarations <!NOTATION Find_Help SYSTEM “Help System” > 5 DTD ELEMENT examples e- business An element with multiple required subelements. <!ELEMENT main (sub1, sub2, sub3) > A subelement (sub2) that occurs once or not at all. <!ELEMENT main (sub1, sub2?) > A subelement (sub2) that occurs one or more times. <!ELEMENT main (sub1, sub2+) > A subelement (sub2) that occurs zero or more times. <!ELEMENT main (sub1, sub2*) > An element that contains one of multiple elements. <!ELEMENT main (choice1 | choice2 | choice3) > DTD ATTLIST examples e- business #REQUIRED default indicates that an attribute must be specified in XML document instance. <!ATTLIST main attr1 CDATA #REQUIRED > #IMPLIED default indicates that an attribute is not required by the XML document instance. <!ATTLIST main attr1 CDATA #IMPLIED > #FIXED default indicates an attribute has a fixed value, and no other values are acceptable. Since the attribute value is fixed, it does NOT need to be specified in an instance document. <!ATTLIST main attr1 CDATA #FIXED “FixedValue” > Default value supplied. The default value will be used only if no value is supplied by XML document instance. <!ATTLIST main attr1 CDATA “DefaultValue” > 6 Parsing XML (DOM) e- business DOM parsers read XML into a tree structure of nodes. Node types are shown below: Document DocumentFragment DocumentType EntityReference Element Attr ProcessingInstruction Comment Text CDATASection Entity Notation DOM Element API e- business getAttribute, setAttribute, removeAttribute, getAttributeNode, setAttributeNode, removeAttributeNode, hasAttribute getAttributeNS, setAttributeNS, removeAttributeNS, getAttributeNodeNS, removeAttributeNodeNS, hasAttributeNS getElementsByTagName, getElementsByTagNameNS 7 Parsing XML (SAX) e- business SAX parsers generate parsing events that are processed by handlers in an application program. Parsers allow users to plug in custom implementations of the SAX interfaces. The SAX 2.0 interfaces are: Attributes ContentHandler DTDHandler EntityResolver ErrorHandler Locator XMLFilter XMLReader SAX ContentHandler interface e- business characters endDocument endElement endPrefixMapping ignorableWhitespace processingInstruction setDocumentLocator skippedEntity startDocument startElement startPrefixMapping 8 VAST XML parser e- business XML 1.0 specification http://www.w3.org/TR/1998/REC-xml-19980210 DOM level-2 core interfaces http://www.w3.org/TR/1998/REC-xml-19980210 SAX 2.0 http://www.saxproject.org/ Wedding planner DTD e- <!-- 3/10/2001 WildAndWackyWeddings.com retains information and performs billing for wedding planners. All wedding planners must provide records in the format specified by this DTD. --> business <!ELEMENT WeddingPlanner (Address,PhoneNumber,Weddings)> <!ATTLIST WeddingPlanner Name NMTOKEN #REQUIRED id ID #REQUIRED> <!ELEMENT WeddingPlanners (WeddingPlanner*) > <!ENTITY % AddressMembers 'Street,City,State,Zip' > <!ELEMENT Address (%AddressMembers;)> <!ELEMENT PhoneNumber (#PCDATA) > <!ELEMENT Street (#PCDATA) > <!ELEMENT City (#PCDATA) > <!ELEMENT State (#PCDATA) > <!ELEMENT Zip (#PCDATA) > <!ELEMENT Weddings (Wedding)* > <!ELEMENT Wedding (Bride,Groom,Date,Time,CeremonyLocation,ReceptionLocation,Caterer,NumberOfGuests,TotalFee,BillingAddress)> <!ATTLIST Wedding id ID #REQUIRED > <!ELEMENT Bride (#PCDATA) > <!ELEMENT Groom (#PCDATA) > <!ELEMENT BillingAddress (%AddressMembers;) > <!ELEMENT CeremonyLocation (FacilityName,Address) > <!ELEMENT ReceptionLocation (FacilityName,Address) > <!ELEMENT Date (#PCDATA) > <!ELEMENT Time (#PCDATA) > <!ELEMENT Caterer (#PCDATA) > <!ELEMENT NumberOfGuests (#PCDATA) > <!ELEMENT TotalFee (#PCDATA) > <!ELEMENT FacilityName (#PCDATA) > 9 Valid XML document e- <?xml version="1.0"?> business <!DOCTYPE WeddingPlanner SYSTEM "wedding.dtd" > <WeddingPlanner Name="J-Lo" id="Planner_1" > <Address> <Street>29 Oak St.</Street> <City>Raleigh</City> <State>NC</State> <Zip>99999</Zip> </Address> <PhoneNumber>555-4343</PhoneNumber> <Weddings> <Wedding id="Ghezzo.G"> <!-- Detailed wedding information removed ... --> </Wedding> </Weddings> </WeddingPlanner> VAST XML parser example e- business " Validating parser used to read well-formed XML and verify that the contents conform to the DTD referenced in the XML." | domDocument domElement | domDocument := AbtXmlDOMParser newValidatingParser parseURI: ‘d:\ncsu_2003\wedding1.xml’. domElement := domDocument getElementById: ‘Planner_1’. " Non-validating parser used to read well-formed XML data. " | domDocument domElements | domDocument := AbtXmlDOMParser newNonValidatingParser parseURI: ‘d:\ncsu_2003\wedding1.xml’. domElements := domDocument getElementsByTagName: ‘Address’. 10 XML namespaces e- business An XML namespace is a collection of names, identified by a URI reference, which are used in XML documents as element types and attribute names. In an XML instance document, items are namespace qualified using a namespace prefix. The reserved word xmlns is used to associate an arbitrary namespace prefix with the actual namespace. Items in the document instance are prefixed to identify the namespace containing their definition. XML namespace example e- <?xml version="1.0"?> business <vastwsd:deployment targetNamespace="urn:SstWSInsurancePolicyInterface" xmlns:vastwsd="urn:VASTWebServiceDeployment600" xmlns:vast="Smalltalk" xmlns:swsipi="urn:Test"> <services> <service name="SstWSInsurancePolicyInterface" namespace="urn:Test"> <serviceInterfaceClass>SstWSService</serviceInterfaceClass> <provider type="swsipi:TestProvider"> <vast:provider className=“TestClass" creationMethod="new"/> </provider> </service> </services> </vastwsd:deployment> 11 XML schema e- business ° XML schema improves on XML DTD ° XML schemas are coded in XML ° XML schema includes type information allowing object models to be represented ° The W3C XML Schema Primer is a great resource for