xquery:xml的sql

由于项目需要,最近较深入的研究了一下xquery.
通常我们解析xml文档,sax,jdom,dom4j是常用工具.不过看到满篇的hasChild(),getChild()是否很头痛.
其实如果采用xquery来处理xml,一切都显得很优雅.
xquery之于xml,就如同sql之于关系数据库,写xquery脚本,就如同写sql,存储过程一样的爽.
并且,xquery还可以扣网页,用超级简单的手段就可以做出令人匪夷所思的效果.
java中的xquery引擎,目前较好的是saxon.把saxon8.jar加到自己的classpath,就可以开始java的xquery开发了.
至于调试工具,你当然可以用java代码来进行,不过,stylus和xmlspy都支持xquery,相比之下,stylus支持的更好,支持点语法.写起来顺手许多.
只是国内用的人少,社区支持不多.不过,熟悉了它的语法,写起脚本来,还是很爽的.
flowor语法对应sql语句, let $a := if () then () else () 类似oracle的decode()函数,concat()为合并字符串,字符串是否相等为eq,至于其他函数,用的时候查查这个就可以了:
http://www.w3.org/TR/xquery-operators/#func-number-examples
http://www.w3.org/TR/2004/WD-xquery-20040723/#node-tests

好了,下班了,备份一下今天的xquery代码: osstr.xquery

xquery version "1.0";

declare variable $content :=//ResponseMessage;

declare variable $tmpelement:= local:getTmpelement();

declare function local:getTmpelement() as element() {
 if(exists($content/results/WFA/TRTRERSP)) then
  $content/results/WFA/TRTRERSP
 else if (exists($content/results/WFA/CKTIDRSP) )then
  $content/results/WFA/CKTIDRSP
 else
     ""
};

(:create fact node:)
declare function local:creatFact($s as element()) as element() {
  <fact>
  <TN value="{replace(//CKTID,"/","")}"/>
  <LN value="{//CUSTNAME}"/>
  <SA value="{//CUSTADDR}"/>
  <LOC value="{//P2LOC}"/>
  <TTN value="{//TR}"/>
  <TRBL value="{//TRBLTYPE}"/>
  <STATUS value="{(//STATUS/STATUS)[1]}"/>
  <DATE value="{(//STATUS/STAT_DT)[1]}"/>
  <TIME value="{(//STATUS/STAT_TM)[1]}"/>
  <flag value="{local:createGroupKey($content/results/WFA)}"/>
 </fact>
};

declare function local:getMCN($s as xs:string) as xs:string {
 if (string-length($s) > 4) then
  substring($s,4,2)
 else
  ""
  
};

declare function local:getOCS($s as xs:string) as xs:string {
 if ($s eq "Y" or $s eq "y" ) then
  "OCS"
 else
  ""
};

declare function local:createGroupKey($root as element()) as xs:string {
 let $n :=
     normalize-space(
   concat(local:createGrpkey($root )," ",
    $tmpelement/CSN," ",
    $tmpelement/CSU,"  ",
    $tmpelement/MP_USOC," ",
    $tmpelement/MP_USOC1," ",
    $tmpelement/MP_USOC2

    )
   )
   
    let $m :=
  if (exists($tmpelement/MCN)) then
   local:getMCN($tmpelement/MCN)
  else
   ""
 let $o:=
  if (exists($tmpelement/OCS)) then
   local:getOCS($tmpelement/OCS)
  else
   "" 
 return
  normalize-space(
   concat($n, " ", $m, " ",$o)
  )
};

declare function local:createGrpkey($root as element()) as xs:string {
   if (exists($root/USERMSGS)) then
     concat($root/USERMSGS/GRPKEY01, " ",
     $root/USERMSGS/GRPKEY02, " ",
     $root/USERMSGS/GRPKEY03, " ",
     $root/USERMSGS/GRPKEY04, " ",
     $root/USERMSGS/GRPKEY05, " ",
     $root/USERMSGS/GRPKEY06, " ",
     $root/USERMSGS/GRPKEY07, " "
     )
 else
      ""
};

(:create formatData node
declare function local:formatData($root as element()) as element() {


};
:)

<adapter>
{
if (exists($content/results/WFA)) then
<out>
 {
  local:creatFact($content /results/WFA)
 }
</out>
else
<out>no wfa</out>
}
</adapter> 



你可能感兴趣的:(oracle,sql,xml,脚本)