How much do you know about XQuery Element construction?

Source: Internet
Author: User
Tags xquery
I encountered a problem today. For the following XML string
<Opdetail> <recordinfo> <fieldinfo> <fieldchname> alarm serial number </fieldchname> <fieldenname> alarmid </fieldenname> <fieldcontent> detail </fieldcontent> </fieldinfo> <> <fieldchname> Alert ID </fieldchname> <fieldenname> alarmstaid </fieldenname> <fieldcontent> 2014070200101 </fieldcontent> </fieldinfo> </recordinfo> </opdetail>
How to convert to the following form:
<opDetail>    <recordInfo>        <alarmId>GJ_2816071613_352385154_2970760107_588354443</alarmId>        <alarmStaId>2014070200101</alarmStaId>   </recordInfo></opDetail>
Based on my previous experience in configuring various parsing scripts in the project, my intuition tells me that using the XQuery script can implement such a conversion process, I saw a similar example when I read the XQuery authoritative guide. So I opened the XQuery authoritative guide and quickly found a solution based on the directory prompts, is to use XQuery to construct elements.
There are two methods to construct elements using XQuery: Direct Element construction and computed construction:
  • Direct Element construction
This construction method is simple and easy to understand. Based on the description in the book, "The direct constructor uses XML-like syntax to create elements and attributes with fixed names ." I didn't pay too much attention to this sentence at the beginning. "using XML-like Syntax" is a nonsense, the phrase "elements and attributes used to create fixed names" was directly ignored by me. When solving the above problems, I realized the importance of this sentence. The author means that direct Element construction is suitable for element construction with the node name and attribute name known. In our problem, the node name is unknown, therefore, you cannot directly construct elements. Now, we may not know how the direct Element construction works. Let's take two examples.
<opDetail>    <recordInfo>    {    for $a in //fieldInfo    return <name>{$a/fieldEnName/text()}</name>    }    </recordInfo></opDetail>
Run the above script on the XML string in stylus-studio and get the following results:
<opDetail><recordInfo><name>alarmId</name><name>alarmStaId</name></recordInfo></opDetail>
We can see that the names of nodes opdetail, recordinfo, and name are fixed. For example
<opDetail>    <recordInfo>    {    for $a in //fieldInfo    return <name fieldChName="{$a/fieldChName/text()}">{$a/fieldEnName/text()}</name>    }    </recordInfo></opDetail>
Run the above script on the XML string in stylus-studio and get the following results:
<Opdetail> <recordinfo> <name fieldchname = "alarm serial number"> alarmid </Name> <name fieldchname = "alarm ID"> alarmstaid </Name> </recordinfo> </ opdetail>
Similarly, the attribute name fieldchname is also fixed.
  • Computing Structure
Unlike the direct element structure, the description of the computation constructor in the book is "allow dynamic generation of names in queries ." The calculation element constructor uses the keyword element followed by the name and content. The name can be a string or a name expression enclosed in braces, and the content must be a name expression. Syntax: element name/name expression content expression is the simplest construction method, which is no different from the preceding direct Element construction method. The node name is fixed:
element html {    element h1 { "This is a Test"}}
After running, the returned result is
If you are targeting the XML string in the problem, execute the following statement
element opDetail {    for $a in //fieldInfo    return element {$a/fieldEnName/text()} {$a/fieldContent/text()}}
The returned result is
<opDetail><alarmId>GJ_2816071613_352385154_2970760107_588354443</alarmId><alarmStaId>2014070200101</alarmStaId></opDetail>
In addition, you can use a fixed string + operation result for the node name. Similarly, you can run the following statement for the XML file in the problem:
element opDetail {    for $a in //fieldInfo    return element {concat("ele",$a/fieldEnName/text())} {$a/fieldContent/text()}}
The returned result is
<opDetail><elealarmId>GJ_2816071613_352385154_2970760107_588354443</elealarmId><elealarmStaId>2014070200101</elealarmStaId></opDetail>
More chestnuts will not be introduced much. You can find many chestnuts in chapter 5 of the XQuery authoritative guide.
In general, the XQuery element structure is divided into direct construction and computing construction. The direct element structure applies to the case where the node/attribute name remains unchanged, and the computing structure applies to the case where the node/attribute name is variable.

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.