Tải bản đầy đủ - 0 (trang)
Appendix C. The XSD You Need for Office

Appendix C. The XSD You Need for Office

Tải bản đầy đủ - 0trang

C.1WhatIsXSD?

XMLSchemaDescription(XSD),sometimesreferredtoasW3C

XMLSchema(WXS),isanXMLvocabularythatletsyou

describeotherXMLvocabulariessothatprogramscantest

whetheragivendocumentmeetsruleslaiddowninthe

schema.XSDisdefinedbyasetofthreeW3C

Recommendations:



XMLSchemaPart0:Primer

AtutorialforXMLSchema,explainingParts1and2in

simplertermswithmoreexamplesandintegration.

Availableathttp://www.w3.org/TR/xmlschema-0/.



XMLSchemaPart1:Structures

AnXMLvocabularyfordescribingthestructuresofXML

vocabularies,basedonamixtureofmarkupandobjectorienteddesign.Availableat

http://www.w3.org/TR/xmlschema-1/.



XMLSchemaPart2:Datatypes

AsetofextensibletypesfordescribingthecontentsofXML

elementsandattributes,includingthingslikeintegers,

decimals,anddates.Availableat

http://www.w3.org/TR/xmlschema-2/.

Themechanismsfordefiningstructuresanddatatypesallow

schemadesignerstocreatetypesystemsthatmaybeextended



orrestricted.ThisbriefappendixwillfocusonthepartsofXSD

youneedtodefinedocumentstructures,andleavesadvanced

featureslikeextension,restriction,substitutiongroups,and

keysformoredetailedexplorationinotherbooks.



FormoreinformationonXSDgenerally,seeEricvanderVlist'sXML

Schema(O'Reilly)orPriscillaWalmsley'sDefinitiveXMLSchema

(Prentice-Hall).ThePrimernotedabovemayalsobeagoodplaceto

start.



C.2CreatingaSimpleSchema

Asasimpleexampletogetyoustartedbuildingschemas,

examinethestructureofExampleC-1.Youmayhaveseenthe

documentbefore(itwasExampleA-1inAppendixA),butthis

timedoaninventoryofthepartsitcontains.



ExampleC-1.AsimpleXMLdocumentfor

definitioninaschema







EdwardLear

British





IsaacAsimov

American









Thisdocumentcontainsanauthorselement,whichitself

containsmultiplepersonelements.Eachpersonelementhas

anidattributeandmaycontainanameandanationality

element.Fornow,we'lltreatallofthetextualcontentofthe

elementsandattributesastext.Onewaytodefinethis

documentinaschemaiswithaschemawhosestructure

mirrorsthedocumentshowninExampleC-2,calleda"Russian

doll"schemaafterthewoodenmatruschkas.Thenamesofthe

elementsbeingdefinedareinboldtomakeiteasiertoread.



ExampleC-2.A"russiandoll"schemathat

describesExampleC-1.







































Thisschemastartsbydefiningtheauthorselement,whichwill

betherootelementforthedocument,anditscontents.

Becausetheauthorselementcontainsmorethansimpletext,it

isdefinedasanxs:complexType.Thattypecontainsa

sequenceofpersonelements.Thepartsofthedeclarationthat

pertainonlytotheauthorselementareshownhere:









...













Thedefinitionofthepersonelementitselfcontainsan

xs:complexTypecontaininganxs:sequence,thistime

specifyingthatnameandnationalityelements(eachofwhich

onlycontainastring)mayappearinthatsequence.The

xs:complexTypeforthepersonelementalsocontainsa

definitionfortheidattribute.





















Becausethenameandnationalityelementsandtheid

attributejustcontainstrings,theyare"simple"relativetothe

complextypesusedfortheelementsthatcontainthem,soa

declarationlike:







issufficienttosay"thenameelementwillappearhereand

containastring."

ThereareafewotherpiecestoexamineinExampleC-2,

notablythemaxOccursandminOccursattributeson

xs:element,andtheuseattributeonxs:attribute.

Youcanwritethesameschemainamoremodularway,shown

inExampleC-3.Again,thenamesofelementsarebolded.



ExampleC-3.Adifferentstyleofschemathat

describesExampleC-1





























































Insteadofnestingallthedeclarationsintoonexs:element,this

versionseparatesallthedeclarationsintoseparatepieces.Only

onenewpieceisneededtodothis,therefattributeon

xs:elementandxs:attribute.Writingschemasthiswayis

frequentlysimpler,asitallowsyoutoreusethesameelements

inmultipleplacesandbecauseitseparatesinformationabout

howoftenanelementorattributemayappear(maxOccurs,

minOccurs,anduse,whichgowiththeref)fromthe

informationaboutanelementorattribute'scontent(thetype

attribute,xs:complexTypechildelement,andsoon).

Whenthexs:elementandxs:attributedeclarationsare

movedouttobeimmediatechildrenofthexs:schemaelement,

theybecomeglobalelementsandattributes,accessibleforuse

inanydeclaration.Elementsalsobecomepossibleroot

elementsforthedocument,soOfficeapplicationsmayask

whichelementtouseastherootifgivenschemaswrittenin

thisstyle.(It'sgenerallyeasiertokeepxs:attribute

declarationsinsideoftheelementsthatusethem,orin

attributegroups,describedlater,ratherthanasglobals.)

IfyouloadeitheroftheseschemasintoanExcelXMLmap(as

describedinChapter6)andloadExampleC-1intothemap,

you'llgettheresultshowninFigureC-1.



FigureC-1.AnXMLmapusingtheschemain

ExampleC-2andExampleC-3,loadedwiththe

datafromExampleC-1



Whilethetwoschemasaredifferent,themodeltheydescribeto

Excel(orWord,oranyotherschema-processingsoftware)is

exactlythesame.Forsomerecord/fieldbasedvocabularies,the

simplestructurespresentedinExamplesExampleC-2and

ExampleC-3aremorethanenoughtogetworkaccomplished.



C.3SchemaParts

ThesimpleschemasinExamplesExampleC-2andExampleC-3

usealotofpiecesofXSD,andyoucanusethemasmodelsfor

futureschemas,buttherearealotmoreoptionsavailable,even

inthemostreadilyusablesubsetofXSD.



C.3.1Namespaces

Theonlynamespacedeclarationtoappearineitherexample

wasthenamespacedeclarationforXSDitself:

xmlns:xs="http://www.w3.org/2001/XMLSchema"



Inthiscase,theschemawasdefiningavocabularythatwasnot

inanamespace,sotherewasnoneedtodefineanadditional

namespace.If,asistypical,yourschemasdefinevocabularies

thatareinanamespace,you'llneedtodefinethenamespace

ontherootxs:schemaelement.ExampleC-4showsaslightly

modifiedversionofExampleC-3,definingthevocabularyas

belongingtothehttp://simonstl.com/ns/authors/

namespace.Changestotheschemaappearinbold.



ExampleC-4.ExampleC-3rewrittentosupporta

namespace






Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Appendix C. The XSD You Need for Office

Tải bản đầy đủ ngay(0 tr)

×
x