Tải bản đầy đủ - 0 (trang)
Appendix D. Using DTDs and RELAX NG Schemas with Office

Appendix D. Using DTDs and RELAX NG Schemas with Office

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

D.1WhatAreDTDs?

DocumentTypeDefinitionsexpresstheallowedelementsand

attributesinacertaindocumenttypeandconstraintheorderin

whichelementsmustappearwithinthatdocumenttype.ADTD

isoftencomposedofasinglefile,whichcontainsdeclarations

definingtheelementtypesandattributelists.(Intheory,aDTD

mayspanmorethanonefile;however,themechanismfor

includingonefileinsideanotherparameterentitiesisoutsidethe

scopeofthisappendix.)



D.1.1ElementTypeDeclarations

Anelementistheactualinstanceofthestructureasfoundinan

XMLdocument,whereastheelementtypedefinestheelement,

givingitanameandastructure.Theformofanelementtype

declarationis:





Theallowablecontentdefinedbycontentspecisdefinedin

termsofasimplegrammar,whichallowstheexpressionof

sequence,alternatives,anditerationwithinelements.Fora

formaldefinitionoftheelementtypedeclaration,seeSection

3.2oftheXML1.0specificationathttp://w3.org/TR/RECxml#NT-elementdecl.TableD-1introducesthemostcommon

constructs.

TableD-1.Elementtypecontentspecifications

Contentspecification



Meaning

Theeelementmaycontaincharacterdatathatis,text(and







possiblyentityandcharacterreferences).







Theeelementhasnocontentthatis,itcanonlyappearas

or.







Theeelementmaycontaincharacterdataoranyother

elementdefinedintheDTD.







Theeelementmustcontainatleastoneaelementandmay

containmultipleaelements.(Theplusmeans"oneor

more.")







Theeelementmustcontainthefollowingsequence:onea

element,followedbyzeroormorebelements,followedby

oneormorecelements.Theasteriskmeans"zeroormore."



Theeelementmaycontainbelementsorcharacterdata,

andtheycanallbemixedtogether.







Theeelementmaycontainzeroormorea,b,orcelements,

inanyorder.



Foradocumenttobevalid,theDTDmustprovideanelement

typedeclarationforeveryelementusedinthedocumentand

thecontentsofallofthoseelementsmustconformtothe

contentmodelsspecifiedintheelementtypedeclaration.

Elementtypedeclarationsleaveoffoneimportantaspectof

elements,however:attributes.



D.1.2AttributeListDeclarations

InsideaDTD,permissibleattributesarespecifiedonaperelementbasis.Anattributelistdeclarationtakesthisform:







Intheattributedefinitions,youhavetoidentifytheattribute's

nameandtype,whethertheattributeisoptionalorrequired,

and,ifnecessary,theattribute'sdefaultvalue.Unlikeelements,

youcanspecifydefaultvaluesforattributes,whichareinserted

byanapplicationwhenitparsestheXMLdocument,evenif

they'renotexplicitlywritteninthedocument.Attributescan

storeallkindsofcontent,butthemaintypesusedareCDATA

(characterdata,includingentityandcharacterreferences),ID

(identifierswhosevaluemustbeuniquewithinthedocument),

andIDREFandIDREFS(whichpointtoIDvalues).Attribute

definitionsmayalsospecifyalistofacceptablevaluesrather

thanagenerictype.Attributetypesareonlyasubsetofthe

XSDtypesdescribedinAppendixCallofthemaretextual.Table

D-2showssomecommonattributedefinitions.

TableD-2.Attributedefinitions

Attributedefinition



Meaning



subjectCDATA#REQUIRED



Thesubjectattributemustalwaysbepresent

anditshouldcontainonlycharacterdata.Ithas

nodefaultvalue.



ratingCDATA#IMPLIED



Theratingattributeisallowed,butnot

mandatory.Ithasnodefaultvalue.



play(scissors|paper|stone)"stone" Theplayattributemaytakeonlythevalues

scissors,paper,orstone.Ifitisnotspecified,

itisassumedtotakethedefaultvaluestone.



colorCDATA#FIXED"purple"



Thecolorattributemusttakethevaluepurple.

Ifitisnotspecifiedontheelement,the

processingapplicationprovidespurpleasa

defaultvalue.



Here'sacompleteattributedeclarationforafictitiousanimals

element,whichmusthaveaname,eithertwoorfourlegs,and,

optionally,anotefield:


nameCDATA#REQUIRED

legs(two|four)"four"

notesCDATA#IMPLIED>



Whileattributescanbeveryusefulforannotations,Microsoft

Officetendstouseelementcontentforinformationthat's

presenteddirectly.Youcancertainlyuseattributes,butyou

mayfinditeasiertostickwithelementsunlessyouhavea

particularreasontochooseattributes.



D.1.3PuttingitTogether

TodemonstrateacompleteDTD,we'llexploreadocumentand

itsDTD.ThedocumentisshowninExampleD-1,whiletheDTD

isshowninExampleD-2.



ExampleD-1.AvalidXMLdocument











EddDumbill

British





SimonSt.Laurent

American





EricvanderVlist

French







TheDOCTYPEdeclarationatthetopofExampleD-1assumes

thattheDTDfileshowninExampleD-2hasbeenplacedona

webserveratexample.com.Notethatthedocumenttype

declarationspecifiestherootelementofthedocument,notthe

DTDitself.(YoucouldusethesameDTDfordocumentsthat

usedperson,name,ornationalityastherootelementofa

validdocument.)



ExampleD-2.TheDTDforExampleD-1








abbrevCDATA#REQUIRED>









TheDTDdefinesthestructuresyoufindinthedocument.There

isanauthorselementtypethatmaycontainzeroormore

personelements.Inthisdocument,wehavethreeperson

elements.Thereisapersonelementtypethatmustcontaina

nameelementfollowedbyanationalityelement.Eachofthe

personelementsinthedocumenthasthosepartsinthatorder.

Thepersonelementsarerequiredtohaveanattributenamed

abbrev,andallofthemdo.Finally,thenameelementtypeand

thenationalityelementtypecanonlyholdtextualcontent.All

ofthenameandnationalityelementsheredothat.

AvalidatingXML1.0processorisrequiredtochecktheinput

documentagainstitsDTD.Ifitdoesnotvalidate,errorsare

reportedtotheapplication,whichtypicallyrejectsthe

document.Non-validatingprocessorswillacceptthedocument

evenifitdoesn'tconformtostructuresdefinedbytheDTD,and



justusetheDTDforthingslikedefaultvaluesforattributes.

MicrosoftOfficeandmostMicrosofttoolsusenon-validating

XML1.0parsers.(Schemavalidationisaseparateprocess,

definedlongafterXML1.0wasfinished.)



D.1.4OtherDTDFeatures

DTDsincludeanumberofotherfeaturesthataren'tcovered

here.Parameterentitiesandconditionalsectionsmakeit

possiblefordeveloperstocreatemoreflexibleDTDs,turning

featuresonandofforreusingthem.Documentscancontain

internalsubsetsintheDOCTYPEdeclaration,addingtheirown

informationtothedocumenttypedeclaration.Entity

declarationsmakeitpossiblefordeveloperstocreatenamed

referencestocontent,makingitsimplertoincludeexternalfiles

orcharactersnoteasilyaccessedfromthekeyboard.Notation

declarationsandunparsedentitiesmakeitpossibletocreate

metadataandincludenon-XMLcontent,thoughthesearerarely

used.DTDdonotsupportnamespacesorXMLSchema

datatypesdirectlyatall.

WhileMicrosoftOfficeapplicationscanprocessthesefeatures

whenopeningafile(exceptfornotationsandunparsedentities,

whichitignores),alloftheDOCTYPEinformationisremoved

whenthedocumentissavedbackout.BecauseXSDprovides

nosupportatallforentities,youcan'tpreservetheentity

informationfromanXMLDTDinaschemaandusethatwith

Office.



D.2WhatIsRELAXNG?

TheW3CXMLSchemaprocessbeganwithhighambitionstobe

amorepowerfulalternativetoDTDs,butmanypeoplefound

XSDtobemoretroublethanitwasworth.XSDisdifficultfor

manypeopletocreate,difficulttoprocess,hasareas(notably

blockandfinal)thatarefairlycontentious,andnoteveryone

wantstodefinetheirdocumentsintermsofobjectinheritance

anyway.WhileXSDhasdonewellinsomefieldsofXMLwork,

andMicrosofthasimplementeditthroughoutitsproductline,

therewasaplainneedforanalternative.

RELAXNG,whichhasdevelopedlargelyfromworkdonebyXML

pioneersMurataMakotoandJamesClark,hasmathematical

foundationsratherthantheadhocobjectstructuresusedby

XSD.Fortunately,youdon'tneedtoknowthemathtousethe

schemas,butthesefoundationsmakeitalotsimplertoboth

useandprocessRELAXNG.RELAXNGcomesinbothanXML

syntaxandacompactsyntax,butwe'llfocusonthecompact

syntaxherebecauseit'sgenerallyquiteapproachable.

RELAXNGisbeingdevelopedattheOrganizationforthe

AdvancementofStructuredInformationStandards(OASIS),a

differentspecificationdevelopmentorganizationfromtheW3C,

andstandardizedthroughtheInternationalOrganizationfor

Standardization(ISO)aspartoftheDocumentSchema

DefinitionLanguages(DSDL)effort.FormoreonOASIS

developmentofRELAXNG,seehttp://www.oasisopen.org/committees/relax-ng/.FormoreontheDSDLwork,

seehttp://dsdl.org.



D.2.1ABasicRELAXNGSchema

ForourfirstRELAXNGschema,we'llstartwithExampleD-3,



whichisthesamedocumentshowninExampleD-1except

withouttheDOCTYPEdeclaration.



ExampleD-3.AsampleXMLdocument







EddDumbill

British





SimonSt.Laurent

American





EricvanderVlist

French







DescribedinRELAXNGCompactsyntax,theschemaforthis

documentcanresembletheschemasshowninExamples

ExampleD-4andExampleD-5.ExampleD-4usesanested

syntax.



ExampleD-4.AnestedRELAXNGschema

elementauthors{

elementperson{

attributeabbrev{text},

elementname{text},

elementnationality{text}

}*

}



ThecurlybracesworkmuchlikethoseinCstructs,definingthe

contentsofnamedcomponents.Thisschemadefinesan

authorselement,whichcontainszeroormoreperson

elements.(Thezeroormorecomesfromtheasteriskafterthe

closingbraceforperson.)Thepersonelementshave

mandatoryabbrevattributesandnameandnationality

elements,allofwhichstoretheircontentsastext.Ifyouprefer

amoredeclarativeapproach,RELAXNGalsosupportsthat

option.ExampleD-5usesamoreDTD-likedeclaration

approach.



ExampleD-5.AdeclarativeRELAXNGschema



start=authors



authors=elementauthors{person*}

person=elementperson{abbrev,name,nationality}

abbrev=attributeabbrev{text}

name=elementname{text}

nationality=elementnationality{text}



Thisapproachreadsdifferently,butdescribesthesame

structure.Insteadofjuststartingwiththeauthorselement,it

explicitlylistspossiblerootelementsinthestartdeclaration.

Eachdeclarationdescribesthecontentsofoneelementor

attribute.Thedifferencebetweenattributeandelement

declarationsismuchsmallerinRELAXNGthaninXSDorin

DTDs,andtheabbrevattributeisattachedtotheperson

elementjustlikethenameandnationalityelements.

Elementsandattributesthatcontaintextjustlisttextastheir

content.

Tovalidatedocumentsagainsttheseschemas,youcanuse

JamesClark'sJingtool,whichisincludedwithTrang,thetool

we'llbeusinglaterinthisappendixtoconvertRELAXNGtypes

intoXSD.Gotothedirectorywhereyou'veunzippedTrang,and

youcanrunthevalidatorbytypingthefollowing:

java-jarjing.jar-cappD-4.rncappD-3.xml



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

Appendix D. Using DTDs and RELAX NG Schemas with Office

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

×
x