Tải bản đầy đủ (.pdf) (179 trang)

Addison wesley XML a managers guide 2nd edition aug 2002 ISBN 0201770067

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.59 MB, 179 trang )

Chapter3.RelatedStandards
ExecutiveSummary
OverviewofNamespaces
OverviewofXPath,XPointer,andXQuery
OverviewofXSLT
OverviewofXSL
OverviewofXLink
OverviewofXMLSchema
TechnicalStandards
InfrastructureStandards
TechnicalSummary


ExecutiveSummary
XMLdocumentsandDTDsprovidethefoundationforan
evolvingInternetdocumentparadigm.Aspeopledeveloped
XMLapplications,theynaturallyidentifiedadditionalfeatures
theywantedtohaveandtroublesomedrawbackstheywanted
toavoid.Suchdiscoveriesinspiredfurtherstandardsefforts
builtonthefoundationofXMLdocumentsandDTDs.Astheuse
ofXMLhasevolved,thisprocesshasledtoacomplicated
landscapewhereoldspecificationeffortslanguishforextended
periodsoftimeandneweffortsrapidlyachieveacceptance.
Therefore,identifyingthestandardsrelevanttoyourneedscan
besomethingofachallenge.
Categorizingthevariousspecificationsoffersapotentialaidto
navigatingthelandscape.Figure3-1presentsonepotential
categorizationofXML-relatedstandards.ItexpandsonFigure
2-1byrevealingthedetailedstructureofboththeplatform
standardsandthedomainstandards.Thiscategorizationcan
helpyouidentifythedepthofunderstandingyouneedforany


givenstandard.
Figure3-1.CategoriesofRelatedStandards


Thelinebetweenplatformanddomainstandardsissomewhat
fuzzy.Asaruleofthumb,platformstandardspotentiallyadd
capabilitiestoXMLdocumentsofalltypes,whiledomain
standardsdefineanXMLformattoencodeinformationfora
narrowerpurpose.Ingeneral,astandardthatcouldultimately
affectamajorityofXMLdevelopersisprobablyaplatform
standard.Withinthiscategory,therearethreesubcategories.
1. Technicalstandards.Standardsinthiscategory
provideaprecisetechnicalfoundationforXML
processingcomponentstoworkcorrectly.Without
thesestandards,ambiguitycouldcauseunexpected
resultswhenworkingwithdocuments.Themost
importantisXMLInfoset,whichprovidesan
underlyingdatamodelforXMLdocuments.
Featurestandards.Standardsinthiscategoryaddnew
featurestotheoverallXMLdocumentparadigm.Withoutthese
standards,peoplecouldnotdoallofthethingsthattheywant


todowithXMLdocuments,andtheparadigmwouldbemuch
lessvaluable.Table3-1liststhemostimportantfeature
standards.
Infrastructurestandards.Standardsinthiscategoryprovide
crucialinfrastructurenecessaryformanyXMLapplications.
Theyarelow-levelapplicationsofthefeaturestandardsthat
provideservicesusedbyhigher-levelapplications.Standards

suchasXHTMLandXFormsspecifyhowtointegrateXMLwith
Webtechnologies.Others,suchasSOAPandXMLEncryption,
specifyauniformwaytoperformcommonoperations.Without
thesestandards,XMLapplicationswouldbeincompatiblewith
existingWebapplications,andalldeveloperswouldhaveto
reinventthesameinfrastructure.
Itisimportanttohaveathoroughunderstandingoffeature
standardsbecausetheywillhelpyoutodeterminewhetherXML
suppliesthecapabilitiesnecessarytosolveagivenbusiness
problem.Ontheotherhand,mostmanagersonlyneedto
understandtechnicalandinfrastructurestandardsataveryhigh
level.Ageneralknowledgeoftechnicalstandardscouldhelp
youunderstandthesourceofadefectinathird-party
componentthatdelaysaproject.Ageneralknowledgeof
infrastructurestandardscouldhelpyouensurethatyour
developersmaximizetheuseofexistingsolutions.Detailed
comprehensionoftechnicalandinfrastructurestandardsis
usuallynecessaryonlyfordevelopersworkingwithsoftware
componentsthatimplementthem.
Figure3-1alsoshowsthestructureofthedomainstandards.
TheXMLparadigmincludesbothhorizontalandverticaldomain
standards.Horizontaldomainstandardsprovideasolutionfor
manipulatinginformationspecifictoatypeofapplicationbut
usedinmanyindustries.Examplesincludeportableformatsfor
vectorgraphics(SVG),multimedia(SMIL),andmathematical
expressions(MathML).Theygenerallyfacilitatecommon
softwarecomponentsacrossdifferentindustries.Their


applicabilitydependsonwhetheryourapplicationmanipulates

theparticulartypeofdatatheydescribe.
Theverticaldomainstandardsprovideformatsfordescribing
informationspecifictoaparticularindustrybutusedbymany
typesofapplications.Examplesincludeinteroperabilityformats
forfinance,healthcare,andtelecommunications.They
generallyfacilitatecompatibilityamongsystemsusedwithinthe
sameindustry.Theirapplicabilitydependsonwhetheryour
organizationworkswithinformationfromaparticularindustry.
Table3-1.XMLFeatureStandards

Standard
XMLNamespaces
XMLPath
Language
XMLPointer
Language
XMLQuery
Language
XSL
Transformations
Extensible
Stylesheet
Language
XMLLinking
Language
XMLSchema

Abbreviation Purpose
Namespaces Preventoverlapofnames
usedbydifferentsoftware

XPath
Addressdatawithina
document
XPointer
Specifylocationswithina
document
XQuery
Searchfordatawithinaset
ofdocuments
XSLT
Transformdatafromone
formattoanother
XSL
Presentdatainapaginated
format
XLink
N/A

Linkdatafromdifferent
documents
Specifymoreextensive
formatdefinitionrulesthan
DTDs

Table3-1liststhemostimportantfeaturestandards.Therestof
thischapterreviewstheimportantdetailsofthesestandards.At
theendofthechapter,thereisasummaryofkeytechnicaland


infrastructurestandards.



OverviewofNamespaces
Asyousawinthepreviouschapter,theprimaryunitsofcontent
inXMLareelementsandattributes.Authorsdistinguishamong
differenttypesofelementsandattributesbyassigningthem
uniquenames.Whenapplicationsprocessadocument,they
associateelementcontentwiththecorrespondingelement
nameandattributecontentwiththecorrespondingattribute
name.
AspeoplestartedtouseXML,theyrealizedthatorganizations
andapplicationoftenusethesamenametodescribedifferent
concepts.Whentheseorganizationswanttoexchange
informationusingXML,conflictsmayoccur.Asingledocument
oftencombinesinformationintendedformultipleorganizations
andapplications.Forexample,mostmailordervendorsdonot
chargecustomersuntiltheyshiptheorderedgoods.Enforcing
thispolicyrequiresprocessingthesameinformationwithan
AccountingapplicationforbillingpurposesandaFulfillment
applicationforshippingpurposes.
ButwhatiftheAccountingandFulfillmentapplicationsusethe
term"status"tosignifydifferentmeanings?Bothapplications
wanttheirrespectivestatusdataassociatedwiththe"Status"
elementname.Suchanamingcollisionhasthepotentialto
makelifeveryuncomfortablefordevelopersresponsiblefor
specifyingtheformatsofaffecteddocuments.
XMLNamespaces,aW3CRecommendation,enables
developerstoavoidnamingcollisionsbyassigningelementand
attributenamestonamespaces.AccountingandFulfillment
departmentscouldeachhaveseparatenamespaces.Developers

cantherebyqualifytheuseofthe"Status"elementnamewith
theappropriatenamespace.

HowItWorks


Figure3-2illustrateshownamespacescanresolvetheconflict
betweentheAccountingandFulfillmentapplications.A
ReconciliationDocumentconsolidatesbillingandshipping
informationaboutagivenorder.Whenthebillingstatusis
"invoiced"andtheshippingstatusis"shipped,"thedocument
goestoboththeAccountingandFulfillmentapplications.
Figure3-2.ResolvingaConflictwithNamespaces

WhentheAccountingapplicationprocessesthedocument,it
wantstouse"Status"asakeywordtosignifybillingstatus.
WhentheFulfillmentapplicationprocessesthedocument,it
wantstouse"Status"asakeywordtosignifyshippingstatus.
Moreover,thesameproblemextendstootherkeywords.
Example3-1showshowtosolvethisproblemusingXML
Namespaces.
Example3-1

xmlns:acct= />

xmlns:ful=" /><acct:Customer>
<acct:Name>BarCorporation</acct:Name>
<acct:Addressee>
AccountsPayable—BarCorporation

</acct:Addressee>
<acct:Orderacct:reference="5566-1010">
<acct:Status>invoice</acct:Status>
</acct:Order>
</acct:Customer>
<ful:Customer>
<ful:Name>BarCorporation</ful:Name>
<ful:Addressee>
LoadingDock—BarCorporation
</ful:Addressee>
<ful:Orderful:reference="A9875656">
<ful:Status>shipped</ful:Status>
</ful:Order>
</ful:Customer>
</Reconciliation>
Asyoucansee,attachingthenamespaceidentifiertoan
elementorattributenamewithacolonassociatesthat
namespacewiththatelementorattribute.Beforeyouusea
namespace,youassociatethenamespaceprefixwiththe
namespacename,usingthereservedXMLattribute"xmlns."
ThenamespacenameisaUniformResourceIdentifier
(URI),essentiallyindicatingthattheownerofanInternet
resourceistheownerofthenamespace.Inthiscase,"acct"
signifiesthenamespaceownedbytheAccountingdepartment
ofFooCompany,and"ful"signifiesthenamespaceownedby
theFulfillmentdepartmentofFooCompany.Theexampleuses
thecommonconventionofdeclaringallnamespacesinthe
documentelement.Thisconventionenableshumanreadersand
softwareprocessorstodetermineeasilyallthenamespacesthat



maybeusedsubsequently.
Attachingtheappropriateprefixtoallelementandattribute
namescanbecumbersome.Toavoidthischore,youcanalso
setadefaultnamespace,whichappliestoallcontainednames,
asshowninExample3-2.
Example3-2

xmlns=" /><Name>BarCorporation</Name>
<Addressee>
AccountsPayable—BarCorporation
</Addressee>
<Orderreference="5566-1010">
<Status>invoice</Status>
</Order>
</Customer>
xmlns=" /><Name>BarCorporation</Name>
<Addressee>
LoadingDock—BarCorporation
</Addressee>
<Orderreference="A9875656">
<Status>shipped</status>
</Order>
</Customer>
</Reconciliation>
InExample3-2,wefirstsetadefaultAccountingnamespaceby
specifyingthevalueofthe"xmlns"attributewithoutattachinga



namespaceprefix.Allnameswithinthefirst"Customer"
elementautomaticallyusetheAccountingnamespace.Thenwe
setadefaultnamespaceFulfillmentforthesecondCustomer
element.Youcanalsosetadefaultnamespaceandthenuse
thesyntaxinExample3-1tospecifyadifferentnamespacefor
particularelementsorattributes.Thecombinationofthetwo
mechanismsforspecifyingnamespacesdeliversaconvenient
andflexiblewaytoavoidnamingcollisions.

PracticalUsage
Inpractice,themostimportantapplicationofXMLNamespaces
comeswhenusingtherelatedfeaturestandards.Forexample,
agivendocumentmayhavewrappertagsfromanXML
messagingstandard,tagsfordomain-specificXMLcontent,and
tagsfromtheXLinkstandard,specifyinglinkstoother
documents.WithoutXMLNamespaces,itwouldbeimpossibleto
ensurethatthecorrecttypeofprocessingcomponentreceived
theelementsintendedforit.Asyouwillseeinthesubsequent
sectionsofthischapter,eachoftheotherfeaturestandardshas
itsownnamespacetoavoidthisproblem.
Indecidingwhethertodefineyourownnamespaces,consider
thattheneedforthemincreasesindirectproportiontothe
numberofdifferentgroupsthatwilluseadocumentformat.In
thecaseofindustry-specificstandards,thisneedisobviously
quitehigh.Theindustrystandardsbodydoesnotwantthe
formatitdefinestoconflictwithanyoftheinternallydefined
formatsofitsmembers.Anenterprisewideformatforalarge
multinationalcorporationwouldalsoalmostcertainlyneedto

useXMLNamespacessothatitwouldnotconflictwithanylocal
formats.However,aformatintendedforusebytwoparties,
eachofwhichplanstoconstructadedicatedapplication,might
beabletogetawaywithoutXMLNamespaces.Butitwould
probablybeagoodideatousethemanyway,incasethe
partiesdecidetoexpandthenumberofparticipants.Aformat
usedonlybyasingleapplicationforinternalpurposesalmost


certainlydoesnotneedXMLNamespaces.
IfyouworkforasoftwarevendorthatusesXMLinaproduct,
youwillwanttodefineanamespaceifyourXMLfeaturesare
visibletoexternaldevelopersorapplications.Typically,using
theInternetdomainnameforyourcompany,thenameofthe
product,andaversionidentifierwillbesufficient.
IfyouworkforalargeenterprisethatusesXMLinan
integrationore-commerceapplication,theproperapproachfor
constructingnamespacesislessclear.Ofcourse,youusethe
domainnameofyourcompanyforthefirstpartofthe
namespaceandaversionidentifierforthelastpart.Butthe
middleparttakessomethought.Youcanuseapaththat
mirrorsyourorganizationalchart,butorganizationalchartsmay
changeratheroften.Ifyourenterprisehasanorganizational
directorybasedonastandarddatamodelsuchasX.500or
LDAP,youmaybeabletoleveragethat.Inanycase,an
officiallydesignatedpersonshouldcoordinatethepolicyusedby
allgroupsdevelopingXMLapplications.


OverviewofXPath,XPointer,andXQuery

AnXMLdocumentrepresentsaconvenientpackageofrelated
XMLdata.However,therearemanysituationswherepeople
needtoworkwithasubsetofdatawithinasingledocumentor
anaggregationofdatafromseveraldocuments.Toprinta
shippinglabel,theymightwanttograbjusttheaddress
informationfromanorderdocument.Toprintafraud-resistant
receipt,theymightwanttoreferencejustthelastfourdigitsof
thecreditcardnumber.Toproduceanupgradenotification,
theymightwantcontactinformationfromeveryorder
documentthatincludesthepurchaseofaspecificproduct.To
performthesetypesofoperations,ithelpstohavethemeans
ofspecifyingsuchsubsetsandaggregationsofdocumentdata.
XMLPathLanguage(XPath)providesasyntaxfor
addressingindividualnodeswithinadocumenthierarchy.
Developersspecifyapathfromthedocumentnodetothetarget
node.Itincludesbasicselectioncriteriasothataparticular
XPathexpressioncandifferentiateamongsimilarnodes.Tograb
onlythebillingaddressfromorders,adevelopercoulduse
XPathtospecifythe"Address"elementwithinan"Order"
documentwhose"addType"attributeissetto"ship"or
"billship."XPathisawidelyusedW3CRecommendation.
Whenreferringtocontentwithinadocument,authors
sometimesneedmoreprecisionthanXPathprovides.XML
PointerLanguage(XPointer)extendstheXPathsyntaxto
includethenotionsofpointsandranges.Withpoints,authors
canspecifylocationswithinaparticularnode.Withranges,they
canspecifyasetofinformationthatcrossesnodeboundaries.
Tocreateafraud-resistantreceipt,anauthorcoulduseXPointer
tospecifythelastfourcharactersofthe"Number"element
withinthe"Card"elementofan"Order"document.XPointeris

usefulprimarilyforhypertextapplicationsandistakingalong
timetoworkitswaythroughtheW3Cstandardsprocess.


Aswithotherformsofdata,peopleoftenwanttosearchamong
collectionsofXMLdocuments.XMLQueryLanguage(XQuery)
providesaframeworkthatenablesdeveloperstoindicatethe
datatheywant,thecriteriathisdatamustsatisfy,and
intermediateprocessingsteps.ItusesXPathastheformatfor
specifyingmanyoftheseparameters.Tocreateamailinglistfor
aproductupgradenotice,adevelopercoulduseXQueryto
search"Order"documentsfor"Address"elementswithan
"addType"attributesetto"ship"or"billship"wherethe
documenthasa"Product"elementwhosevalueis"FooBar
Version5."TheexpectationsforadoptionofXQueryasa
standardsearchsyntaxareveryhigh,butitwasstillaWorking
Draftatthetimeofthiswriting.

HowTheyWork
WithXPath,youspecifyaparticularnodeorsetofnodesby
indicatinganavigationpath.Supposeyouwantedtoreferto
eachofthe"Description"elementsinourexampleorderfrom
Example2-6.YoucouldusethreeseparateXPathexpressions
toaddresseachofthethreedesiredelementsindividually,as
showninExample3-3.
Example3-3

/Order/LineItems/LineItem[1]/Description
/Order/LineItems/LineItem[2]/Description
/Order/LineItems/LineItem[3]/Description

NoticethateachexpressioninExample3-3providesafullpath
fromthedocumentelementtothetargetelement.Eachone
usesanindexinbracketstoindicateaspecific"LineItem"
elementbasedontheorderinwhichtheyappearinthe
document.It'ssomewhatinconvenienttouseadifferent
expressionforeach"LineItem"element,especiallyifyoudon't


knowbeforehandhowmanythereare.Example3-4showshow
toreturnallthe"Description"elementsinadocument,
regardlessofhowmanythereare.
Example3-4

//Description
Theuseofadoubleslashratherthanasingleslashindicates
thatdesired"Description"elementsmayhaveanypathtothe
documentelementratherthanthespecificoneindicatedin
Example3-3.Thissingleexpressionreturnsallthethree
"Description"elementsinourexampleorderdocumentatthe
sametime.Whenmultipleelementsmeetthesamecriteria,
XPathreturnsthesetofallmatchingelements.However,you
maynotwantalltheelements.Considerthepreviously
discussedscenarioofaccessingonlythe"Address"elementwith
an"addType"attributeofeither"ship"or"billship."Example3-5
showstheappropriateXPathexpression.
Example3-5

//Address[@addType="ship"or@addType="billship"]
Thedoubleslashfirstselectsallthe"Address"elements.Then
thequalificationoftheseelementsappearsinbrackets.But

insteadofanindexcorrespondingtothedocumentorderofthe
element,Example3-5specifiesamoredynamicconstraint
basedonthe"addType"attribute.Becauseabillingaddress
couldhavean"addType"valueofeither"bill"or"billship,"the
expressionconcatenatesthetwopossibilitieswith"or."
AlloftheseXPathexpressionsreturndocumentnodes.When
workingwithtextdocuments,authorsmaywanttospecifya
pointthatmarksthebeginningorendofatextselection.This


featureiswhatweneedtosolvetheproblemofselectingthe
lastfourdigitsofacreditcardnumber.Thefirststepinsolving
thisproblemisspecifyingapointjustafterthe12thdigitofa
16-digitnumber.Example3-6showshowtouseXPointerfor
thispurpose.NotethatthesyntaxofanyfinalXPointer
Recommendationmaydiffersomewhatfromthatusedhere.
Example3-6

xpointer(//Payment/Card/Number
/child::text()/point()[position()=12])
ThefirstpartofthisXPointerexpressionusesXPathsyntaxto
narrowthescopeofthematchtothe"Number"elementofthe
"Card"elementofany"Payment"elementwithinthedocument.
Thislimitationpreventsanunintendedmatchwithina
documentthatalsousesa"Number"elementaspartof
somethingotherthanacreditcardnumber.Thisexpression
thengoesbeyondXPath,usingthe"child:text"clausetonarrow
furtherthescopeofthematchtotheinternaltextcontentofa
node.The"position()=12"clauseplacesthepointjustafterthe
12thcharacterofthetextcontent.

Bycombiningtwopointstospecifyarange,authorscanuse
XPointertoachievetheprecisionnecessarytoindicateonlythe
lastfourdigitsofacreditcardnumber.Example3-7specifies
twoseparatepoints,usingthesyntaxfromExample3-6.The
firstpointisjustafterthe12thcharacter.Thesecondpointis
justafterthe16thcharacter.Connectingthesetwopointswith
the"range-to"functionreturnsalltextbetweenthepoints—in
thiscase,the13th,14th,15th,and16thdigitsofthecredit
cardnumber.Pointscanevencrossnodeboundaries.This
capabilityisusefulforquotingpassagesintextdocuments.Itis
possibletograballofthetextfromthe2ndparagraphofthe
3rdchaptertothe5thparagraphofthe4thchapterusinga
similarXPointerexpression.


Example3-7

xpointer(//Payment/Card/Number
/child::text()/point()[position()=12]
/range-to(//Payment/Card/Number
/child::text()/point()[position()=16]))
WhileXPointerprovidesprecisionforspecifyingmatcheswithin
asingledocument,XQueryprovidesprecisionforspecifying
matchesacrossmultipledocuments.Thebasicframeworkof
XQueryisthe"FLWR"expression—FOR,LET,WHERE,RETURN—
althoughthefinalsyntaxmayevolvefromthatusedinWorking
Draft.Wecanusethisframeworktocreateamailinglistto
notifycustomersofanupgradeopportunityfromahypothetical
collectionofdocumentsthatusesthe"Order"DTDfrom
Example2-9.

Example3-8wrapsthequeryexpressionina"MailingList"
elementsothatthequeryenginecangenerateawell-formed
XMLresultsdocument.The"FOR"clausespecifiesthescopeof
thequeryas"Order"documentswithinthe"Orders"collection
atFooCompanyandassignsthesedocumenttothe"o"
variable.The"WHERE"clauseconstrainsthematched
documentstothosewherea"Description"elementhasthe
value"FooBarVersion5."
The"RETURN"clauseindicatesthattheresultsdocument
shouldwrapindividuallyreturnedrecordsinan"Addressee"
element.Itfiltersthereturnedinformationdowntoallchild
elementsofbillingaddresselementsusinganXPathexpression
likethatofExample3-5.Example3-8doesn'tusea"LET"
clause.Thisclauseisusefulforsophisticatedquerieswhere
developersneedtomanipulateintermediateresultstogenerate
thefinalresults.
Example3-8


<MailingList>
FOR$oINdocument
("")/Orders/Order
WHERE$o//Description="FooBarVersion5"
RETURN
<Addressee>
$o//Address[@addType="ship"or
@addType="billship"]/*
</Addressee>
</MailingList>
Atthetimeofthiswriting,theW3CwasrapidlyrevisingXPath

andXQuery.XPath2.0andXQuery1.0werebothWorking
Drafts.Thepurposeoftheserevisionsistoresolvetwoprimary
issues.ThefirstistoensurecompatibilitywiththeXMLSchema
datamodel.ItwouldbeniceifXPathexpressionscoulduse
constraintsbasedonthebasicdatatypesdefinedbyXML
Schema.ThesecondistoensurecompatibilitybetweenXPath
andXQuery.ItmakesagreatdealofsensetouseXPathasthe
syntaxforexpressingmanyoftheXQueryparameters.
However,thereareseveralsubtleissuesconcerningXPath1.0
thatinhibitthiscompatibility.XPath2.0willresolvetheseissues
tothepointwhereXQuery1.0willincludeXPath2.0asa
subset.

PracticalUsage
DevelopersandauthorshavebeenusingXPathforsometime,
buttheyarejustbeginningtoexperimentwithXPointerand
XQuery.XPointerandXQueryincorporatemuchoftheXPath
syntax.ThesefactorsmakeXPaththemostimportantfeature
standardforaccessingpartsofdocuments.Managersshould
ensurethatalldevelopersworkingonXMLprojectsare
comfortableusingXPath.


ThebiggestuseforXPathisasameansofpassingreferences
tospecificdocumentnodesamongsoftwarecomponents.
Programminglanguagesmakeextensiveuseofpointersor
handlestorefertospecificlocationswithininternaldata
structures.XMLdocumentsareessentiallyexternaldata
structures.PointersorhandlestoXMLdocumentdatawouldbe
meaninglesstoanothercomponentthatdidnotsharethesame

memoryspace.XPathprovidestheappropriateexternal
referencemechanism.Ifonecomponentwantstotellanother
touseaparticularaddressforsendinganinvoice,itpassesthe
documentlocationandtheappropriateXPath.
XPointeroffersadvantagesoverXPathprimarilyforhypertext
applications.Intheseapplications,muchofthecontentistextorientedratherthandata-oriented,anddocumentsnaturally
tendtorefertocontentwithinotherdocuments.This
applicabilityextendstoboundarysituationswhereatextorienteddocumentreferstopartsofadata-orienteddocument
generatedbyanapplication.XPointeriscloselyrelatedtoXLink,
thefeaturestandardfocusedonXMLhypertextlinksand
discussedlater.ThemotivationforXPointerstemsfromthe
commonneedforsuchlinkstospecifyaparticularplacewithin
adocumentratherthantheentiredocument.
WhileXPathandXPointeraddresstheissueofaccessingdata
withinanindividualdocument,XQueryhandlesaccessingdata
acrossacollectionofdocuments.Althoughthestandardwas
stillunderdevelopmentatthetimeofthiswriting,the
motivationforitisclear.Anytimeanapplicationgeneratesa
significantnumberofXMLdocumentscontainingmeaningful
data,it'sonlyamatteroftimebeforesomeonewantstosearch
thosedocumentsforaspecificpieceofinformation.Incases
whereXMLisonlyonerepresentationofdataactuallymanaged
withinarelationaldatabasemodel,existingquerylanguages
andtoolswillbeadequate.ButifXMListhenative
representationofthedataoristhecommonrepresentation
providedbyotherwiseheterogeneousdatamodels,XQuery


shouldseeextensiveuse.



OverviewofXSLT
Thethreestandardscoveredintheprevioussectionenableyou
toselectrelativelysmallpartsofdocumentsinvariousways.In
somecases,youmaywanttotakethismanipulationastep
furtherbycompletelyreorganizingadocument.Thistypeof
transformationrequiresselectingdifferentpartsofadocument
andrearrangingthem.XSLTransformations(XSLT)extends
theXPathmodelofhowtoaddresspartsofdocumentswith
moresophisticatedoperatorsthatenabledeveloperstospecify
theserearrangements.
Youmaybewonderingwhy,asthenameimplies,XSLTis
associatedwithXSL.Originally,peoplesawXSLasageneric
waytodisplayXMLdocumentsforallpresentationtechnologies.
Accomplishingthisgoalnaturallyrequiredtwodifferenttypesof
features:(1)featuresforrearrangingdocumentcontentsothat
itmadethemostsensefordisplayand(2)featuresfor
attachingdisplaypropertiestothecontent.However,
rearrangingcontentturnedouttobeusefulforotherpurposes,
andtheappropriatedisplaypropertiesturnedouttobeatopic
ofextensivedebate.Becausepeoplewantedtousethe
rearrangingfeaturesandagreementonthesefeatureswasfar
aheadofthatfordisplayproperties,thetwostandards
separatedandXSLTachievedW3CRecommendationstatus
aheadofXSL.
OneofthegreatestdemandsforXSLTcamefromtryingto
avoidthemeta-incompatibilityproblemwheretwodifferent
organizationsusetwodifferent,socalled,standards.XML
enhancescompatibilitybymakingitpossibletoexchange
informationusingastandardsyntaxandtodefinestandard

formatsthatconstrainthestructureofthisinformation.But
whatiftwodifferentgroupsdefinedifferentformatsforthe
samelogicaltypeofdocument?Thatleadstometaincompatibilityandbigheadachesformanagerswhoare


developingapplicationsthatmustexchangedocumentswith
bothgroups.Insomesense,thisistheoppositeproblemfrom
theonesolvedbyNamespaces.Namespacessolvetheproblem
ofgroupscallingdifferentconceptsbythesamename.XSLT
solvestheproblemofgroupscallingthesameconceptby
differentnames.
Suchascenarioishighlylikelyinapplicationssuchassupply
chainmanagementwheretwocompanieswanttoexchange
conceptuallythesameinformationbutalreadyhaveformats
thattheyuseinternally.Also,industrygroupsinfinance,
telecommunications,andtransportationhavedefinedformats
fortransactionsinthoseindustries.Insomecases,multiple
industrygroupsareworkingonthesameproblem,creatingthe
potentialforduelingstandards.Moreover,withtheintegration
ofglobalsupplychains,standardsforrelatedindustriessuchas
manufacturingandshippingmayneedtoensurecompatibility
wheretheyoverlap.
TheideabehindXSLTistodefineascriptinglanguage—using
XMLsyntax,ofcourse—thatenablesdeveloperstotransform
oneformatintoanother.Wherevertwoformatsoverlapped,
developerswouldcreateatransformthatextractsthe
overlappinginformationfromoneformatandrearrangesitinto
theotherformat.Thesetransformsaredirectional;torearrange
documentsinbothdirections,youwouldneedtwoseparate
transforms.


HowItWorks
Considerthebasicproblemofautomaticallyplacinganorder
withatradingpartnerovertheInternet.FooCompanyhas
definedaFooCompanyOrderDTDthatitusesinternally.Bar
CorphasdefinedaBarCorpOrderDTDthatitusesinternally.
NowFooCompanywantstoplaceordersautomaticallywithBar
Corp.


WhenFooCompanycreatesaFooCompanyOrderDocument,it
isvalidwithrespecttotheFooCompanyOrderDTD.However,
forBarCompanytoaccepttheorder,theorderdocumentmust
bevalidwithCorptotheBarCorpOrderDTD.Toachievethis
end,FooCompanycreatesaFooBarTransformationdocument
thatspecifieshowtotranslateaFooCompanyOrderDocument
intoaBarCorpOrderDocumentasshowninFigure3-3.
Figure3-3.TranslatingOrderFormatswithXSLT

ToseehowXSLTworks,let'sconsiderasimpleexample.
Examples3-9aand3-9bshowpartsoforderdocumentsintwo
differentformats.Example3-9amodelscurrencyinformationas
anattributeonthe"Order"element.Example3-9bmodels
currencyinformationasachildelementofthe"Order"element.
Thechoiceofmodelinginformationasanattributeorchild
elementisanarbitraryone,soitislikelythattwodifferent


DTDsforthesameconceptwouldchoosedifferentmodeling
techniquesforatleastonepieceofinformation.

Example3-9a

<Ordercurrency="USD">
...
</Order>
Example3-9b

<Order>
<Currency>USD</Currency>
...
</Order>
Example3-10istheXSLTcodefortransformingtheattribute
modelofExample3-9atothechildelementmodelofExample
3-9b.NotetheuseoftheXSLnamespacetodenotethe
elementsusingXSL-specificconstructs.Thetransformation
documentselectsthe"Order"elementinthesourcedocument.
Itthenbeginsanew"Order"elementwithanew"Currency"
childelementinthetranslateddocument.Itinsertsthevalueof
the"Currency"attributeoftheselectedorderelementinthe
sourcedocumentastheelementcontentofthe"Currency"
elementinthetranslateddocument.
Example3-10

<xsl:templatematch="/">
<xsl:for-eachselect="Order">
<Order>
<Currency>


<xsl:value-ofselect="@currency"/>

</Currency>
</Order>
</xsl:for-each>
</xsl:template>
Asyoucansee,XSLTisitselfXML.EveryXSLTdocumentisalso
anXMLdocument.Therefore,allthetoolsforcreatingand
managingXMLdocumentsworkwithXSLTdocuments.Of
course,theydonotnecessarilyunderstandthespecificXSLT
syntax,buttheydoprovidesomeleverage.Althoughthis
exampleismotivatedbyvaliddocumentsthatusedifferent
DTDs,XSLTscriptsalsoworkwithwell-formedones.However,
peopleoftenuseXSLTwhentheyhaveinonedataformatmany
documentsthattheywanttranslatedtoanotherdataformat—
preciselythesameconditionsunderwhichtheyusevalid
documents.
Atthetimeofthiswriting,theW3Chadcommencedworkon
XSLT2.0.Theworkwasinitsbeginningstages,focusingonthe
requirementsforthenewversion.GiventheworkonXML
SchemaandXPath2.0sinceXSLT1.0appearedasa
Recommendation,oneoftheprimaryrequirementswas
compatibilitywiththerestoftheXMLstandardsfamily.Therest
oftheproposedrequirementsmostlyrevolvedaroundmaking
XSLTeasiertouse.SincethereleaseofXSLT1.0,agreatdeal
hasbeenrevealedaboutwhatpeoplewanttodowithXSLTand
whattheyarefindingtheycando.Sothereareanumberof
proposalstoclosethemostglaringgapsbetweenfrequencyof
needtoperformatypeofoperationandthedifficultyofactually
performingtheoperation.

PracticalUsage

XSLTisbecominganincreasinglyimportantpartoftheXML
familyofspecifications.XMLprovidesageneralgrammarfor


×