You are on page 1of 59

Chapter10:XML

DatabaseSystemConcepts
Silberschatz,KorthandSudarshan
Seewww.dbbook.comforconditionsonreuse

XML
StructureofXMLData
XMLDocumentSchema
QueryingandTransformation
ApplicationProgramInterfacestoXML
StorageofXMLData
XMLApplications

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.2

Silberschatz,KorthandSudarshan

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

simplertousethanSGML

Documentshavetagsgivingextrainformationaboutsectionsofthe

document

E.g.<title>XML</title><slide>Introduction</slide>

Extensible,unlikeHTML

Userscanaddnewtags,andseparatelyspecifyhowthetagshouldbe
handledfordisplay

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.3

Silberschatz,KorthandSudarshan

XMLIntroduction(Cont.)
Theabilitytospecifynewtags,andtocreatenestedtagstructuresmake

XMLagreatwaytoexchangedata,notjustdocuments.

MuchoftheuseofXMLhasbeenindataexchangeapplications,notasa
replacementforHTML

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>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.4

Silberschatz,KorthandSudarshan

XML:Motivation
Datainterchangeiscriticalintodaysnetworkedworld

Examples:

Banking:fundstransfer

Orderprocessing(especiallyintercompanyorders)

Scientificdata
Chemistry:ChemML,
Genetics:BSML(BioSequenceMarkupLanguage),

Paperflowofinformationbetweenorganizationsisbeingreplacedby
electronicflowofinformation

Eachapplicationareahasitsownsetofstandardsforrepresenting

information

XMLhasbecomethebasisforallnewgenerationdatainterchange

formats

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.5

Silberschatz,KorthandSudarshan

XMLMotivation(Cont.)
Earliergenerationformatswerebasedonplaintextwithlineheaders

indicatingthemeaningoffields

Similarinconcepttoemailheaders

Doesnotallowfornestedstructures,nostandardtypelanguage

Tiedtoocloselytolowleveldocumentstructure(lines,spaces,etc)

EachXMLbasedstandarddefineswhatarevalidelements,using

XMLtypespecificationlanguagestospecifythesyntax

DTD(DocumentTypeDescriptors)

XMLSchema

Plustextualdescriptionsofthesemantics

XMLallowsnewtagstobedefinedasrequired

However,thismaybeconstrainedbyDTDs

Awidevarietyoftoolsisavailableforparsing,browsingandqueryingXML

documents/data

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.6

Silberschatz,KorthandSudarshan

ComparisonwithRelationalData
Inefficient:tags,whichineffectrepresentschemainformation,are

repeated

Betterthanrelationaltuplesasadataexchangeformat

Unlikerelationaltuples,XMLdataisselfdocumentingdueto
presenceoftags

Nonrigidformat:tagscanbeadded

Allowsnestedstructures

Wideacceptance,notonlyindatabasesystems,butalsoin
browsers,tools,andapplications

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.7

Silberschatz,KorthandSudarshan

StructureofXMLData
Tag:labelforasectionofdata
Element:sectionofdatabeginningwith<tagname>andendingwith

matching</tagname>

Elementsmustbeproperlynested

Propernesting

Impropernesting

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

Formally:everystarttagmusthaveauniquematchingendtag,
thatisinthecontextofthesameparentelement.

Everydocumentmusthaveasingletoplevelelement

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.8

Silberschatz,KorthandSudarshan

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

<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>

</customer>
.
.
</bank1>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.9

Silberschatz,KorthandSudarshan

MotivationforNesting
Nestingofdataisusefulindatatransfer

Example:elementsrepresentingcustomer_id,customer_name,and
addressnestedwithinanorderelement

Nestingisnotsupported,ordiscouraged,inrelationaldatabases

Withmultipleorders,customernameandaddressarestored
redundantly
normalizationreplacesnestedstructuresineachorderbyforeignkey
intotablestoringcustomernameandaddressinformation
Nestingissupportedinobjectrelationaldatabases

Butnestingisappropriatewhentransferringdata

Externalapplicationdoesnothavedirectaccesstodatareferenced
byaforeignkey

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.10

Silberschatz,KorthandSudarshan

StructureofXMLData(Cont.)
MixtureoftextwithsubelementsislegalinXML.

Example:

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

Usefulfordocumentmarkup,butdiscouragedfordata
representation

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.11

Silberschatz,KorthandSudarshan

Attributes
Elementscanhaveattributes

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

element

Anelementmayhaveseveralattributes,buteachattributenamecan

onlyoccuronce

<accountaccttype=checkingmonthlyfee=5>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.12

Silberschatz,KorthandSudarshan

Attributesvs.Subelements
Distinctionbetweensubelementandattribute

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
XMLdatahastobeexchangedbetweenorganizations
Sametagnamemayhavedifferentmeaningindifferentorganizations,

causingconfusiononexchangeddocuments

Specifyingauniquestringasanelementnameavoidsconfusion
Bettersolution:useuniquename:elementname
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
Elementswithoutsubelementsortextcontentcanbeabbreviatedby

endingthestarttagwitha/>anddeletingtheendtag

<accountnumber=A101branch=Perryridgebalance=200/>

Tostorestringdatathatmaycontaintags,withoutthetagsbeing

interpretedassubelements,useCDATAasbelow

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

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

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.15

Silberschatz,KorthandSudarshan

XMLDocumentSchema
Databaseschemasconstrainwhatinformationcanbestored,andthe

datatypesofstoredvalues

XMLdocumentsarenotrequiredtohaveanassociatedschema
However,schemasareveryimportantforXMLdataexchange

Otherwise,asitecannotautomaticallyinterpretdatareceivedfrom
anothersite

TwomechanismsforspecifyingXMLschema

DocumentTypeDefinition(DTD)

Widelyused

XMLSchema

Newer,increasinguse

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.16

Silberschatz,KorthandSudarshan

DocumentTypeDefinition(DTD)
ThetypeofanXMLdocumentcanbespecifiedusingaDTD
DTDconstraintsstructureofXMLdata

Whatelementscanoccur

Whatattributescan/mustanelementhave

Whatsubelementscan/mustoccurinsideeachelement,andhow
manytimes.

DTDdoesnotconstraindatatypes

AllvaluesrepresentedasstringsinXML

DTDsyntax

<!ELEMENTelement(subelementsspecification)>

<!ATTLISTelement(attributes)>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.17

Silberschatz,KorthandSudarshan

ElementSpecificationinDTD

Subelementscanbespecifiedas

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
Attributespecification:foreachattribute

Name

Typeofattribute

CDATA

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

Whether

mandatory(#REQUIRED)

hasadefaultvalue(value),

orneither(#IMPLIED)

Examples

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

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.20

Silberschatz,KorthandSudarshan

IDsandIDREFs
AnelementcanhaveatmostoneattributeoftypeID
TheIDattributevalueofeachelementinanXMLdocumentmustbe

distinct

ThustheIDattributevalueisanobjectidentifier

AnattributeoftypeIDREFmustcontaintheIDvalueofanelementin

thesamedocument

AnattributeoftypeIDREFScontainsasetof(0ormore)IDvalues.

EachIDvaluemustcontaintheIDvalueofanelementinthesame
document

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.21

Silberschatz,KorthandSudarshan

BankDTDwithAttributes
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_street>Monroe</customer_street>
<customer_city>Madison</customer_city>
</customer>
<customercustomer_id=C102accounts=A401A402>
<customer_name>Mary</customer_name>

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

</customer>
</bank2>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.23

Silberschatz,KorthandSudarshan

LimitationsofDTDs
Notypingoftextelementsandattributes

Allvaluesarestrings,nointegers,reals,etc.

Difficulttospecifyunorderedsetsofsubelements

Orderisusuallyirrelevantindatabases(unlikeinthedocument
layoutenvironmentfromwhichXMLevolved)
(A|B)*allowsspecificationofanunorderedset,but

CannotensurethateachofAandBoccursonlyonce

IDsandIDREFsareuntyped

Theownersattributeofanaccountmaycontainareferenceto
anotheraccount,whichismeaningless

ownersattributeshouldideallybeconstrainedtoreferto
customerelements

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.24

Silberschatz,KorthandSudarshan

XMLSchema
XMLSchemaisamoresophisticatedschemalanguagewhich

addressesthedrawbacksofDTDs.Supports

Typingofvalues

E.g.integer,string,etc

Also,constraintsonmin/maxvalues

Userdefined,comlextypes

Manymorefeatures,including

uniquenessandforeignkeyconstraints,inheritance

XMLSchemaisitselfspecifiedinXMLsyntax,unlikeDTDs

Morestandardrepresentation,butverbose

XMLSchemeisintegratedwithnamespaces
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:elementref=accountminOccurs=0maxOccurs=unbounded/>
<xs:elementref=customerminOccurs=0maxOccurs=unbounded/>
<xs:elementref=depositorminOccurs=0maxOccurs=unbounded/>
</xs:sequence>

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

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.26

Silberschatz,KorthandSudarshan

XMLSchemaVersionofBankDTD
Choiceofxs:wasoursanyothernamespaceprefixcouldbe

chosen

ElementbankhastypeBankType,whichisdefinedseparately

xs:complexTypeisusedlatertocreatethenamedcomplextype
BankType

Elementaccounthasitstypedefinedinline

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.27

Silberschatz,KorthandSudarshan

MorefeaturesofXMLSchema
Attributesspecifiedbyxs:attributetag:

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

Keyconstraint:accountnumbersformakeyforaccountelements

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

Foreignkeyconstraintfromdepositortoaccount:

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

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.28

Silberschatz,KorthandSudarshan

QueryingandTransformingXMLData
TranslationofinformationfromoneXMLschematoanother
QueryingonXMLdata
Abovetwoarecloselyrelated,andhandledbythesametools
StandardXMLquerying/translationlanguages

XPath

XQuery

Simplelanguageconsistingofpathexpressions
AnXMLquerylanguagewitharichsetoffeatures

XSLT

SimplelanguagedesignedfortranslationfromXMLtoXML
andXMLtoHTML

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.29

Silberschatz,KorthandSudarshan

TreeModelofXMLData
QueryandtransformationlanguagesarebasedonatreemodelofXML

data

AnXMLdocumentismodeledasatree,withnodescorrespondingto

elementsandattributes

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

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.30

Silberschatz,KorthandSudarshan

XPath
XPathisusedtoaddress(select)partsofdocumentsusing

pathexpressions

Apathexpressionisasequenceofstepsseparatedby/

Thinkoffilenamesinadirectoryhierarchy

Resultofpathexpression:setofvaluesthatalongwiththeir

containingelements/attributesmatchthespecifiedpath

E.g./bank2/customer/customer_nameevaluatedonthe

bank2datawesawearlierreturns

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

returnsthesamenames,butwithouttheenclosingtags

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.31

Silberschatz,KorthandSudarshan

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

Eachstepoperatesonthesetofinstancesproducedbytheprevious
step

Selectionpredicatesmayfollowanystepinapath,in[]

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

returnsaccountelementswithabalancevaluegreaterthan400
/bank2/account[balance]returnsaccountelementscontaininga
balancesubelement

Attributesareaccessedusing@

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

returnstheaccountnumbersofaccountswithbalance>400

IDREFattributesarenotdereferencedautomatically(moreonthis
later)

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.32

Silberschatz,KorthandSudarshan

FunctionsinXPath
XPathprovidesseveralfunctions

Thefunctioncount()attheendofapathcountsthenumberof
elementsinthesetgeneratedbythepath

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

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

predicates

IDREFscanbereferencedusingfunctionid()

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

returnsallcustomersreferredtofromtheownersattributeof
accountelements.

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.33

Silberschatz,KorthandSudarshan

MoreXPathFeatures
Operator|usedtoimplementunion

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

Givescustomerswitheitheraccountsorloans

However,|cannotbenestedinsideotheroperators.

//canbeusedtoskipmultiplelevelsofnodes

E.g./bank2//customer_name

findsanycustomer_nameelementanywhereunderthe
/bank2element,regardlessoftheelementinwhichitis
contained.

Astepinthepathcangotoparents,siblings,ancestorsand

descendantsofthenodesgeneratedbythepreviousstep,notjust
tothechildren

//,describedabove,isashortfromforspecifyingall
descendants
..specifiestheparent.

doc(name)returnstherootofanameddocument
DatabaseSystemConcepts5thEdition,Aug22,2005.

10.34

Silberschatz,KorthandSudarshan

XQuery
XQueryisageneralpurposequerylanguageforXMLdata
CurrentlybeingstandardizedbytheWorldWideWebConsortium

(W3C)

ThetextbookdescriptionisbasedonaJanuary2005draftofthe
standard.Thefinalversionmaydiffer,butmajorfeatureslikelyto
stayunchanged.

XQueryisderivedfromtheQuiltquerylanguage,whichitselfborrows

fromSQL,XQLandXMLQL

XQueryusesa

forletwhereorderbyresult
syntax
forSQLfrom
whereSQLwhere
orderbySQLorderby
resultSQLselect
letallowstemporaryvariables,andhasnoequivalentinSQL

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.35

Silberschatz,KorthandSudarshan

FLWORSyntaxinXQuery
ForclauseusesXPathexpressions,andvariableinforclauserangesover

valuesinthesetreturnedbyXPath

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

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
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>
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
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>
$c/*denotesallthechildrenofthenodetowhich$cisbound,withoutthe

enclosingtopleveltag

$c/text()givestextcontentofanelementwithoutanysubelements/tags

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.38

Silberschatz,KorthandSudarshan

SortinginXQuery

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)

<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>}

</customer>
}</bank1>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.39

Silberschatz,KorthandSudarshan

FunctionsandOtherXQueryFeatures
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
}
Typesareoptionalforfunctionparametersandreturnvalues
The*(asindecimal*)indicatesasequenceofvaluesofthattype
Universalandexistentialquantificationinwhereclausepredicates

some$einpathsatisfiesP

every$einpathsatisfiesP

XQueryalsosupportsIfthenelseclauses

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.40

Silberschatz,KorthandSudarshan

XSLT
Astylesheetstoresformattingoptionsforadocument,usually

separatelyfromdocument

E.g.anHTMLstylesheetmayspecifyfontcolorsandsizesfor
headings,etc.

TheXMLStylesheetLanguage(XSL)wasoriginallydesignedfor

generatingHTMLfromXML

XSLTisageneralpurposetransformationlanguage

CantranslateXMLtoXML,andXMLtoHTML

XSLTtransformationsareexpressedusingrulescalledtemplates

TemplatescombineselectionusingXPathwithconstructionof
results

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.41

Silberschatz,KorthandSudarshan

XSLTTemplates
ExampleofXSLTtemplatewithmatchandselectpart

<xsl:templatematch=/bank2/customer>

<xsl:valueofselect=customer_name/>

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

actionswithinthexsl:templateelement
xsl:valueofselects(outputs)specifiedvalues(here,customer_name)
Forelementsthatdonotmatchanytemplate
Attributesandtextcontentsareoutputasis
Templatesarerecursivelyappliedonsubelements
The<xsl:templatematch=*/>templatematchesall

elementsthatdonotmatchanyothertemplate

Usedtoensurethattheircontentsdonotgetoutput.

Ifanelementmatchesseveraltemplates,onlyoneisusedbasedona

complexpriorityscheme/userdefinedpriorities

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.42

Silberschatz,KorthandSudarshan

CreatingXMLOutput
AnytextortagintheXSLstylesheetthatisnotinthexslnamespace

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

<xsl:valueofselect=customer_name/>
</customer>

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

Exampleoutput:
<customer>Joe</customer>
<customer>Mary</customer>

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.43

Silberschatz,KorthandSudarshan

CreatingXMLOutput(Cont.)
Note:Cannotdirectlyinsertaxsl:valueoftaginsideanothertag

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

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.44

Silberschatz,KorthandSudarshan

StructuralRecursion
Templateactioncanapplytemplatesrecursivelytothecontentsofa

matchedelement

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

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

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.45

Silberschatz,KorthandSudarshan

JoinsinXSLT
XSLTkeysallowelementstobelookedup(indexed)byvaluesof

subelementsorattributes
Keysmustbedeclared(withaname)and,thekey()functioncanthen
beusedforlookup.E.g.
<xsl:keyname=acctnomatch=account
use=account_number/>
<xsl:valueofselect=key(acctno,A101)
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
Usinganxsl:sortdirectiveinsideatemplatecausesallelements

matchingthetemplatetobesorted

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
TherearetwostandardapplicationprograminterfacestoXMLdata:

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
XMLdatacanbestoredin

Nonrelationaldatastores

Flatfiles
NaturalforstoringXML
ButhasallproblemsdiscussedinChapter1(noconcurrency,
norecovery,)

XMLdatabase
DatabasebuiltspecificallyforstoringXMLdata,supporting
DOMmodelanddeclarativequerying
Currentlynocommercialgradesystems

Relationaldatabases

Datamustbetranslatedintorelationalform

Advantage:maturedatabasesystems

Disadvantages:overheadoftranslatingdataandqueries

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.49

Silberschatz,KorthandSudarshan

StorageofXMLinRelationalDatabases
Alternatives:

StringRepresentation

TreeRepresentation

Maptorelations

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.50

Silberschatz,KorthandSudarshan

StringRepresentation
Storeeachchildoftoplevelelementasastringfieldofatupleina

relationaldatabase

Useasinglerelationtostoreallelements,or

Useaseparaterelationforeachtoplevelelementtype

E.g.account,customer,depositorrelations
Eachwithastringvaluedattributetostoretheelement

Indexing:

Storevaluesofsubelements/attributestobeindexedasextrafields
oftherelation,andbuildindicesonthesefields

E.g.customer_nameoraccount_number

Somedatabasesystemssupportfunctionindices,whichusethe
resultofafunctionasthekeyvalue.

Thefunctionshouldreturnthevalueoftherequired
subelement/attribute

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.51

Silberschatz,KorthandSudarshan

StringRepresentation(Cont.)
Benefits:

CanstoreanyXMLdataevenwithoutDTD
Aslongasthetoplevelelementinadocumenthasalarge
numberofchildren,stringsaresmallcomparedtofulldocument

Allowsfastaccesstoindividualelements.

Drawback:Needtoparsestringstoaccessvaluesinsidetheelements

Parsingisslow.

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.52

Silberschatz,KorthandSudarshan

TreeRepresentation
Treerepresentation:modelXMLdataastreeandstoreusingrelations

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

bank(id:1)

customer(id:2)

account(id:5)

customer_name
(id:3)

account_number
(id:7)

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

Canaddanextraattributetochildtorecordorderingofchildren

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.53

Silberschatz,KorthandSudarshan

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

Dataisbrokenupintotoomanypieces,increasingspace
overheads
Evensimplequeriesrequirealargenumberofjoins,whichcanbe
slow

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.54

Silberschatz,KorthandSudarshan

MappingXMLDatatoRelations
Relationcreatedforeachelementtypewhoseschemaisknown:

Anidattributetostoreauniqueidforeachelement

Arelationattributecorrespondingtoeachelementattribute

Aparent_idattributetokeeptrackofparentelement

Asinthetreerepresentation

Positioninformation(ithchild)canbestoredtoo

Allsubelementsthatoccuronlyoncecanbecomerelationattributes

Fortextvaluedsubelements,storethetextasattributevalue

Forcomplexsubelements,canstoretheidofthesubelement

Subelementsthatcanoccurmultipletimesrepresentedinaseparatetable

SimilartohandlingofmultivaluedattributeswhenconvertingER
diagramstotables

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.55

Silberschatz,KorthandSudarshan

StoringXMLDatainRelationalSystems
Publishing:processofconvertingrelationaldatatoanXMLformat
Shredding:processofconvertinganXMLdocumentintoasetof

tuplestobeinsertedintooneormorerelations

XMLenableddatabasesystemssupportautomatedpublishingand

shredding

SomesystemsoffernativestorageofXMLdatausingthexmldata

type.Specialinternaldatastructuresandindicesareusedfor
efficiency

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.56

Silberschatz,KorthandSudarshan

SQL/XML
NewstandardSQLextensionthatallowscreationofnestedXML

output

EachoutputtupleismappedtoanXMLelementrow

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

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.57

Silberschatz,KorthandSudarshan

SQLExtensions
xmlelementcreatesXMLelements
xmlattributescreatesattributes

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

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

elements,withtagnamessameastheSQLattributename

xmlagg:aggregatefunctioncreatesaforestfromelementsingroup

selectxmlelement(namebranch,branch_name,
xmlagg(xmlforest(account_number)
orderbyaccount_number)
fromaccount
groupbybranch_name
DatabaseSystemConcepts5thEdition,Aug22,2005.

10.58

Silberschatz,KorthandSudarshan

XMLApplication:WebServices
TheSimpleObjectAccessProtocol(SOAP)standard:

Invocationofproceduresacrossapplicationswithdistinct
databases
XMLusedtorepresentprocedureinputandoutput

AWebserviceisasiteprovidingacollectionofSOAPprocedures

DescribedusingtheWebServicesDescriptionLanguage(WSDL)
DirectoriesofWebservicesaredescribedusingtheUniversal
Description,Discovery,andIntegration(UDDI)standard

DatabaseSystemConcepts5thEdition,Aug22,2005.

10.59

Silberschatz,KorthandSudarshan

You might also like