XQuery: XML SQL

Source: Internet
Author: User
Tags xquery

Due to project requirements, I have recently studied XQuery in depth.
We usually Parse XML documents, sax, JDOM, and dom4j as common tools. However, we can see whether haschild () and getchild () have a headache.
In fact, if you use XQuery to process XML, everything looks elegant.
In XML, XQuery is just like SQL in relational databases and XQuery scripts. It is just as nice as SQL and stored procedures.
In addition, XQuery can also buckle web pages and use super simple means to make incredible results.
The XQuery engine in Java, which is better now than Saxon. Adds saxon8.jar to its classpath to start Java's XQuery development.
As for debugging tools, you can certainly use Java code. However, both stylus and xmlspy support XQuery. In contrast, stylus supports better and supports point syntax.
There are only a few people in China, and there is not much community support. However, it is quite nice to be familiar with its syntax and write scripts.
The flowor syntax corresponds to the SQL statement. Let $ A: = If () Then () else () is similar to the decode () function of Oracle, and Concat () is the merged string, whether the string is equal to Eq. For other functions, check this when using it:
Http://www.w3.org/TR/xquery-operators/#func-number-examples
Http://www.w3.org/TR/2004/WD-xquery-20040723/#node-tests

Okay, after work, back up today's XQuery code: 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 (){
<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/kgkey01 ,"",
$ Root/usermsgs/kgkey02 ,"",
$ Root/usermsgs/kgkey03 ,"",
$ Root/usermsgs/kgkey04 ,"",
$ Root/usermsgs/kgkey05 ,"",
$ Root/usermsgs/kgkey06 ,"",
$ Root/usermsgs/kgkey07 ,""
)
Else
""
};

(: Create formatdata Node
Declare Function Local: formatdata ($ root as element (){

};

<Adapter>
{
If (exists ($ content/results/WFA) then
<Out>
{
Local: creatfact ($ content/results/WFA)
}
</Out>
Else
<Out> no wfa </out>
}
</Adapter>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.