




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1Roger L. Costello16 June 2010XQuery Update /TR/xquery-update-10/2Prerequisites This tutorial assumes that you know XQuery3XQuery vs XQuery Update XQuery enables you to select nodes from XML documents, and then output new documents containing the nodes. XML XQuery New Document Conver
2、sely, XQuery Update enables you to change an XML document, in-place. XML XQuery Update4Update Operations Insert a node inside, before, or after a specified node. Delete a node. Replace a node by another node. Rename a node 5UsageXQueryProcessorFitnessCenter.xmlFitnessCenter.xuupdateretrieve6XQuery U
3、pdate Processor: SAXON SAXON is an XSLT processor, an XQuery processor, and an XQuery Update processor. I created a DOS batch file to enable you to invoke the SAXON XQuery Update processor. In the examples folders you will find: run-saxon-1-arg.bat Heres how to use it:run-saxon-1-arg FitnessCenter.x
4、u7SAXON flags You need to set these flags when invoking SAXON:-update:on-tree:linked-backup:on (A backup of the file is made before it is updated.)java -classpath %CLASSPATH% net.sf.saxon.Query -update:on -tree:linked -backup:on FitnessCenter.xuNote: these flags are already set in run-saxon-1-arg.ba
5、t8Using Oxygen XML Oxygen XML supports XQuery Update (because it is bundled with Saxon-EE) To enable XQuery update: Select Options Preferences (see next slide)9Set to this value10Using Oxygen XML Select the wrench icon, Select Transformation Scenario (see next slide)11Selectthis12Using Oxygen XML Yo
6、u need to associate the .xu file extension to the XQuery editor. Go to Options Preferences File Types and add a new association there with xu as the extension and XQuery Editor as the editor. (See next slide)13Associate the xu file extensionto XQuery Editor14Back up your XML files! Oxygen XML doesnt
7、 create a backup of your XML files (in their next release they will). So, before executing an update, backup your XML file!15rename16Example: rename element/attribute names to lower case In XQuery.ppt you saw how to transform all element and attribute names to lower case.See next slide 17 Jeff light
8、grey David lightblue Roger lightyellow Jeff lightgrey David lightblue Roger lightyellow XQueryProcessordeclare namespace ex = ;declare function ex:identity-plus-lcase ($seq) for $i in $seq return if ($iself:*) then element lower-case(name($i) for $j in $i/* return attribute lowe
9、r-case(name($j) data($j), ex:identity-plus-lcase($i/child:node() else text $i ;ex:identity-plus-lcase(doc(FitnessCenter.xml)/root()/*)18Solving the problem with XQuery Update The problem is solved much more easily with XQuery Update. Simply visit each element and attribute in the tree and rename it
10、to lower case.See next slide 19 Jeff lightgrey David lightblue Roger lightyellow XQueryProcessorfor $i in doc(FitnessCenter.xml)/(*|*) return rename node $i as lower-case(name($i)rename,rename,rename,see example0120for $i in doc(FitnessCenter.xml)/(*|*) return rename node $i as lower-case(name($i)It
11、erate over each element and attribute node21for $i in doc(FitnessCenter.xml)/(*|*) return rename node $i as lower-case(name($i)Rename the node to the same name, but lower case22Operate on N documents On the next slide I show how to rename all the elements and attributes in two XML documents.23 Jeff
12、lightgrey David lightblue Roger lightyellow XQueryProcessorSee Next Sliderename,rename,rename,FitnessCenter1.xml Jeff lightgrey David lightblue Roger lightyellow FitnessCenter2.xml24see example01-afor $i in doc(FitnessCenter1.xml)/(*|*) | doc(FitnessCenter2.xml)/(*|*) return rename node $i as lower-
13、case(name($i)25Update all the XML filesin a folder The XPath 2.0 doc() function is used to select one file. The XPath 2.0 collection() function is used to select a bunch of files. Heres how to select all *.xml files in a subfolder called dir:collection(dir?select=*.xml;recurse=yes;on-error=ignore)26
14、Rename all the elements and attributes in all *.xml files in the subfolder filesfor $i in collection(dir?select=*.xml;recurse=yes;on-error=ignore)/(*|*) return rename node $i as lower-case(name($i)see example01-b27Bug in SAXON The previous slide is the correct way to solve the problem. However, it w
15、ont work in SAXON SAXON has a bug. To fix the bug, append onto collection() this: /doc(document-uri(.)for $i in collection(dir?select=*.xml;recurse=yes;on-error=ignore)/doc(document-uri(.)/(*|*) return rename node $i as lower-case(name($i)28Update Conflict Suppose you rename a node to lower-
16、case. Then you rename the same node to upper-case. What will be the result? Answer: you will get an update conflict error: Update conflict: two attempts to rename the same element() nodesee example01-c29rename node N as name-expr N must identify a single element or attribute node. name-expr must yie
17、ld a value of type xsd:QName.30Namespace rename Currently each node in FitnessCenter.xml is in no namespace. That is, the namespace URI for each node is Rename each node to have the same local name, but the namespace URI is http:/rename31Heres how to do itfor $i in doc(FitnessCenter.xml)/* return re
18、name node $i as QName(http:/, local-name($i)see example0232QName(namespace, prefix:local-name) QName(namespace, local-name) These are XPath 2.0 functions. They return a value of type xsd:QnameExample: QName(http:/, FitnessCenter)returns FitnessCenter in the gym namespace, with no prefixExample:QName
19、(http:/, gym:FitnessCenter)returns gym:FitnessCenter, where the prefix is associated to the gym namespacesee example02-a33for $i in doc(FitnessCenter.xml)/* return rename node $i as QName(http:/, local-name($i)Rename the node to the same name, but in the gym namespace34Dont do thisfor $i in doc(Fitn
20、essCenter.xml)/* return rename node $i as QName(http:/, name($i)Heres the error message you will get:New name conflicts with existing namespace binding35Explanation The name() function returns both the namespace URI and the local name. (It just so happens the namespace URI is ) The second argument o
21、f the QName() function cannot contain a namespace URI, only local name or a prefix + local name.36Rename the Level attribute to levelfor $i in doc(FitnessCenter.xml)/Member return rename node $i/Level as levelsee example0337Rename FitnessCenter to HealthClubrename node doc(FitnessCenter.xml)/Fitness
22、Center as HealthClubsee example04Do Lab138delete39Delete all Level attributesfor $i in doc(FitnessCenter.xml)/Member return delete node $i/Levelsee example0540Alternate Solutiondelete nodes doc(FitnessCenter.xml)/Levelsee example0641delete node nodeordelete nodes node-sequence If you want to delete
23、one node, usedelete node If you want to delete multiple nodes, usedelete nodes42 Jeff lightgrey David lightblue Roger lightyellow Delete this memberdelete node doc(FitnessCenter.xml)/Memberchild:Name eq Rogersee example0743 Jeff lightgrey David lightblue Roger lightyellow Delete this textdelete node
24、 doc(FitnessCenter.xml)/Memberchild:Name eq Roger/Name/text()see example0844Things you can delete The last slides showed how you can delete:attribute nodeselement nodestext nodes45insert46 Jeff lightgrey David lightblue Roger lightyellow XQueryProcessorlet $source := doc(FitnessCenter.xml)for $i in
25、1 to count($source/Member) return insert node attribute id $i into $source/Member$iinsert id,insert id,insert idsee example0947Insert an attribute into Member Jeff lightgrey David lightblue Roger lightyellow Jeff lightgrey David lightblue Roger lightyellow Do Lab248Places to insert The last two slid
26、es showed how to insert an attribute into an element. You can insert an element A into an element B at different places: first (A will become the first child of B) last (A will become the last child of B) before (A will become the first preceding sibling of B) after (A will become the first followin
27、g sibling of B)49insert node A relative position Binsert node A as first into Binsert node A as last into Binsert node A before Binsert node A after B50Insert as the first child of Member Jeff lightgrey David lightblue Roger lightyellow 1 Jeff lightgrey 2 David lightblue 3 Roger lightyellow let $sou
28、rce := doc(FitnessCenter.xml)for $i in 1 to count($source/Member) return insert node element id $i as first into $source/Member$isee example1051Insert as the last child of Member Jeff lightgrey David lightblue Roger lightyellow Jeff lightgrey 1 David lightblue 2 Roger lightyellow 3 let $source := do
29、c(FitnessCenter.xml)for $i in 1 to count($source/Member) return insert node element id $i as last into $source/Member$isee example1152Insert before Member1 Jeff lightgrey David lightblue Roger lightyellow 201 Boylston St. Boston MA Jeff lightgrey David lightblue Roger lightyellow 53let $source := do
30、c(FitnessCenter.xml)return insert node 201 Boylston St. Boston MA before $source/Member1see example12Insert before Member154Why return?let $source := doc(FitnessCenter.xml)return insert node 201 Boylston St. Boston MA before $source/Member1Why?55Heres why return is requiredThis is how FLWOR is defin
31、ed:(for expr | let expr)+ (where expr)? (order by expr)? return exproptionaloptionalrequiredSuppose you use let56Insert after the last Member Jeff lightgrey David lightblue Roger lightyellow Jeff lightgrey David lightblue Roger lightyellow 3let $source := doc(FitnessCenter.xml)return insert node cou
32、nt($source/Member) after $source/Memberlast()see example1357insert nodes (i.e., sequence) Thus far we inserted just one node. We used: insert node You can insert multiple nodes. Use: insert nodes insert nodes X, Y, Z as first into Binsert nodes X, Y, Z as last into Binsert nodes X, Y, Z before Binse
33、rt nodes X, Y, Z after B58Insert node sequencebefore Member1 Jeff lightgrey David lightblue Roger lightyellow 201 Boylston St. Boston MA Jeff lightgrey David lightblue Roger lightyellow 59let $source := doc(FitnessCenter.xml)return insert nodes ( 201 Boylston St., Boston, MA ) before $source/Member1
34、see example1460Sequences always in (, , )let $source := doc(FitnessCenter.xml)return insert nodes ( 201 Boylston St., Boston, MA ) before $source/Member1Must put the node sequence in parentheses, and separate each item with a comma.61Insert 2 attributes into Member Jeff lightgrey David lightblue Rog
35、er lightyellow Jeff lightgrey David lightblue Roger lightyellow 62let $source := doc(FitnessCenter.xml)for $i in 1 to count($source/Member) return insert nodes ( attribute id $i, attribute active true ) into $source/Member$isee example1563Delete Level then insert it as the first child of Member Jeff
36、 lightgrey David lightblue Roger lightyellow platinum Jeff lightgrey gold David lightblue platinum Roger lightyellow 64for $i in doc(FitnessCenter.xml)/Member return let $name := name($i/Level) let $value := data($i/Level) return ( delete node $i/Level, insert node element $name $value as first into
37、 $i )see example16Do Lab365replace66Replace with prose Jeff lightgrey David lightblue Roger lightyellow Jeff lightgrey Works at Hanscom AFB David lightblue Works in Bedford Roger lightyellow Works in Hawaii 67let $source := doc(FitnessCenter.xml)return ( replace node $source/Member1/Remark/msg with
38、Works at Hanscom AFB, replace node $source/Member2/Remark/msg with Works in Bedford, replace node $source/Member3/Remark/msg with Works in Hawaii )see example1768Replace with its data Jeff lightgrey David lightblue Roger lightyellow Jeff lightgrey David lightblue Roger lightyellow69let $source := do
39、c(FitnessCenter.xml)let $M1 := $source/Member1let $M2 := $source/Member2let $M3 := $source/Member3return ( replace node $M1 with element MemberData normalize-space(data($M1), replace node $M2 with element MemberData normalize-space(data($M2), replace node $M3 with element MemberData normalize-space(
40、data($M3) )see example1870Replace level with id, insert level after Jeff lightgrey David lightblue Roger lightyellow lightgrey lightblue lightyellow 71let $source := doc(FitnessCenter.xml)for $i in $source/Member return ( replace node $i/level with attribute id data($i/Name), delete node $i/Name, in
41、sert node $i/level after $i/FavoriteColor )see example1972Any order is OK!let $source := doc(FitnessCenter.xml)for $i in $source/Member return ( delete node $i/Name, replace node $i/level with attribute id data($i/Name), insert node $i/level after $i/FavoriteColor )see example19-adelete Name, then u
42、se it73Why can I delete an element and then use its value?Below is an XQuery Update that first deletes a element and then replaces level with the value of . How can that be? How can I use after its been deleted? let $source := doc(FitnessCenter.xml)for $i in $source/Member return ( delete node $i/Na
43、me, replace node $i/level with attribute id data($i/Name) )74XQuery updates do not apply during execution. Instead the query will just return a pending update list (during its creation the element is still there) and will then apply all updates.XQuery with Updates is a declarative language: beforean
44、d after are meaningless - the updates are applied at the endall together.75Accumulate Pending Updates Updates are applied when execution of the whole expression completes. Updates are accumulated into a Pending Update List. At some point at the end of execution, Pending Updates are applied all at on
45、ce, and the XML file is updated atomically.76Loop Forever?let $source := doc(FitnessCenter.xml)for $i in $source/Member/Name return insert node $i/text() after $iThe for-loop iterates over each element. Each iteration adds another element, i.e., it is a self-replicating loop. Will it ever stop?see e
46、xample19-b77The Loop Does Stop Create the set of elements to be inserted. Insert the elements. Done.78Record each Members BMI(Body Mass Index) Jeff lightgrey 36 32 David lightblue 25 Roger lightyellow Jeff lightgrey 36 32 David lightblue 25 Roger lightyellow 10 Add a BMI record for Roger79declare na
47、mespace ex = ;declare variable $source := doc(FitnessCenter.xml);declare updating function ex:insert-BMI ($person, $BMI) if (empty($person/BMI-Values) then insert node $BMI as last into $person else insert node $BMI as last into $person/BMI-Values;ex:insert-BMI($source/Member3,
48、10)see example19-d80declare namespace ex = ;declare variable $source := doc(FitnessCenter.xml);declare updating function ex:insert-BMI ($person, $BMI) if (empty($person/BMI-Values) then insert node $BMI as last into $person else insert node $BMI as last into $person/BMI-Values;e
49、x:insert-BMI($source/Member3, 10)If the body of a function contains an updating expression, then the function must be declared with the updating keyword. If you forget to add this, you will get this error: The body of a non-updating function must be a non-updating expression.81declare namespace ex =
50、 ;declare variable $source := doc(FitnessCenter.xml);declare updating function ex:insert-BMI ($person, $BMI) if (empty($person/BMI-Values) then insert node $BMI as last into $person else insert node $BMI as last into $person/BMI-Values;ex:insert-BMI($source/Member3, 10)Notice th
51、at no return type is specified (e.g., as element(). An updating function returns no value and therefore is not allowed to declare a return type.82This Wont Workdeclare namespace ex = ;declare variable $source := doc(FitnessCenter.xml);declare updating function ex:insert-BMI ($pe
52、rson, $BMI) if (empty($person/BMI-Values) then insert node as last into $person else (), insert node $BMI as last into $person/BMI-Values;ex:insert-BMI($source/Member3, 10)see example19-c83Comparedeclare updating function ex:insert-BMI ($person, $BMI) if (empty($person/BMI-Values) then insert node $
53、BMI as last into $person else insert node $BMI as last into $person/BMI-Values;declare updating function ex:insert-BMI ($person, $BMI) if (empty($person/BMI-Values) then insert node as last into $person else (), insert node $BMI as last into $person/BMI-Values;84Cant Build on Previous Updatesdeclare
54、 updating function ex:insert-BMI ($person, $BMI) if (empty($person/BMI-Values) then insert node as last into $person else (), insert node $BMI as last into $person/BMI-Values;This insert statement is trying to insert after has been inserted. This is not allowed. The next slides explain why.85Declara
55、tive Programming XQuery and XQuery Update are declarative programming languages.86Declarative Programming Requires a Different MindsetFirst, distinguish between imperative and declarative programming: Imperative: specify how to do it; prescriptive. Declarative: specify what you want; descriptive.Imp
56、erative programming is like a recipe: do this first, then that second, and so forth. Statement A must be executed before statement B.In declarative programming before and after are meaningless. Statements can be executed in any order, even in parallel.Below are two examples that illustrate-in a rath
57、er dramatic fashion-the differences between imperative and declarative programming.87EXAMPLE #1Consider this XML document that contains the name and favorite color of members of a fitness center: Jeff lightgrey David lightblue Roger lightyellow Note that each member has an id attribute.I want to cha
58、nge the document so that each member is identified by his name. I want to change this: Jeff lightgrey to this: lightgrey 88Heres some pseudo code that shows how to accomplish this:For each element: 1. replace id with the value of 2. delete Some key things to note:A. In my preamble I said . pseudo co
59、de that shows how to . Thats prescriptive.B. There is a definite order of actions - delete only after extracting its value.This is all very imperative. Is declarative programming not useful for this application? In fact declarative programming is well suited for this application. Lets recast the pse
60、udo-code into the declarative mindset: For each element: The new document has the id attributes value replaced with what was previously the value of the element, and the element no longer exists.Here is a key thing to note: The new pseudo-code is a description of the new document. It is not a recipe
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 广西壮族自治区柳州市2025年七年级下学期语文期末试卷及答案
- 2025年中考历史单选与多选分类复习题集及答案(原创)
- 城市交通规划案例研究重点基础知识点
- 教师移动端备课实施路径
- 建筑施工资料员培训课件:提升工程档案管理技能
- 《当代企业创新策略》课件
- 医学检验重点专科建设汇报
- 火化合伙协议书
- 转让纠纷协议书范本
- 软件租用商务合同协议
- 湖南省天壹名校联盟2025届高三5月适应性考试(化学)
- 浙江省杭州地区(含周边)重点中学2024-2025学年高一下学期期中考试化学试卷(含答案)
- 房地产广告效果的评测与分析
- 2025年北京市石景山区九年级初三一模语文试卷(含答案)
- 华大新高考联盟2025届高三4月教学质量测评历史+答案
- 2024年7月27日内蒙古阿拉善盟直机关遴选笔试真题及解析
- 穴位埋线疗法疗法
- 电解加工机床的设计
- 某煤矿中央变电所供电设计1
- 架子工实操试题(共10页)
- 心电图技术操作考核评分标准
评论
0/150
提交评论