浙江大学数据库系统概念第十章对应原版教材第五版课件

上传人:无*** 文档编号:241558423 上传时间:2024-07-04 格式:PPT 页数:61 大小:746.50KB
返回 下载 相关 举报
浙江大学数据库系统概念第十章对应原版教材第五版课件_第1页
第1页 / 共61页
浙江大学数据库系统概念第十章对应原版教材第五版课件_第2页
第2页 / 共61页
浙江大学数据库系统概念第十章对应原版教材第五版课件_第3页
第3页 / 共61页
点击查看更多>>
资源描述
Silberschatz,Korth and Sudarshan,Bo Zhou10.1Database System ConceptsXMLnStructure of XML DatanXML Document SchemanQuerying and TransformationnApplication Program Interfaces to XMLnStorage of XML DatanXML ApplicationsSilberschatz,Korth and Sudarshan,Bo Zhou10.2Database System ConceptsIntroductionnXML:Extensible Markup LanguageHMarkup:anything in a document that is not intended to be part of the printed output;HThe most famous markup language:HTML(Hyper-Text Markup Language);HXML Derived from SGML(Standard Generalized Markup Language),but simpler to use than SGML HWas not originally conceived as a database technology.nExtensible,unlike HTMLHXML does not prescribe the set of tags allowed.HUsers can add new tags,and separately specify how the tag should be handled for displayHDocuments have tags giving extra information about sections of the document4E.g.XML Introduction HAbility of adding user define tags makes XML as a attractive way of exchanging data among different applications.nDefined by the (W3C)Silberschatz,Korth and Sudarshan,Bo Zhou10.3Database System ConceptsA XML SamplenTags make data(relatively)self-documenting HE.g.A-101 Downtown 500 A-101 Johnson From Textbook Page 396Silberschatz,Korth and Sudarshan,Bo Zhou10.4Database System ConceptsXML:MotivationnData interchange is critical in todays networked worldHExamples:4Banking:funds transfer4Order processing(especially inter-company orders)4Scientific dataChemistry:ChemML,Genetics:BSML(Bio-Sequence Markup Language),HPaper flow of information between organizations is being replaced by electronic flow of informationnEach application area has its own set of standards for representing informationnXML has become the basis for all new generation data interchange formatsSilberschatz,Korth and Sudarshan,Bo Zhou10.5Database System ConceptsXML Motivation(Cont.)nEarlier generation formats were based on plain text with line headers indicating the meaning of fieldsHSimilar in concept to email headersHDoes not allow for nested structures,no standard“type”languageHTied too closely to low level document structure(lines,spaces,etc)nEach XML based standard defines what are valid elements,usingH XML type specification languages to specify the syntax4DTD(Document Type Descriptors)4XML SchemaHPlus textual descriptions of the semanticsnXML allows new tags to be defined as requiredHHowever,this may be constrained by DTDs/SchemasnA wide variety of tools is available for parsing,browsing and querying XML documents/dataSilberschatz,Korth and Sudarshan,Bo Zhou10.6Database System ConceptsComparison with Relational DatanInefficient:tags,which in effect represent schema information,are repeatednBetter than relational tuples as a data-exchange formatHUnlike relational tuples,XML data is self-documenting due to presence of tagsHNon-rigid format:tags can be addedHAllows nested structuresHWide acceptance,not only in database systems,but also in browsers,tools,and applicationsSilberschatz,Korth and Sudarshan,Bo Zhou10.7Database System ConceptsStructure of XML DatanTag:label for a section of datanElement:section of data beginning with and ending with matching nElements must be properly nestedHProper nesting4 .HImproper nesting 4 .HFormally:every start tag must have a unique matching end tag,that is in the context of the same parent element.nEvery document must have a single top-level(root)elementSilberschatz,Korth and Sudarshan,Bo Zhou10.8Database System ConceptsExample of Nested Elements Hayes Main Harrison A-102 Perryridge 400 .Silberschatz,Korth and Sudarshan,Bo Zhou10.9Database System ConceptsWhy NestingnNesting of data is useful in data transferHExample:elements representing customer_id,customer_name,and address nested within an order elementnNesting is not supported,or discouraged,in relational databasesHWith multiple orders,customer name and address are stored redundantlyHnormalization replaces nested structures in each order by foreign key into table storing customer name and address informationHNesting is supported in object-relational databasesnBut nesting is appropriate when transferring dataHExternal application does not have direct access to data referenced by a foreign keySilberschatz,Korth and Sudarshan,Bo Zhou10.10Database System ConceptsFreedom of adding other informationsnMixture of text with sub-elements is legal in XML.HExample:This account is seldom used any more.A-102 Perryridge 400 nUseful for document markup,addition information.Silberschatz,Korth and Sudarshan,Bo Zhou10.11Database System ConceptsAttributesnElements can have attributes A-102 Perryridge 400 nAttributes are specified by name=value pairs inside the starting tag of an elementnAn element may have several attributes,but each attribute name can only occur onceSilberschatz,Korth and Sudarshan,Bo Zhou10.12Database System ConceptsAttributes vs.SubelementsnDistinction between subelement and attributeHIn the context of documents,attributes are part of markup,while subelement contents are part of the basic document contentsHIn the context of data representation,the difference is unclear and may be confusing4Same information can be represented in two ways .A-101 HSuggestion:use attributes for identifiers of elements,and use subelements for contentsSilberschatz,Korth and Sudarshan,Bo Zhou10.13Database System ConceptsNamespacesnXML data has to be exchanged between organizationsnSame tag name may have different meaning in different organizations,causing confusion on exchanged documentsnSpecifying a unique string as an element name avoids confusionnBetter solution:use unique-name:element-nameHE.g :branchnAvoid using long unique names all over document by using XML Namespaces Downtown Brooklyn Silberschatz,Korth and Sudarshan,Bo Zhou10.14Database System ConceptsMore on XML SyntaxnElements without subelements or text content can be abbreviated by ending the start tag with a /and deleting the end tagHnTo store string data that may contain tags,without the tags being interpreted as subelements,use CDATA as belowH!CDATA Here,and are treated as just stringsCDATA stands for“character data”Silberschatz,Korth and Sudarshan,Bo Zhou10.15Database System ConceptsXML Document SchemanDatabase schemas constrain what information can be stored,and the data types of stored valuesnXML documents are not required to have an associated schemanHowever,schemas are very important for XML data exchangeHOtherwise,a site cannot automatically interpret data received from another sitenTwo mechanisms for specifying XML schemaHDocument Type Definition(DTD)4Widely usedHXML Schema 4Newer,increasing useSilberschatz,Korth and Sudarshan,Bo Zhou10.16Database System ConceptsDocument Type Definition(DTD)nThe type of an XML document can be specified using a DTDnDTD constraints structure of XML dataHWhat elements can occurHWhat attributes can/must an element haveHWhat subelements can/must occur inside each element,and how many times.nDTD does not constrain data typesHAll values represented as strings in XMLnDTD syntaxHHSilberschatz,Korth and Sudarshan,Bo Zhou10.17Database System ConceptsElement Specification in DTDnSubelements can be specified asHnames of elements,orH#PCDATA(parsed character data),i.e.,character stringsHEMPTY(no subelements)or ANY(anything can be a subelement)nExample nSubelement specification may have regular expressions 4Notation:“|”-alternatives“+”-1 or more occurrences“*”-0 or more occurrences“?”-0 or 1 occurrenceSilberschatz,Korth and Sudarshan,Bo Zhou10.18Database System ConceptsBank DTD!DOCTYPE bank Silberschatz,Korth and Sudarshan,Bo Zhou10.19Database System ConceptsAttribute Specification in DTDnAttribute specification:for each attribute HNameHType of attribute 4CDATA4ID(identifier)or IDREF(ID reference)or IDREFS(multiple IDREFs)HWhether 4mandatory(#REQUIRED)4has a default value(value),4or neither(#IMPLIED)nExamplesHHSilberschatz,Korth and Sudarshan,Bo Zhou10.20Database System ConceptsIDs and IDREFsnAn element can have at most one attribute of type IDnThe ID attribute value of each element in an XML document must be distinctHThus the ID attribute value is an object identifiernAn attribute of type IDREF must contain the ID value of an element in the same documentnAn attribute of type IDREFS contains a set of(0 or more)ID values.Each ID value must contain the ID value of an element in the same documentSilberschatz,Korth and Sudarshan,Bo Zhou10.21Database System ConceptsBank DTD with AttributesnBank DTD with ID and IDREF attribute types.!DOCTYPE bank-2 declarations for branch,balance,customer_name,customer_street and customer_citySilberschatz,Korth and Sudarshan,Bo Zhou10.22Database System ConceptsXML data with ID and IDREF attributesXML data with ID and IDREF attributes Downtown 500 Joe Monroe Madison Mary Erin Newark Silberschatz,Korth and Sudarshan,Bo Zhou10.23Database System ConceptsLimitations of DTDsnNo typing of text elements and attributesHAll values are strings,no integers,reals,etc.nDifficult to specify unordered sets of subelementsHOrder is usually irrelevant in databases(unlike in the document-layout environment from which XML evolved)H(A|B)*allows specification of an unordered set,but4Cannot ensure that each of A and B occurs only oncenIDs and IDREFs are untypedHThe owners attribute of an account may contain a reference to another account,which is meaningless4owners attribute should ideally be constrained to refer to customer elementsSilberschatz,Korth and Sudarshan,Bo Zhou10.24Database System ConceptsXML SchemanXML Schema is a more sophisticated schema language which addresses the drawbacks of DTDs.SupportsHTyping of values4E.g.integer,string,etc4Also,constraints on min/max valuesHUser-defined,complex typesHMany more features,including4uniqueness and foreign key constraints,inheritance nXML Schema is itself specified in XML syntax,unlike DTDsHMore-standard representation,but verbosenXML Scheme is integrated with namespaces nBUT:XML Schema is significantly more complicated than DTDs.Silberschatz,Korth and Sudarshan,Bo Zhou10.25Database System ConceptsXML Schema Version of Bank DTD .definitions of customer and depositor.Silberschatz,Korth and Sudarshan,Bo Zhou10.26Database System ConceptsXML Schema Version of Bank DTDnChoice of“xs:”was ours-any other namespace prefix could be chosennElement“bank”has type“BankType”,which is defined separatelyHxs:complexType is used later to create the named complex type“BankType”nElement“account”has its type defined in-lineSilberschatz,Korth and Sudarshan,Bo Zhou10.27Database System ConceptsMore features of XML SchemanAttributes specified by xs:attribute tag:HHadding the attribute use=“required”means value must be specifiednKey constraint:“account numbers form a key for account elements under the root bank element:nForeign key constraint from depositor to account:Silberschatz,Korth and Sudarshan,Bo Zhou10.28Database System ConceptsQuerying and Transforming XML DatanTranslation of information from one XML schema to anothernQuerying on XML data nAbove two are closely related,and handled by the same toolsnStandard XML querying/translation languagesHXPath4Simple language consisting of path expressionsHXSLT4Simple language designed for translation from XML to XML and XML to HTMLHXQuery4An XML query language with a rich set of featuresSilberschatz,Korth and Sudarshan,Bo Zhou10.29Database System ConceptsTree Model of XML DatanQuery and transformation languages are based on a tree model of XML datanAn XML document is modeled as a tree,with nodes corresponding to elements and attributesHElement nodes have child nodes,which can be attributes or subelementsHText in an element is modeled as a text node child of the elementHChildren of a node are ordered according to their order in the XML documentHElement and attribute nodes(except for the root node)have a single parent,which is an element nodeHThe root node has a single child,which is the root element of the documentSilberschatz,Korth and Sudarshan,Bo Zhou10.30Database System ConceptsXPathnXPath is used to address(select)parts of documents using path expressionsnA path expression is a sequence of steps separated by“/”HThink of in a directory hierarchynResult of path expression:set of values that along with their containing elements/attributes match the specified path nE.g./bank-2/customer/customer_name evaluated on the bank-2 data we saw earlier returns JoeMarynE.g./bank-2/customer/customer_name/text()returns the same names,but without the enclosing tagsSilberschatz,Korth and Sudarshan,Bo Zhou10.31Database System ConceptsXPath(Cont.)nThe initial“/”denotes root of the document(above the top-level tag)nPath expressions are evaluated left to rightHEach step operates on the set of instances produced by the previous stepnSelection predicates may follow any step in a path,in HE.g./bank-2/accountbalance 400 4returns account elements with a balance value greater than 4004/bank-2/accountbalance returns account elements containing a balance subelementnAttributes are accessed using“”HE.g./bank-2/accountbalance 400/account_number4returns the account numbers of accounts with balance 400Silberschatz,Korth and Sudarshan,Bo Zhou10.32Database System ConceptsFunctions in XPathnXPath provides several functionsHThe function count()at the end of a path counts the number of elements in the set generated by the path4E.g./bank-2/accountcount(./customer)2 Returns accounts with 2 customersHAlso function for testing position(1,2,.)of node w.r.t.siblingsnBoolean connectives and and or and function not()can be used in predicatesnIDREFs can be referenced using function id()Hid()can also be applied to sets of references such as IDREFS and even to strings containing multiple references separated by blanksHE.g./bank-2/account/id(owner)4returns all customers referred to from the owners attribute of account elements.Silberschatz,Korth and Sudarshan,Bo Zhou10.33Database System ConceptsMore XPath FeaturesnOperator“|”used to implement union HE.g./bank-2/account/id(owner)|/bank-2/loan/id(borrower)4Gives customers with either accounts or loans4However,“|”cannot be nested inside other operators.n“/”can be used to skip multiple levels of nodes HE.g./bank-2/customer_name 4finds any customer_name element anywhere under the/bank-2 element,regardless of the element in which it is contained.nA step in the path can go to parents,siblings,ancestors and descendants of the nodes generated by the previous step,not just to the childrenH“/”,described above,is a short from for specifying“all descendants”H“.”specifies the parent.ndoc(name)returns the root of a named document Silberschatz,Korth and Sudarshan,Bo Zhou10.34Database System ConceptsXQuerynXQuery is a general purpose query language for XML data nCurrently being standardized by the World Wide Web Consortium(W3C)HThe textbook description is based on a January 2005 draft of the standard.The final version may differ,but major features likely to stay unchanged.nXQuery is derived from the Quilt query language,which itself borrows from SQL,XQL and XML-QLnXQuery uses a for let where order by result syntax for SQL from where SQL where order by SQL order by result SQL select let allows temporary variables,and has no equivalent in SQLSilberschatz,Korth and Sudarshan,Bo Zhou10.35Database System ConceptsFLWOR Syntax in XQuery nFor clause uses XPath expressions,and variable in for clause ranges over values in the set returned by XPathnSimple FLWOR expression in XQuery Hfind all accounts with balance 400,with each result enclosed in an .tag for$x in/bank-2/account let$acctno:=$x/account_number where$x/balance 400 return$acctno HItems in the return clause are XML text unless enclosed in,in which case they are evaluatednLet clause not really needed in this query,and selection can be done In XPath.Query can be written as:for$x in/bank-2/accountbalance400return$x/account_number Silberschatz,Korth and Sudarshan,Bo Zhou10.36Database System ConceptsJoinsnJoins are specified in a manner very similar to SQLfor$a in /bank/account,$c in /bank/customer,$d in /bank/depositor where$a/account_number=$d/account_number and$c/customer_name=$d/customer_name return$c$a nThe same query can be expressed with the selections specified as XPath selections:for$a in/bank/account$c in/bank/customer$d in/bank/depositor account_number=$a/account_number and customer_name =$c/customer_name return$c$a Silberschatz,Korth and Sudarshan,Bo Zhou10.37Database System ConceptsNested QueriesnThe following query converts data from the flat structure for bank information into the nested structure used in bank-1 for$c in/bank/customer return$c/*for$d in/bank/depositorcustomer_name=$c/customer_name,$a in/bank/accountaccount_number=$d/account_number return$a n$c/*denotes all the children of the node to which$c is bound,without the enclosing top-level tagn$c/text()gives text content of an element without any subelements/tagsSilberschatz,Korth and Sudarshan,Bo Zhou10.38Database System ConceptsSorting in XQuery nThe order by clause can be used at the end of any expression.E.g.to return customers sorted by name for$c in/bank/customer order by$c/customer_name return$c/*nUse order by$c/customer_name to sort in descending ordernCan sort at multiple levels of nesting(sort by customer_name,and by account_number within each customer)for$c in/bank/customer order by$c/customer_namereturn$c/*for$d in /bank/depositorcustomer_name=$c/customer_name,$a in/bank/accountaccount_number=$d/account_number order by$a/account_number return$a/*Silberschatz,Korth and Sudarshan,Bo Zhou10.39Database System ConceptsFunctions and Other XQuery FeaturesnUser defined functions with the type system of XMLSchema function balances(xs:string$c)returns list(xs:decimal*)for$d in/bank/depositorcustomer_name=$c,$a in/bank/accountaccount_number=$d/account_number return$a/balance nTypes are optional for function parameters and return valuesnThe*(as in decimal*)indicates a sequence of values of that typenUniversal and existential quantification in where clause predicatesHsome$e in path satisfies P Hevery$e in path satisfies P nXQuery also supports If-then-else clausesSilberschatz,Korth and Sudarshan,Bo Zhou10.40Database System ConceptsXSLTnA style sheet stores formatting options for a document,usually separately from documentHE.g.an HTML style sheet may specify font colors and sizes for headings,etc.nThe XML Stylesheet Language(XSL)was originally designed for generating HTML from XMLnXSLT is a general-purpose transformation language HCan translate XML to XML,and XML to HTMLnXSLT transformations are expressed using rules called templatesHTemplates combine selection using XPath with construction of resultsSilberschatz,Korth and Sudarshan,Bo Zhou10.41Database System ConceptsXSLT TemplatesnExample of XSLT template with match and select part nThe match attribute of xsl:template specifies a pattern in XPathnElements in the XML document matching the pattern are processed by the actions within the xsl:template elementHxsl:value-of selects(outputs)specified values(here,customer_name)nFor elements that do not match any template HAttributes and text contents are output as isHTemplates are recursively applied on subelementsnThe template matches all elements that do not match any other templateHUsed to ensure that their contents do not get output.nIf an element matches several templates,only one is used based on a complex priority scheme/user-defined prioritiesSilberschatz,Korth and Sudarshan,Bo Zhou10.42Database System ConceptsCreating XML OutputnAny text or tag in the XSL stylesheet that is not in the xsl namespace is output as isnE.g.to wrap results in new XML elements.HExample output:Joe Mary Silberschatz,Korth and Sudarshan,Bo Zhou10.43Database System ConceptsCreating XML Output(Cont.)nNote:Cannot directly insert a xsl:value-of tag inside another tagHE.g.cannot create an attribute for in the previous example by directly using xsl:value-ofHXSLT provides a construct xsl:attribute to handle this situation4xsl:attribute adds attribute to the preceding element4E.g.res
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 管理文书 > 施工组织


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!