You are on page 1of 59

Chapter10:XML

DatabaseSystemConcepts
Silberschatz,KorthandSudarshan Seewww.dbbook.comforconditionsonreuse

XML
s StructureofXMLData s XMLDocumentSchema s QueryingandTransformation s ApplicationProgramInterfacestoXML s StorageofXMLData s XMLApplications

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.2

Silberschatz,KorthandSudarshan

Introduction
s XML:ExtensibleMarkupLanguage s DefinedbytheWWWConsortium(W3C) s DerivedfromSGML(StandardGeneralizedMarkupLanguage),but

simplertousethanSGML document
q

s Documentshavetagsgivingextrainformationaboutsectionsofthe

E.g.<title>XML</title><slide>Introduction</slide> Userscanaddnewtags,andseparatelyspecifyhowthetagshouldbe handledfordisplay

s Extensible,unlikeHTML
q

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.3

Silberschatz,KorthandSudarshan

XMLIntroduction(Cont.)
s Theabilitytospecifynewtags,andtocreatenestedtagstructuresmake

XMLagreatwaytoexchangedata,notjustdocuments.
q

MuchoftheuseofXMLhasbeenindataexchangeapplications,notasa replacementforHTML

s Tagsmakedata(relatively)selfdocumenting
E.g. <bank> <account> <account_number>A101</account_number> <branch_name>Downtown</branch_name> <balance>500</balance> </account> <depositor> <account_number>A101</account_number> <customer_name>Johnson</customer_name> </depositor> </bank>
q

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.4

Silberschatz,KorthandSudarshan

XML:Motivation
s Datainterchangeiscriticalintodaysnetworkedworld
q

Examples:

Banking:fundstransfer Orderprocessing(especiallyintercompanyorders) Scientificdata Chemistry:ChemML, Genetics:BSML(BioSequenceMarkupLanguage),

Paperflowofinformationbetweenorganizationsisbeingreplacedby electronicflowofinformation

s Eachapplicationareahasitsownsetofstandardsforrepresenting

information formats

s XMLhasbecomethebasisforallnewgenerationdatainterchange

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.5

Silberschatz,KorthandSudarshan

XMLMotivation(Cont.)
s Earliergenerationformatswerebasedonplaintextwithlineheaders

indicatingthemeaningoffields
q q q

Similarinconcepttoemailheaders Doesnotallowfornestedstructures,nostandardtypelanguage Tiedtoocloselytolowleveldocumentstructure(lines,spaces,etc) XMLtypespecificationlanguagestospecifythesyntax


s EachXMLbasedstandarddefineswhatarevalidelements,using
q

DTD(DocumentTypeDescriptors) XMLSchema

Plustextualdescriptionsofthesemantics However,thismaybeconstrainedbyDTDs

s XMLallowsnewtagstobedefinedasrequired
q

s Awidevarietyoftoolsisavailableforparsing,browsingandqueryingXML

documents/data

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.6

Silberschatz,KorthandSudarshan

ComparisonwithRelationalData
s Inefficient:tags,whichineffectrepresentschemainformation,are

repeated
q

s Betterthanrelationaltuplesasadataexchangeformat

Unlikerelationaltuples,XMLdataisselfdocumentingdueto presenceoftags Nonrigidformat:tagscanbeadded Allowsnestedstructures Wideacceptance,notonlyindatabasesystems,butalsoin browsers,tools,andapplications

q q q

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.7

Silberschatz,KorthandSudarshan

StructureofXMLData
s Tag:labelforasectionofdata s Element:sectionofdatabeginningwith<tagname>andendingwith

matching</tagname>
q

s Elementsmustbeproperlynested

Propernesting

<account><balance>.</balance></account> <account><balance>.</account></balance>

Impropernesting

Formally:everystarttagmusthaveauniquematchingendtag, thatisinthecontextofthesameparentelement.

s Everydocumentmusthaveasingletoplevelelement

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.8

Silberschatz,KorthandSudarshan

ExampleofNestedElements
<bank1> <customer> <customer_name>Hayes</customer_name>

</customer> . . </bank1>

<customer_street>Main</customer_street> <customer_city>Harrison</customer_city> <account> <account_number>A102</account_number> <branch_name>Perryridge</branch_name> <balance>400</balance> </account> <account> </account>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.9

Silberschatz,KorthandSudarshan

MotivationforNesting
s Nestingofdataisusefulindatatransfer
q

Example:elementsrepresentingcustomer_id,customer_name,and addressnestedwithinanorderelement Withmultipleorders,customernameandaddressarestored redundantly normalizationreplacesnestedstructuresineachorderbyforeignkey intotablestoringcustomernameandaddressinformation Nestingissupportedinobjectrelationaldatabases Externalapplicationdoesnothavedirectaccesstodatareferenced byaforeignkey

s Nestingisnotsupported,ordiscouraged,inrelationaldatabases
q

s Butnestingisappropriatewhentransferringdata
q

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.10

Silberschatz,KorthandSudarshan

StructureofXMLData(Cont.)
s MixtureoftextwithsubelementsislegalinXML.
q

Example:

<account> Thisaccountisseldomusedanymore. <account_number>A102</account_number> <branch_name>Perryridge</branch_name> <balance>400</balance> </account>


q

Usefulfordocumentmarkup,butdiscouragedfordata representation

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.11

Silberschatz,KorthandSudarshan

Attributes
s Elementscanhaveattributes

<accountaccttype=checking> <account_number>A102</account_number> <branch_name>Perryridge</branch_name> <balance>400</balance> </account>


s Attributesarespecifiedbyname=valuepairsinsidethestartingtagofan

element

s Anelementmayhaveseveralattributes,buteachattributenamecan

onlyoccuronce

<accountaccttype=checkingmonthlyfee=5>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.12

Silberschatz,KorthandSudarshan

Attributesvs.Subelements
s Distinctionbetweensubelementandattribute
q

Inthecontextofdocuments,attributesarepartofmarkup,while subelementcontentsarepartofthebasicdocumentcontents Inthecontextofdatarepresentation,thedifferenceisunclearand maybeconfusing

Sameinformationcanberepresentedintwoways <accountaccount_number=A101>.</account> <account> <account_number>A101</account_number> </account>

Suggestion:useattributesforidentifiersofelements,anduse subelementsforcontents

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.13

Silberschatz,KorthandSudarshan

Namespaces
s XMLdatahastobeexchangedbetweenorganizations s Sametagnamemayhavedifferentmeaningindifferentorganizations,

causingconfusiononexchangeddocuments

s Specifyingauniquestringasanelementnameavoidsconfusion s Bettersolution:useuniquename:elementname s AvoidusinglonguniquenamesalloverdocumentbyusingXML

Namespaces

<bankXmlns:FB=http://www.FirstBank.com> <FB:branch> <FB:branchname>Downtown</FB:branchname> <FB:branchcity>Brooklyn</FB:branchcity> </FB:branch> </bank>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.14

Silberschatz,KorthandSudarshan

MoreonXMLSyntax
s Elementswithoutsubelementsortextcontentcanbeabbreviatedby

endingthestarttagwitha/>anddeletingtheendtag
q

<accountnumber=A101branch=Perryridgebalance=200/>

s Tostorestringdatathatmaycontaintags,withoutthetagsbeing

interpretedassubelements,useCDATAasbelow
q

<![CDATA[<account></account>]]>

Here,<account>and</account>aretreatedasjuststrings CDATAstandsforcharacterdata

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.15

Silberschatz,KorthandSudarshan

XMLDocumentSchema
s Databaseschemasconstrainwhatinformationcanbestored,andthe

datatypesofstoredvalues

s XMLdocumentsarenotrequiredtohaveanassociatedschema s However,schemasareveryimportantforXMLdataexchange
q

Otherwise,asitecannotautomaticallyinterpretdatareceivedfrom anothersite DocumentTypeDefinition(DTD)

s TwomechanismsforspecifyingXMLschema
q

Widelyused Newer,increasinguse

XMLSchema

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.16

Silberschatz,KorthandSudarshan

DocumentTypeDefinition(DTD)
s ThetypeofanXMLdocumentcanbespecifiedusingaDTD s DTDconstraintsstructureofXMLdata
q q q

Whatelementscanoccur Whatattributescan/mustanelementhave Whatsubelementscan/mustoccurinsideeachelement,andhow manytimes. AllvaluesrepresentedasstringsinXML <!ELEMENTelement(subelementsspecification)> <!ATTLISTelement(attributes)>

s DTDdoesnotconstraindatatypes
q

s DTDsyntax
q q

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.17

Silberschatz,KorthandSudarshan

ElementSpecificationinDTD
s

Subelementscanbespecifiedas
q q q

namesofelements,or #PCDATA(parsedcharacterdata),i.e.,characterstrings EMPTY(nosubelements)orANY(anythingcanbeasubelement)

Example <!ELEMENTdepositor(customer_nameaccount_number)> <!ELEMENTcustomer_name(#PCDATA)> <!ELEMENTaccount_number(#PCDATA)> Subelementspecificationmayhaveregularexpressions <!ELEMENTbank((account|customer|depositor)+)>

Notation: |alternatives +1ormoreoccurrences *0ormoreoccurrences

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.18

Silberschatz,KorthandSudarshan

BankDTD
<!DOCTYPEbank[ <!ELEMENTbank((account|customer|depositor)+)> <!ELEMENTaccount(account_numberbranch_namebalance)> <!ELEMENTcustomer(customer_namecustomer_street customer_city)> <!ELEMENTdepositor(customer_nameaccount_number)> <!ELEMENTaccount_number(#PCDATA)> <!ELEMENTbranch_name(#PCDATA)> <!ELEMENTbalance(#PCDATA)> <!ELEMENTcustomer_name(#PCDATA)> <!ELEMENTcustomer_street(#PCDATA)> <!ELEMENTcustomer_city(#PCDATA)> ]>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.19

Silberschatz,KorthandSudarshan

AttributeSpecificationinDTD
s Attributespecification:foreachattribute
q q

Name Typeofattribute

CDATA ID(identifier)orIDREF(IDreference)orIDREFS(multipleIDREFs) moreonthislater

Whether

mandatory(#REQUIRED) hasadefaultvalue(value), orneither(#IMPLIED)

s Examples
q q

<!ATTLISTaccountaccttypeCDATAchecking> <!ATTLISTcustomer customer_idID#REQUIRED accountsIDREFS#REQUIRED>


10.20 Silberschatz,KorthandSudarshan

DatabaseSystemConcepts5thEdition,Aug22,2005.

IDsandIDREFs
s AnelementcanhaveatmostoneattributeoftypeID s TheIDattributevalueofeachelementinanXMLdocumentmustbe

distinct
q

ThustheIDattributevalueisanobjectidentifier

s AnattributeoftypeIDREFmustcontaintheIDvalueofanelementin

thesamedocument

s AnattributeoftypeIDREFScontainsasetof(0ormore)IDvalues.

EachIDvaluemustcontaintheIDvalueofanelementinthesame document

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.21

Silberschatz,KorthandSudarshan

BankDTDwithAttributes
s BankDTDwithIDandIDREFattributetypes.

<!DOCTYPEbank2[ <!ELEMENTaccount(branch,balance)> <!ATTLISTaccount account_numberID#REQUIRED ownersIDREFS#REQUIRED> <!ELEMENTcustomer(customer_name,customer_street, customer_city)> <!ATTLISTcustomer customer_idID#REQUIRED accountsIDREFS#REQUIRED> declarationsforbranch,balance,customer_name, customer_streetandcustomer_city ]>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.22

Silberschatz,KorthandSudarshan

XMLdatawithIDandIDREFattributes
<bank2> <accountaccount_number=A401owners=C100C102> <branch_name>Downtown</branch_name>

<balance>500</balance>
</account>

..

<customercustomer_id=C100accounts=A401> <customer_name>Joe</customer_name> </customer> <customercustomer_id=C102accounts=A401A402> <customer_name>Mary</customer_name>

<customer_street>Monroe</customer_street> <customer_city>Madison</customer_city>

</customer> </bank2>

<customer_street>Erin</customer_street> <customer_city>Newark</customer_city>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.23

Silberschatz,KorthandSudarshan

LimitationsofDTDs
s Notypingoftextelementsandattributes
q

Allvaluesarestrings,nointegers,reals,etc. Orderisusuallyirrelevantindatabases(unlikeinthedocument layoutenvironmentfromwhichXMLevolved) (A|B)*allowsspecificationofanunorderedset,but

s Difficulttospecifyunorderedsetsofsubelements
q

CannotensurethateachofAandBoccursonlyonce

s IDsandIDREFsareuntyped
q

Theownersattributeofanaccountmaycontainareferenceto anotheraccount,whichismeaningless

ownersattributeshouldideallybeconstrainedtoreferto customerelements

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.24

Silberschatz,KorthandSudarshan

XMLSchema
s XMLSchemaisamoresophisticatedschemalanguagewhich

addressesthedrawbacksofDTDs.Supports
q

Typingofvalues

E.g.integer,string,etc Also,constraintsonmin/maxvalues

q q

Userdefined,comlextypes Manymorefeatures,including

uniquenessandforeignkeyconstraints,inheritance

s XMLSchemaisitselfspecifiedinXMLsyntax,unlikeDTDs
q

Morestandardrepresentation,butverbose

s XMLSchemeisintegratedwithnamespaces s BUT:XMLSchemaissignificantlymorecomplicatedthanDTDs.

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.25

Silberschatz,KorthandSudarshan

XMLSchemaVersionofBankDTD
<xs:schemaxmlns:xs=http://www.w3.org/2001/XMLSchema> <xs:elementname=banktype=BankType/> <xs:elementname=account> <xs:complexType> <xs:sequence> <xs:elementname=account_numbertype=xs:string/> <xs:elementname=branch_nametype=xs:string/> <xs:elementname=balancetype=xs:decimal/> </xs:squence> </xs:complexType> </xs:element> ..definitionsofcustomeranddepositor. <xs:complexTypename=BankType> <xs:squence>

</xs:complexType> </xs:schema>

<xs:elementref=accountminOccurs=0maxOccurs=unbounded/> <xs:elementref=customerminOccurs=0maxOccurs=unbounded/> <xs:elementref=depositorminOccurs=0maxOccurs=unbounded/> </xs:sequence>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.26

Silberschatz,KorthandSudarshan

XMLSchemaVersionofBankDTD
s Choiceofxs:wasoursanyothernamespaceprefixcouldbe

chosen
q

s ElementbankhastypeBankType,whichisdefinedseparately

xs:complexTypeisusedlatertocreatethenamedcomplextype BankType

s Elementaccounthasitstypedefinedinline

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.27

Silberschatz,KorthandSudarshan

MorefeaturesofXMLSchema
s Attributesspecifiedbyxs:attributetag:
q q

<xs:attributename=account_number/> addingtheattributeuse=requiredmeansvaluemustbe specified

s Keyconstraint:accountnumbersformakeyforaccountelements

undertherootbankelement: <xs:keyname=accountKey> <xs:selectorxpath=/bank/account/> <xs:fieldxpath=account_number/> <\xs:key>

s Foreignkeyconstraintfromdepositortoaccount:

<xs:keyrefname=depositorAccountKeyrefer=accountKey> <xs:selectorxpath=/bank/depositor/> <xs:fieldxpath=account_number/> <\xs:keyref>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.28

Silberschatz,KorthandSudarshan

QueryingandTransformingXMLData
s TranslationofinformationfromoneXMLschematoanother s QueryingonXMLdata s Abovetwoarecloselyrelated,andhandledbythesametools s StandardXMLquerying/translationlanguages
q

XPath

Simplelanguageconsistingofpathexpressions AnXMLquerylanguagewitharichsetoffeatures SimplelanguagedesignedfortranslationfromXMLtoXML andXMLtoHTML

XQuery

XSLT

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.29

Silberschatz,KorthandSudarshan

TreeModelofXMLData
s QueryandtransformationlanguagesarebasedonatreemodelofXML

data

s AnXMLdocumentismodeledasatree,withnodescorrespondingto

elementsandattributes
q

Elementnodeshavechildnodes,whichcanbeattributesor subelements Textinanelementismodeledasatextnodechildoftheelement ChildrenofanodeareorderedaccordingtotheirorderintheXML document Elementandattributenodes(exceptfortherootnode)haveasingle parent,whichisanelementnode Therootnodehasasinglechild,whichistherootelementofthe document

q q

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.30

Silberschatz,KorthandSudarshan

XPath
s XPathisusedtoaddress(select)partsofdocumentsusing

pathexpressions
q

s Apathexpressionisasequenceofstepsseparatedby/

Thinkoffilenamesinadirectoryhierarchy

s Resultofpathexpression:setofvaluesthatalongwiththeir

containingelements/attributesmatchthespecifiedpath bank2datawesawearlierreturns

s E.g./bank2/customer/customer_nameevaluatedonthe

<customer_name>Joe</customer_name> <customer_name>Mary</customer_name>
s E.g./bank2/customer/customer_name/text()

returnsthesamenames,butwithouttheenclosingtags

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.31

Silberschatz,KorthandSudarshan

XPath(Cont.)
s Theinitial/denotesrootofthedocument(abovethetopleveltag) s Pathexpressionsareevaluatedlefttoright
q

Eachstepoperatesonthesetofinstancesproducedbytheprevious step E.g./bank2/account[balance>400]


s Selectionpredicatesmayfollowanystepinapath,in[]
q

returnsaccountelementswithabalancevaluegreaterthan400 /bank2/account[balance]returnsaccountelementscontaininga balancesubelement

s Attributesareaccessedusing@
q

E.g./bank2/account[balance>400]/@account_number

returnstheaccountnumbersofaccountswithbalance>400

IDREFattributesarenotdereferencedautomatically(moreonthis later)

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.32

Silberschatz,KorthandSudarshan

FunctionsinXPath
s XPathprovidesseveralfunctions
q

Thefunctioncount()attheendofapathcountsthenumberof elementsinthesetgeneratedbythepath

E.g./bank2/account[count(./customer)>2]

Returnsaccountswith>2customers q Alsofunctionfortestingposition(1,2,..)ofnodew.r.t.siblings
s Booleanconnectivesandandorandfunctionnot()canbeusedin

predicates
q q

s IDREFscanbereferencedusingfunctionid()

id()canalsobeappliedtosetsofreferencessuchasIDREFSand eventostringscontainingmultiplereferencesseparatedbyblanks E.g./bank2/account/id(@owner)

returnsallcustomersreferredtofromtheownersattributeof accountelements.

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.33

Silberschatz,KorthandSudarshan

MoreXPathFeatures
s Operator|usedtoimplementunion
q

E.g./bank2/account/id(@owner)|/bank2/loan/id(@borrower)

Givescustomerswitheitheraccountsorloans However,|cannotbenestedinsideotheroperators.

s //canbeusedtoskipmultiplelevelsofnodes
q

E.g./bank2//customer_name

findsanycustomer_nameelementanywhereunderthe /bank2element,regardlessoftheelementinwhichitis contained.

s Astepinthepathcangotoparents,siblings,ancestorsand

descendantsofthenodesgeneratedbythepreviousstep,notjust tothechildren
q q

//,describedabove,isashortfromforspecifyingall descendants ..specifiestheparent.

s doc(name)returnstherootofanameddocument
DatabaseSystemConcepts5thEdition,Aug22,2005. 10.34 Silberschatz,KorthandSudarshan

XQuery
s XQueryisageneralpurposequerylanguageforXMLdata s CurrentlybeingstandardizedbytheWorldWideWebConsortium

(W3C)
q

ThetextbookdescriptionisbasedonaJanuary2005draftofthe standard.Thefinalversionmaydiffer,butmajorfeatureslikelyto stayunchanged.

s XQueryisderivedfromtheQuiltquerylanguage,whichitselfborrows

fromSQL,XQLandXMLQL

s XQueryusesa

forletwhereorderbyresult syntax forSQLfrom whereSQLwhere orderbySQLorderby resultSQLselect letallowstemporaryvariables,andhasnoequivalentinSQL

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.35

Silberschatz,KorthandSudarshan

FLWORSyntaxinXQuery
s ForclauseusesXPathexpressions,andvariableinforclauserangesover

valuesinthesetreturnedbyXPath
q

s SimpleFLWORexpressioninXQuery

findallaccountswithbalance>400,witheachresultenclosedinan <account_number>..</account_number>tag for$xin/bank2/account let$acctno:=$x/@account_number where$x/balance>400 return<account_number>{$acctno}</account_number> ItemsinthereturnclauseareXMLtextunlessenclosedin{},inwhich casetheyareevaluated

s Letclausenotreallyneededinthisquery,andselectioncanbedoneIn

XPath.Querycanbewrittenas:

for$xin/bank2/account[balance>400] return<account_number>{$x/@account_number} </account_number>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.36

Silberschatz,KorthandSudarshan

Joins
s JoinsarespecifiedinamannerverysimilartoSQL

for$ain/bank/account, $cin/bank/customer, $din/bank/depositor where$a/account_number=$d/account_number and$c/customer_name=$d/customer_name return<cust_acct>{$c$a}</cust_acct>


s Thesamequerycanbeexpressedwiththeselectionsspecifiedas

XPathselections:

for$ain/bank/account $cin/bank/customer $din/bank/depositor[ account_number=$a/account_numberand customer_name=$c/customer_name] return<cust_acct>{$c$a}</cust_acct>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.37

Silberschatz,KorthandSudarshan

NestedQueries
s Thefollowingqueryconvertsdatafromtheflatstructureforbank

informationintothenestedstructureusedinbank1 <bank1>{ for$cin/bank/customer return <customer> {$c/*} {for$din/bank/depositor[customer_name=$c/customer_name], $ain/bank/account[account_number=$d/account_number] return$a} </customer> }</bank1>
s $c/*denotesallthechildrenofthenodetowhich$cisbound,withoutthe

enclosingtopleveltag

s $c/text()givestextcontentofanelementwithoutanysubelements/tags

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.38

Silberschatz,KorthandSudarshan

SortinginXQuery
s

Theorderbyclausecanbeusedattheendofanyexpression.E.g.toreturncustomers sortedbyname for$cin/bank/customer orderby$c/customer_name return<customer>{$c/*}</customer> Useorderby$c/customer_nametosortindescendingorder Cansortatmultiplelevelsofnesting(sortbycustomer_name,andbyaccount_number withineachcustomer)

s s

<bank1>{ for$cin/bank/customer orderby$c/customer_name return <customer> {$c/*}

{for$din/bank/depositor[customer_name=$c/customer_name], $ain/bank/account[account_number=$d/account_number] orderby$a/account_number return<account>$a/*</account>}

DatabaseSystemConcepts5thEdition,Aug22,2005.

</customer> }</bank1>

10.39

Silberschatz,KorthandSudarshan

FunctionsandOtherXQueryFeatures
s UserdefinedfunctionswiththetypesystemofXMLSchema

functionbalances(xs:string$c)returnslist(xs:decimal*){ for$din/bank/depositor[customer_name=$c], $ain/bank/account[account_number=$d/account_number] return$a/balance }


s Typesareoptionalforfunctionparametersandreturnvalues s The*(asindecimal*)indicatesasequenceofvaluesofthattype s Universalandexistentialquantificationinwhereclausepredicates
q q

some$einpathsatisfiesP every$einpathsatisfiesP

s XQueryalsosupportsIfthenelseclauses

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.40

Silberschatz,KorthandSudarshan

XSLT
s Astylesheetstoresformattingoptionsforadocument,usually

separatelyfromdocument
q

E.g.anHTMLstylesheetmayspecifyfontcolorsandsizesfor headings,etc.

s TheXMLStylesheetLanguage(XSL)wasoriginallydesignedfor

generatingHTMLfromXML
q

s XSLTisageneralpurposetransformationlanguage

CantranslateXMLtoXML,andXMLtoHTML TemplatescombineselectionusingXPathwithconstructionof results

s XSLTtransformationsareexpressedusingrulescalledtemplates
q

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.41

Silberschatz,KorthandSudarshan

XSLTTemplates
s ExampleofXSLTtemplatewithmatchandselectpart

<xsl:templatematch=/bank2/customer>

</xsl:template> <xsl:templatematch=*/> s Thematchattributeofxsl:templatespecifiesapatterninXPath


s ElementsintheXMLdocumentmatchingthepatternareprocessedbythe

<xsl:valueofselect=customer_name/>

actionswithinthexsl:templateelement q xsl:valueofselects(outputs)specifiedvalues(here,customer_name) s Forelementsthatdonotmatchanytemplate q Attributesandtextcontentsareoutputasis q Templatesarerecursivelyappliedonsubelements


s The<xsl:templatematch=*/>templatematchesall

elementsthatdonotmatchanyothertemplate
q

Usedtoensurethattheircontentsdonotgetoutput.

s Ifanelementmatchesseveraltemplates,onlyoneisusedbasedona

complexpriorityscheme/userdefinedpriorities

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.42

Silberschatz,KorthandSudarshan

CreatingXMLOutput
s AnytextortagintheXSLstylesheetthatisnotinthexslnamespace

isoutputasis s E.g.towrapresultsinnewXMLelements. <xsl:templatematch=/bank2/customer> <customer>

</xsl;template> <xsl:templatematch=*/>
q

<xsl:valueofselect=customer_name/> </customer> Exampleoutput: <customer>Joe</customer> <customer>Mary</customer>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.43

Silberschatz,KorthandSudarshan

CreatingXMLOutput(Cont.)
s Note:Cannotdirectlyinsertaxsl:valueoftaginsideanothertag
q

E.g.cannotcreateanattributefor<customer>inthepreviousexample bydirectlyusingxsl:valueof q XSLTprovidesaconstructxsl:attributetohandlethissituation xsl:attributeaddsattributetotheprecedingelement E.g.<customer> <xsl:attributename=customer_id> <xsl:valueofselect=customer_id/> </xsl:attribute> </customer> resultsinoutputoftheform <customercustomer_id=.>. s xsl:elementisusedtocreateoutputelementswithcomputednames

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.44

Silberschatz,KorthandSudarshan

StructuralRecursion
s Templateactioncanapplytemplatesrecursivelytothecontentsofa

matchedelement

<xsl:templatematch=/bank> <customers> <xsl:templateapplytemplates/> </customers> </xsl:template> <xsl:templatematch=/customer> <customer> <xsl:valueofselect=customer_name/> </customer> </xsl:template> <xsl:templatematch=*/>
s Exampleoutput:

<customers> <customer>John</customer> <customer>Mary</customer> </customers>


10.45 Silberschatz,KorthandSudarshan

DatabaseSystemConcepts5thEdition,Aug22,2005.

JoinsinXSLT
s XSLTkeysallowelementstobelookedup(indexed)byvaluesof

subelementsorattributes q Keysmustbedeclared(withaname)and,thekey()functioncanthen beusedforlookup.E.g. <xsl:keyname=acctnomatch=account use=account_number/> <xsl:valueofselect=key(acctno,A101) s Keyspermit(some)joinstobeexpressedinXSLT <xsl:keyname=acctnomatch=accountuse=account_number/> <xsl:keyname=custnomatch=customeruse=customer_name/> <xsl:templatematch=depositor> <cust_acct> <xsl:valueofselect=key(custno,customer_name)/> <xsl:valueofselect=key(acctno,account_number)/> </cust_acct> </xsl:template> <xsl:templatematch=*/>
DatabaseSystemConcepts5thEdition,Aug22,2005. 10.46 Silberschatz,KorthandSudarshan

SortinginXSLT
s Usinganxsl:sortdirectiveinsideatemplatecausesallelements

matchingthetemplatetobesorted
q

Sortingisdonebeforeapplyingothertemplates

<xsl:templatematch=/bank> <xsl:applytemplatesselect=customer> <xsl:sortselect=customer_name/> </xsl:applytemplates> </xsl:template> <xsl:templatematch=customer> <customer> <xsl:valueofselect=customer_name/> <xsl:valueofselect=customer_street/> <xsl:valueofselect=customer_city/> </customer> <xsl:template> <xsl:templatematch=*/>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.47

Silberschatz,KorthandSudarshan

ApplicationProgramInterface
s TherearetwostandardapplicationprograminterfacestoXMLdata:
q

SAX(SimpleAPIforXML)

Basedonparsermodel,userprovideseventhandlersforparsing events E.g.startofelement,endofelement Notsuitablefordatabaseapplications

DOM(DocumentObjectModel)

XMLdataisparsedintoatreerepresentation VarietyoffunctionsprovidedfortraversingtheDOMtree E.g.:JavaDOMAPIprovidesNodeclasswithmethods getParentNode(),getFirstChild(),getNextSibling() getAttribute(),getData()(fortextnode) getElementsByTagName(), AlsoprovidesfunctionsforupdatingDOMtree

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.48

Silberschatz,KorthandSudarshan

StorageofXMLData
s XMLdatacanbestoredin
q

Nonrelationaldatastores

Flatfiles NaturalforstoringXML ButhasallproblemsdiscussedinChapter1(noconcurrency, norecovery,)

XMLdatabase DatabasebuiltspecificallyforstoringXMLdata,supporting DOMmodelanddeclarativequerying Currentlynocommercialgradesystems

Relationaldatabases

Datamustbetranslatedintorelationalform Advantage:maturedatabasesystems Disadvantages:overheadoftranslatingdataandqueries


10.49 Silberschatz,KorthandSudarshan

DatabaseSystemConcepts5thEdition,Aug22,2005.

StorageofXMLinRelationalDatabases
s Alternatives:
q q q

StringRepresentation TreeRepresentation Maptorelations

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.50

Silberschatz,KorthandSudarshan

StringRepresentation
s Storeeachchildoftoplevelelementasastringfieldofatupleina

relationaldatabase
q q

Useasinglerelationtostoreallelements,or Useaseparaterelationforeachtoplevelelementtype

E.g.account,customer,depositorrelations Eachwithastringvaluedattributetostoretheelement

s Indexing:
q

Storevaluesofsubelements/attributestobeindexedasextrafields oftherelation,andbuildindicesonthesefields

E.g.customer_nameoraccount_number

Somedatabasesystemssupportfunctionindices,whichusethe resultofafunctionasthekeyvalue.

Thefunctionshouldreturnthevalueoftherequired subelement/attribute

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.51

Silberschatz,KorthandSudarshan

StringRepresentation(Cont.)
s Benefits:
q q

CanstoreanyXMLdataevenwithoutDTD Aslongasthetoplevelelementinadocumenthasalarge numberofchildren,stringsaresmallcomparedtofulldocument

Allowsfastaccesstoindividualelements.

s Drawback:Needtoparsestringstoaccessvaluesinsidetheelements
q

Parsingisslow.

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.52

Silberschatz,KorthandSudarshan

TreeRepresentation
s Treerepresentation:modelXMLdataastreeandstoreusingrelations

nodes(id,type,label,value) child(child_id,parent_id)

bank(id:1) account(id:5) account_number (id:7)

customer(id:2) customer_name (id:3)

s Eachelement/attributeisgivenauniqueidentifier s Typeindicateselement/attribute s Labelspecifiesthetagnameoftheelement/nameofattribute s Valueisthetextvalueoftheelement/attribute s Therelationchildnotestheparentchildrelationshipsinthetree


q

Canaddanextraattributetochildtorecordorderingofchildren

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.53

Silberschatz,KorthandSudarshan

TreeRepresentation(Cont.)
s Benefit:CanstoreanyXMLdata,evenwithoutDTD s Drawbacks:
q

Dataisbrokenupintotoomanypieces,increasingspace overheads Evensimplequeriesrequirealargenumberofjoins,whichcanbe slow

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.54

Silberschatz,KorthandSudarshan

MappingXMLDatatoRelations
s Relationcreatedforeachelementtypewhoseschemaisknown:
q q q

Anidattributetostoreauniqueidforeachelement Arelationattributecorrespondingtoeachelementattribute Aparent_idattributetokeeptrackofparentelement


Asinthetreerepresentation Positioninformation(ithchild)canbestoredtoo

s Allsubelementsthatoccuronlyoncecanbecomerelationattributes
q q

Fortextvaluedsubelements,storethetextasattributevalue Forcomplexsubelements,canstoretheidofthesubelement SimilartohandlingofmultivaluedattributeswhenconvertingER diagramstotables

s Subelementsthatcanoccurmultipletimesrepresentedinaseparatetable
q

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.55

Silberschatz,KorthandSudarshan

StoringXMLDatainRelationalSystems
s Publishing:processofconvertingrelationaldatatoanXMLformat s Shredding:processofconvertinganXMLdocumentintoasetof

tuplestobeinsertedintooneormorerelations shredding

s XMLenableddatabasesystemssupportautomatedpublishingand s SomesystemsoffernativestorageofXMLdatausingthexmldata

type.Specialinternaldatastructuresandindicesareusedfor efficiency

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.56

Silberschatz,KorthandSudarshan

SQL/XML
s NewstandardSQLextensionthatallowscreationofnestedXML

output
q

EachoutputtupleismappedtoanXMLelementrow <account> <row> <account_number>A101</account_number> <branch_name>Downtown</branch_name> <balance>500</balance> </row> .morerowsiftherearemoreoutputtuples </account>

<bank>

</bank>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.57

Silberschatz,KorthandSudarshan

SQLExtensions
s xmlelementcreatesXMLelements s xmlattributescreatesattributes

selectxmlelement(nameaccount, xmlattributes(account_numberasaccount_number), xmlelement(namebranch_name,branch_name), xmlelement(namebalance,balance)) fromaccount

s xmlforest(attr1,attr2,..)createsasequence(forest)ofoneormore

elements,withtagnamessameastheSQLattributename

s xmlagg:aggregatefunctioncreatesaforestfromelementsingroup

selectxmlelement(namebranch,branch_name, xmlagg(xmlforest(account_number) orderbyaccount_number) fromaccount groupbybranch_name


DatabaseSystemConcepts5thEdition,Aug22,2005. 10.58 Silberschatz,KorthandSudarshan

XMLApplication:WebServices
s TheSimpleObjectAccessProtocol(SOAP)standard:
q

Invocationofproceduresacrossapplicationswithdistinct databases XMLusedtorepresentprocedureinputandoutput DescribedusingtheWebServicesDescriptionLanguage(WSDL) DirectoriesofWebservicesaredescribedusingtheUniversal Description,Discovery,andIntegration(UDDI)standard

s AWebserviceisasiteprovidingacollectionofSOAPprocedures
q q

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.59

Silberschatz,KorthandSudarshan

You might also like