
16.12.2009 8. Updates + XSLT 8.1 Introduction 8.2 Full document replacement XML Databases 8.3 XQuery Update Facility 8. Updates + XSLT, 16.12.09 8.4 XSLT & the XSLTRANSFORM function Silke Eckstein 8.5 Overview Andreas Kupfer Institut für Informationssysteme 8.6 References Technische Universität Braunschweig http://www.ifis.cs.tu-bs.de XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 2 8.1 Introduction 8. Updates + XSLT • Three general techniques for modifying XML 8.1 Introduction documents: – Full document replacement 8.2 Full document replacement • Replace existing document with an updated one 8.3 XQuery Update Facility – XQueryUpdate Facility • Standardized extension to XQuery 8.4 XSLT & the XSLTRANSFORM function • Modify, insert or delete individual elements and attributes within an XML document 8.5 Overview – Extensible Stylesheet Language Transformation (XSLT) 8.6 References • Apply a style sheet to an XML document • Use XSLTRANSFORM function to do this in SQL statements [NK09] XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 3 XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 4 8.2 Full document replacement 8.2 Full document replacement • Replacing a full XML document • Example – Use regular SQL UPDATE statement to replace a full XML UPDATE customer document in a table with a new document SET info = '<?xml version="1.0" encoding="UTF-8" ?> • treats XML document as a "black box" <customerinfo Cid="1010"> • application needs to provide new document <name>Larry Trotter</name> <addr country="England"> – UPDATE statement needs to select a single row <street>5 Rosewood</street> • ... predicate on the relational columns of the table </addr> • predicates on an XML element value <phone type="work">416-555-1358</phone> • </customerinfo>' predicates on an XML attribute value WHERE cid = 1000; • predicates on XML and relational values – New documents can be provided via parameter markers [NK09] XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 5 [NK09] XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 6 1 16.12.2009 8.2 Full document replacement 8.2 Full document replacement • Using parameter marker or host variables • 2 more examples: – to provide the new XML document: UPDATE customer UPDATE customer SET info = ? WHERE cid = 1000 SET info = ? WHERE XMLEXISTS('$INFO/customerinfo[name = "Larry Trotter"] UPDATE customer SET info = :hvar WHERE cid = 1000 AND cid = 1000; – ... and to provide the relational value: UPDATE customer SET info = ? WHERE cid = ? UPDATE customer SET info = ? UPDATE customer SET info = :hvar WHERE cid = :hvar2 WHERE XMLEXISTS('$INFO/customerinfo/phone[type = "work" and text()="416-555-1358"]'); • Replacing an existing XML document with a NULL value – removes the document from the row without deleting the row: UPDATE customer SET info = NULL WHERE cid = 1000 [NK09] XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 7 [NK09] XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 8 8. Updates + XSLT 8.3 XQuery Update Facility • 8.1 Introduction XQuery Update Facility – Standardized extension to XQuery 8.2 Full document replacement – Allows to modify, insert or delete individual elements or attributes within an XML document 8.3 XQuery Update Facility – Makes updating easier and provides more performance than full document replacements 8.4 XSLT & the XSLTRANSFORM function – Allows to modify nodes in the following way: • Replace the value of a node 8.5 Overview • Replace a node with a new one • Insert a new node (at a specific location) 8.6 References • Delete a node • Rename a node • Modify multiple nodes in a document in a single statement • Update multiple documents ib a single statement XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 9 XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 10 8.3 XQuery Update Facility 8.3 XQuery Update Facility • XQuery Update Facility: New XQuery expressions • Node insertion XQuery expressions – An insert expression is an updating expression that inserts ExprSingle ::= FLWORExpr copies of zero or more nodes into a designated position | QuantifiedExpr | TypeswitchExpr with respect to a target node. | IfExpr Syntax and examples | InsertExpr taken from the W3C Syntax | DeleteExpr web site. InsertExpr ::= "insert" ("node" | "nodes") | RenameExpr SourceExpr InsertExprTargetChoice TargetExpr | ReplaceExpr InsertExprTargetChoice ::= (("as" ("first" | "last"))? "into") | TransformExpr | "after" | "before" SourceExpr ::= ExprSingle | OrExpr TargetExpr ::= ExprSingle – N.B. Updating expressions (insert, delete, rename, replace) lead to a loss of type/validation information at the affected nodes. Such information may be recovered by revalidation . [Scholl07] XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 11 [Scholl07] XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 12 2 16.12.2009 8.3 XQuery Update Facility 8.3 XQuery Update Facility • Node insertion: Examples • Node deletion – A delete expression deletes zero or more nodes from an XDM Insert a year element after the publisher of the first book. instance. insert node <year>2005</year> – The keywords node and nodes may be used interchangeably, after fn:doc("bib.xml")/books/book[1]/publisher regardless of how many nodes are actually deleted. Syntax Navigating by means of several bound variables, insert a new DeleteExpr ::= "delete" ("node" | "nodes") TargetExpr police report into the list of police reports for a particular TargetExpr ::= ExprSingle accident. Delete the last author of the first book in a given bibliography. insert node $new-police-report delete node as last into fn:doc("insurance.xml")/policies fn:doc("bib.xml")/books/book[1]/author[last()] /policy[id = $pid] /driver[license = $license] Delete all email messages that are more than 365 days old. /accident[date = $accdate] delete nodes /email/message /police-reports [fn:currentDate() - date > xs:dayTimeDuration("P365D")] [Scholl07] XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 13 [Scholl07] XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 14 8.3 XQuery Update Facility 8.3 XQuery Update Facility • Node replacement • Node replacement: Examples Syntax Replace the publisher of the first book with the publisher of the second ReplaceExpr ::= "replace" ("value" "of")? "node" book. TargetExpr "with" ExprSingle TargetExpr ::= ExprSingle replace node fn:doc("bib.xml")/books/book[1]/publisher with fn:doc("bib.xml")/books/book[2]/publisher – Replace takes two forms, depending on whether value of is specified: • If value of is not specified, a replace expression replaces one node with a new sequence of zero or more nodes. The replacement nodes occupy the Increase the price of the first book by ten percent. position in the node hierarchy that was formerly occupied by the node that replace value of node fn:doc("bib.xml")/books/book[1]/price was replaced. with fn:doc("bib.xml")/books/book[1]/price * 1.1 – Hence, an attribute node can be replaced only by zero or more attribute nodes, and an element, text, comment, or processing instruction node can be replaced only by zero or more element, text, comment, or processing instruction nodes. • If value of is specified, a replace expression is used to modify the value of a node while preserving its node identity . [Scholl07] XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 15 [Scholl07] XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 16 8.3 XQuery Update Facility 8.3 XQuery Update Facility • Renaming nodes • Renaming is local! – A rename expression replaces the name property of a data – The effects of a rename expression are limited to its target model node with a new QName. node, descendants are not affected . Global change of names or namespaces needs explicit iteration. Syntax RenameExpr ::= "rename" "node" TargetExpr "as" Example (Change all QNames from prefix abc to xyz and new namespace URI NewNameExpr http://xyz/ns for node $root and its decendents.) for $node in $root//abc:* let $localName := fn:local-name($node), Rename the first author element of the first book to principal-author. $newQName := fn:concat("xyz:", $localName) rename node fn:doc("bib.xml")/books/book[1]/author[1] return as "principal-author" rename node $node as fn:QName("http://xyz/ns", $newQName), for $attr in $node/@abc:* let $attrLocalName := fn:local-name($attr), Rename the first author element of the first book to the QName that $attrNewQName := fn:concat("xyz:", $attrLocalName) is the value of the variable $newname. return rename node fn:doc("bib.xml")/books/book[1]/author[1] rename node $attr as fn:QName("http://xyz/ns", as $newname $attrNewQName) [Scholl07] XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 17 [Scholl07] XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 18 3 16.12.2009 8.3 XQuery Update Facility 8.3 XQuery Update Facility • Node transformation • Node transformation: Examples – . creates modified copies of existing nodes. Each copied node Return a sequence consisting of all employee elements that have Java obtains a new node identity. The resulting XDM instance can as a
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages9 Page
-
File Size-