1. Trang chủ
  2. » Công Nghệ Thông Tin

Addison wesley XML schema complete reference sep 2002 ISBN 0672323745

464 80 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 464
Dung lượng 1,59 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

corresponding schemas and then aggregate them as neededinto a governing document schema on a per document basis.The following section shows how to Identify reusable datatypes Create modu

Trang 1

Most distributed architectures discuss three tiers in the

development of Web-based applications, but the Web Servertier should really be thought of as a separate layer In fact, as

we move closer and closer to Web services this layer might bereplaced altogether Therefore, the layers modeled for the CRSSare:

level architectural diagrams and then model the tiers one at atime

The simplest way to model the application is to start with high-The architecture shown in Figure 26.1 has been used in manyapplications for quite a few years now In fact, the success ofthis architecture has accelerated the acceptance and emergence

of XML standards

Figure 26.1 n-Tier architecture.

Trang 2

concepts should be introduced to the mix illustrated in Figure26.1 We certainly have enough acronyms without introducingall the X-based ones You should not start to use all these Xtechnologies without a good justification Properly justifying theuse of XML-based technologies will seem simple if you havealready been dealing with some of the following issues:

HTML browsers do not comply with rigid standards Thismight not be a stunning revelation to any Web developerwho has been up all night sorting out a Netscape versusInternet Explorer issue Use XML with XSLT to produce well-formed HTML—or better yet, XHTML

The Web Server tier in Figure 26.1 has many competingtechnologies Learning all of them is difficult They are alltrying to do the same thing: accept user inputs as HTTPmessages and format the output results as HTML or WML.Many applications have misused the Web server layer andplaced far too much validation and business logic in it

Maintaining these projects is extremely difficult The

technologies are simplified dramatically if you switch to

XML-based technologies and use XML schemas to performthe validation

The Component tier is certainly the most interesting You

Trang 3

purpose of this tier, which is to provide business logic

Business logic is based on data, and XML is tailor-made formanipulating data By using XML, you can simplify your

components by passing XML nodes back and forth Eachnode can use an XML schema for its test case This

eliminates the need to write software to test software—aclassic Catch-22 If you ask Web developers how they test

an application, they're likely to say they open Netscape andInternet Explorer and verify that the application works

Could you imagine testing a new aircraft wing by installing

it on a plane and taking it up for a flight? If it works once,will it work every time? Will it work with all revisions?

Obviously, much better test methodology is required

The Database tier is a moving target right now Both

Microsoft and Oracle are rapidly adding XML capabilities totheir databases These capabilities enable you to use XMLmessages to request database actions, eliminating all theformatting required in an Active Server Page/Active DataObject (ASP/ADO) solution or corresponding Java ServerPage/Java Database Connectivity (JSP/JDBC) scenario

Note

Microsoft and Oracle are not the only vendors using XML in theirdatabase products IBM, Informix, Sybase, and others are

adding similar capabilities

Our Campus Resource and Schedule System (CRSS) case studyuses XML as its foundation, which is illustrated in Figure 26.2.When comparing Figure 26.2 to Figure 26.1, the most striking

Trang 4

Figure 26.2 n-Tier XML architecture.

As you can see from Figure 26.2, XML technologies do not

replace HTML, JavaScript, NET, Java, or SQL XML simply isused by these technologies to increase their reliability and

an XML schema Once you have valid user input, the message is

Trang 5

Database access is also accomplished with XML messages

The challenge at the database interface is different than at theuser interface You can ensure that your components are

producing valid XML in the design of the components, but youneed to map the data from an XML source to a relational

database format and vice versa XML schemas can provide thehierarchical to relational mapping, as you will see a bit later

Interfaces between all three tiers are simplified when using

XML Testing at all interfaces is accomplished using XML

schemas This ability to test the interfaces declaratively allowssoftware projects to be worked on by teams more easily

You will begin the CRSS application by modeling the Client tier

to the Web Server tier You will model each requirement by

using UML sequence diagrams Then you will convert each ofthese sequence diagrams into XML messages These messagesdefine the workflow of the application

Let's start by modeling the following requirement: View existingreservations on a per resource basis

Obviously, user input will be required You need to support

multiple devices, but let's start by assuming a browser

interface As you will see later, implementing the support forany other Web-enabled device is easy The sequence diagram in

Figure 26.3 models the first requirement

Figure 26.3 View Resource Reservation sequence

diagram.

Trang 6

You can see from the sequence diagram in Figure 26.3 that

Trang 7

</resource>

</getResourceCalendar>

As you can see, the preceding code is a simple XML messagecreated in the Web Client tier You can use an XML schema tovalidate this message The schema provides the contract youhave with the input device Using this model enables us to

support Web-based clients or XML-enabled devices You can useschemas to open your service to the Internet by creating a Webservice

Note

The ability to publish schemas on the Internet so your servicescan be made available to anyone is the basis for Web services.Web Services Description Language (WSDL) uses XML schemasand has been submitted as a possible standard to the W3C

WSDL uses XML schemas to describe your application interfacesand interface definitions

You need to create the XML schema by using the XML messagecreated in Listing 26.1 You have several ways you could go

about creating the XML schema There are quite a few tools that

do a reasonable job; however, no tool can know your businessapplication requirements Most tools that automatically

generate XML schemas produce something similar to the

example in Listing 26.2, produced by XML Spy

Listing 26.2 XML Schema for Resource Calendar Message

<?xml version="1.0" encoding="UTF-8"?>

Trang 8

targetNamespace=

"http://www.XMLSchemaReference.com/examples/CRSS/resourceCalendar" xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns=

"http://www.XMLSchemaReference.com/examples/CRSS/resourceCalendar" elementFormDefault="qualified"

Trang 10

Chapter 3

Testing the XML schema is very straightforward You can useany validating parser to load the document If the documentloads with no errors, the XML message is valid relative to theXML schema Naturally we must make the assumption that thevalidating parser does not have errors There are many toolsyou can use to provide validation Some of the popular tools areXML Spy by Altova (http://www.xmlspy.com)

Xerces (http://xml.apache.org/xerces-c/)

.NET from Microsoft (http://msdn.microsoft.com/vstudio)IBM's parsers (http://www.alphaworks.ibm.com)

When just doing quick tests, the Internet Explorer plug-in isconvenient Microsoft has a download called iexmltls.exe

that can be found by following the links at the

http://msdn.microsoft.com/xml site After installing iexmltls.exeyou can just add the reference to our XML schema into the XMLdocument and then open the XML document in Internet

Explorer Right-clicking within the document in Internet Explorerprovides a menu item to validate Let's test our document usingthis test right now

Performing validation tests using Internet Explorer's plug-in issimple, as shown in Figure 26.4 below You can see that thedocument is valid You can make changes to the XML and verifythat the XML schema functions as expected You should test allthe permutations possible, so feel free to experiment

Figure 26.4 Validation test.

Trang 11

The Schema Recommendation is very recent and some areascan be interpreted in different ways When testing your

schemas, you should use the validating parser that will be used

in your production environment Test by using several validatingparsers to ensure that the schema design is robust Check at

Trang 12

corresponding schemas and then aggregate them as neededinto a governing document schema on a per document basis.The following section shows how to

Identify reusable datatypes

Create modular XML schemas

Test modular schemas

Aggregate the modules into a master XML schema to allowresource reservations

26.1.1.1 XML Schema Support for Reusable

Datatypes

Obviously the question is how to modularize your schemas toprovide a more productionworthy solution Fortunately, theSchema Recommendation provides techniques to achieve thismodularity The three techniques are

include: Used to include other schema documents thathave the same target namespace

import: Used to refer to components from other

namespaces

redefine: Similar to include with the additional option

Trang 13

of redefine Redefining schemas is useful when you are

Trang 14

book; however, we do review the sequence diagram you will use

to create the XML schema

You use the desired reservation request XML message to

identify the datatypes you would like to create This approachlets us focus on the XML Schema techniques and illustrate how

to create reusable XML schemas Typically, you start with theUML sequence diagram and then create a sample XML messagerepresenting the data required to accomplish the task The

sequence diagram for reserving a resource is shown in Figure26.5

Figure 26.5 CRSS Reservation Request Sequence

diagram.

In a real system, the sequence diagram would have far morecomplexity For our CRSS application, a simple diagram

Trang 15

<?xml version="1.0" encoding="utf-8"?>

<reservation_request>

<reservation building="Linden Hall" room="Gymnasium" start_date="2001-02-23"

Trang 16

<viewCalendar view="xslt/viewCalendar.xslt" status="false"/>

<viewPending view="xslt/viewPending.xslt" status="false"/>

</workflow>

</reservation_request>

The reservation request shown in Figure 26.5 can be broken

down into several important steps The reservation elementcontains the information a user is going to provide by using abrowser or some other Web interface device The 'security'element is used to attach security information to the incomingmessage The workflow element is used to track the status ofthe reservation request The building element contains

response data that will be sent to the user with room capacityinformation, required forms, the room's contact person and

more The application will fill in the user's security information,supply the user with the required forms to fill in, and keep track

Trang 17

type string Enumerated list

11 building id int Number assigned to each building

12

location anyURI Constrain to W3C specs for URLs

16 approval status boolean Same as line 15

17 viewCalendar status boolean Same as line 15

18 viewPending status boolean Same as line 15

Trang 18

of datatypes and desired constraints Several of these datatypeswill be very reusable on many messages Specifically,

constraints on user's names, e-mail formatting, date, time, andstatus will be usable across many XML schemas You alwayswant to achieve the highest reusable rate from your work

Creating a simple type to constrain the attribute date can bedone as shown in Figure 26.2 Notice that a pattern is used toconstrain the value range and the format that the date must besubmitted in If you just needed to constrain the values to

range, the minInclusive and maxInclusive constraints

could have been applied

Listing 26.6 CRSS Start Date Constrained

Trang 19

created In the test case in Listing 26.7, a test XML schema is

created that has a root element named root with one

additional element called test The test element takes one

attribute, dateTest, which uses our new dateType Listing

Trang 20

dateType simple type The dateTest attribute uses the

Trang 21

earlier

Use any text editor to create the code shown in Listing 26.8

This listing references the dateTypeTest schema that includes

the dateType simple type created first This provides a very

You can experiment with the XML document in Listing 26.8 and

verify that your new dateType XML schema is working

Changing the dateTest attribute in Listing 26.8 to 2000-02-26

Trang 22

assign the desired schema to validate with XML Spy displayserrors if your XML source violates the governing schema's rules,

as shown in Figure 26.7

Figure 26.7 Failed validation result when using XML Spy.

The failed result in Figure 26.7 is not due to the XML schemadate format The 2000-12-31 format works well with the XMLschema primitive date datatype The pattern you used

constrained the date range to the years 2001 through 2099.Properly restricting your datatypes by using XML Schema facetsreduces the amount of validation code typically required by theapplication

Note

In addition to Chapter 14, a good source for regular expressions

is http://regxlib.com/ Beware that XML Schema regular

expressions are not the same as Perl regular expressions Agood place to test regular expressions for compliance with theXML Schema version is Daniel Potter's site at

http://www.xfront.org/xml-schema/ This site features a Javaapplet that can be used to test your Schema patterns online

Trang 23

Aggregate your tested XML schema modules into a masterfor the action modeled by your sequence diagram

There are many different ways to restrict data by using XMLschemas You have seen several examples in previous chapters

of the book The CRSS application XML schemas restrict thedata by using many XML Schema techniques, including the

following:

pattern: Entries must match the pattern specified

Trang 24

26.9 can be applied to Listing 26.7 to provide validation to the

Trang 25

<xsd:attribute name="building" use="required" type="buildingType" />

Trang 26

<xsd:complexType>

<xsd:attributeGroup ref="userAttGroup"/> </xsd:complexType>

<xsd:attribute name="name" use="required" type="nameType" />

Trang 27

</xsd:sequence>

<xsd:attribute use="required" name="name" type="nameType"/>

Trang 28

<xsd:simpleType>

<xsd:restriction base="xs:string"> <xsd:enumeration value="request"/> <xsd:enumeration value="approval"/> <xsd:enumeration

value="xslt/viewPending.xslt"/> </xsd:restriction>

Trang 30

xmlns="http://XMLSchemaReference.com/examples/resRequest.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

Trang 31

view="xslt/request.xslt" status="1"/> </workflow>

</reservation_request>

XML Spy's automatically generated XML document provides aneasy way to identify problems with your schema If you are

satisfied with the output, you can continue If other constraintsare needed, you should continue working with the XML schemauntil you are satisfied

Trang 32

Namespaces help uniquely identify attribute and element

names They also help to prevent naming collisions They dothis by associating a Uniform Resource Identifier (URI) withyour XML vocabulary so that elements with the same name butassociated with different namespace URIs are differentiated

Because namespaces came out after XML, they are not the

easiest to use when validating your document with a DTD ADTD must specify the exact name of the attributes and

elements that appear in the document As you will see, because

of namespaces, a name is made up of multiple parts, one ofwhich is an optional and arbitrary prefix that must be specified

in the DTD to successfully validate it This isn't the case withXML Schema: Namespaces are an integral part of XML Schema

By its nature, a schema uses at least two Namespaces: the XMLSchema namespace that identifies the XML schema vocabulary,and the target namespace for the tags you are defining

Trang 33

In this chapter, all strings that might appear in an XML instanceappear in double quotes (for example, "a sample value").This is different from most of the rest of book There are somany quoted regular expressions (for example, 'reg.*exp?')and corresponding instance values, that the visual distinctionbetween schema values and instance values is valuable

Occasionally, the instance value contains line delimiters, suchas:

"this string contains

a line break"

Additionally, some instance values contain meaningful spaces

Trang 34

element

An XML validator can validate appropriate values in an XML

instance against a regular expression specified by a simple

type The validation assures that each string value in the XMLinstance conforms to the appropriate set of regular expressions

This chapter assumes an understanding of XML schemas in

general (specifically any chapter before this one) Furthermore,

for simplicity, most of this chapter discusses only the regular

expressions, not the simple or complex types that incorporatethe regular expression All of the XML schema examples in thischapter describe a regular expression and a set of strings thatvalidate against that regular expression This chapter skips

discussion of element types, element instances, attribute types,and attribute instances, although an XML validator requires

these to create and validate an XML instance

All the regular expressions described in this chapter come from

regexpDemo.xsd Similarly, regexpDemo.xml contains thestrings that purport to be valid in a corresponding XML instance.Both of these files are online at

Trang 35

Locators

URIs identify namespaces URIs take the form of Uniform

Resource Names (URNs) or Uniform Resource Locators (URLs).URNs identify a resource by giving it a globally unique and

persistent name The following is a typical URN:

urn:example:mytags

in which 'urn' identifies the Uniform Resource Name scheme,which means that this URI is a URN, 'example' is a namespaceidentifier (NID), and 'mytags' is a namespace-specific string(NSS)

Do not confuse namespace in the URN syntax description with

the concept of Namespace from the Namespace

Recommendation The URN specification has its own concept ofnamespaces

URLs uniquely identify the location of a resource Browsing theWeb wouldn't be possible without URLs The following is a

namespace identifiers These include mailto, ftp, and uuid,among others Although no resource is required to be availablewhen using a URL to identify an XML Namespace, people—andeven applications—might try to dereference your URL, looking

Trang 36

3.1.1 Creating URIs

When generating a URI for a namespace that you plan to

distribute publicly, you should follow certain protocols to ensureuniqueness You do not want to use a common name that

others might use If you do, you risk having naming clasheswith other XML vocabularies

That is one of the reasons HTTP URLs are popular for specifyingnamespaces The first part of the scheme-specific string is adomain name that you register and own This eliminates

accidental naming clashes and gives you certain rights in

forbidding others to use it

URNs have similar rules There is a procedure to register an NIDwith the Internet Engineering Task Force (IETF) After you gothrough the registration process, you own the NID for use increating names You are not required to register an NID,

however There is a protocol for using unregistered names byprepending an 'x-' to the NID to identify it as experimental.

Even so, you risk naming clashes if you choose common wordsfor your NID, so a good practice would be to use a domain youcontrol For example:

urn:x-example-com:mytags

where 'example.com' is a registered domain name under thecontrol of the author You might have noticed that the periodsused in the URL were turned into dashes in the URN Both URLsand URNs have specific rules about which characters are

allowed For more information on creating URLs and URNs, see

Trang 37

schema Others feel it should be documentation or a package ofvarious resources related to the namespace being used

To help solve this problem, an XML vocabulary called ResourceDirectory Description Language (RDDL) was developed by agroup of individuals on the XML-DEV mailing list This

vocabulary uses the XLink Recommendation, XML Linking

Language (XLink) Version 1.0 (http://www.w3.org/TR/xlink), toidentify a number of resources that can be associated with anamespace Here's an example from the XML namespace thatidentifies a DTD as well as a schema:

<rddl:resource

xlink:title="DTD for validation"

xlink:arcrole=

Trang 38

xlink:role=

"http://www.isi.edu/in-notes/iana/assignments/media-types/text/xml-dtd" xlink:href="XMLSchema.dtd"/>

Trang 39

Namespaces enable you to create unique names that appear in

an XML document You associate a URI with the names that youuse in your document When a name is associated with a URI, it

is called a "qualified name" and is said to be in that namespace.You can also have names in your document that are not in anynamespace These names are called "unqualified." You associate

names in your document with a namespace by using a prefix A

prefix is an arbitrary string of characters that acts as shorthandfor the namespace URI so that names are not overly long Theprefix is also optional, because you can declare a namespace to

be a default, meaning all unprefixed element names are in thatnamespace by default

Ngày đăng: 26/03/2019, 17:12