xquery:xml的sql

來源:互聯網
上載者:User

由於項目需要,最近較深入的研究了一下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> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.