XQuery嵌套循环 [英] XQuery Nested For Loop

查看:535
本文介绍了XQuery嵌套循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < user_manual document-type =IUts-cover =Language Part altts-lang = - ts-multi =语言部分章节ts-overview = - metadata1 =9211brand =Boschmaterial-number =dummy_9000727237vib = RW404960language-codes =de,enproduction-date =2012-12-03layout-type = - id =SL9761901> 
< embed-language_part id =SL14686180>
< language_part id =ecls_bio_becls_a3_a14686169.SL9761756.7role = - lang =de>
< embed-user_manual_part id =ecls_bio_becls_a3_a14686169.SL14686446.10>
<?ecls-start-embedded-resource resource =ecls_bio_becls_a3_a17144756?>
< user_manual_part id =ecls_bio_becls_a3_a17144756.SL9765760.11role = - document-type =IU>
< embed-chapter id =ecls_bio_becls_a3_a17144756.SL9762101.13>
<?ecls-start-embedded-resource resource =ecls_bio_becls_a3_a30660983?>
< embed-title_module id =ecls_bio_becls_a3_a30660983.SL2361816.15>
<?ecls-start-embedded-resource resource =ecls_bio_becls_a3_a611713?>
< title_module id =ecls_bio_becls_a3_a611713.SL873735.16role = - >
< title id =ecls_bio_becls_a3_a611713.SL873736.17> Sicherheits- und Warnhinweise< / title>
< / title_module>
<?ecls-end-embedded-resource resource =ecls_bio_becls_a3_a611713?>
< / embed-title_module>
< embed-section id =ecls_bio_becls_a3_a30660983.SL10400094.18>
<?ecls-start-embedded-resource resource =ecls_bio_becls_a3_a11752692?>
< section id =ecls_bio_becls_a3_a11752692.SL1742298.19footrowtitle =norole = - toctitle =yes>
<?Pub Caret1?>
< embed-title_module id =ecls_bio_becls_a3_a11752692.SL1742291.20>
<?ecls-start-embedded-resource resource =ecls_bio_becls_a3_a16181733?>
< title_module id =ecls_bio_becls_a3_a16181733.SL984238.21role = - >
< title id =ecls_bio_becls_a3_a16181733.SL984239.22> Betrieb nehmen的Bevor Sie das Gerat< / title>
< para id =ecls_bio_becls_a3_a16181733.SL984240.23> Lesen Sie Gebrauchs- und Montageanleitung aufmerksam durch! Sie enthalten wichtige InformationenÖberAufstellen,Gebrauch und Wartung desGerütes。< / para>
< para id =ecls_bio_becls_a3_a16181733.SL984241.24> Der Hersteller haftet nicht,wenn die die Hinweise und Warnungen der Gebrauchsanleitung missachten。 Bewahren Sie alle Unterlagenf rsp terenGebrauch oderf rNachbesitzer auf。< / para>
< / title_module>
<?ecls-end-embedded-resource resource =ecls_bio_becls_a3_a16181733?>
< / embed-title_module>
< / section>
<?Pub * 0000000275?>
<?ecls-end-embedded-resource resource =ecls_bio_becls_a3_a11752692?>
< / embed-section>
< / chapter>
< / embed-chapter>
< / user_manual_part>
< / embed-user_manual_part>
< / language_part>
< / embed-language_part>
< / user_manual>

我想使用XQuery语言,但是对于这种查询语言我真的很陌生。



我需要的基础设施是这样的:我想获得章节和章节的标题,例如:

这一章就像是在这个xml文件中:

 < chapter id =ecls_bio_becls_a3_a30660983.SL2300626.14role = -  toctitle =yesfootrowtitle =notype =security> 
< embed-title_module id =ecls_bio_becls_a3_a30660983.SL2361816.15>
<?ecls-start-embedded-resource resource =ecls_bio_becls_a3_a611713?>
< title_module id =ecls_bio_becls_a3_a611713.SL873735.16role = - >
< title id =ecls_bio_becls_a3_a611713.SL873736.17> Sicherheits- und Warnhinweise< / title>
...

在这个例子中,Sicherheits-ind Warnhinweise是章节元素的标题。章节可以有很多部分,在我们的例子中,标题是:

 < section id =ecls_bio_becls_a3_a11752692.SL1742298.19footrowtitle =norole = - toctitle =yes> 
<?Pub Caret1?>
< embed-title_module id =ecls_bio_becls_a3_a11752692.SL1742291.20>
<?ecls-start-embedded-resource resource =ecls_bio_becls_a3_a16181733?>
< title_module id =ecls_bio_becls_a3_a16181733.SL984238.21role = - >
< title id =ecls_bio_becls_a3_a16181733.SL984239.22> Betrieb nehmen的Bevor Sie das Gerat< / title>
...



Betrieb nehmen的Bevor Sie das Gerat



预期结构:

 < chapter> title:Chapter's title
< section> title:First section's title< / section>
< section>标题:第二部分的标题< / section>
< / chapter>

对于这个例子,章节只有一个部分,但是我只是配置了一个来达到我的文件的解决方案,它是最可读的配置,我猜... ...

我试过下面的xquery查询:

 $ doc / user_manual / embed-language_part / language_part / embed-user_manual_part / user_manual_part中的< chapter 
let $ doc:= doc(Test.xml)
/ embed-chapter / chapter
let $ chapter_title:= $​​ chapter / embed-title_module / title_module / title $ b $ for $ section in $ chapter / embed-section / section
return< chapter> title:{data($ chapter_title)}< section>标题:{数据($部/嵌入-title_module / title_module /标题)}< /节>< /章节>

带有两个嵌套循环,但是这个代码使得我的结构像:

 < chapter> title:Chapter's title< section>First section's title< / section>< / chapter> 
< chapter> title:Chapter's title< section>Second section's title< / section>< / chapter>

问题在于循环,但是当我尝试编辑这样的代码时:

$ b $ $ doc / user_manual / embed-language_part / language_part / embed
$

  let $ doc:= doc(assemble.xml)
-user_manual_part / user_manual_part / embed-chapter / chapter
let $ chapter_title:= $​​ chapter / embed-title_module / title_module / title
< chapter> title:{data($ chapter_title)} {
for $ section in $ chapter / embed-section / section
return< section>标题:{数据($部/嵌入-title_module / title_module /标题)}< /节>
}< / chapter>

我只想把标签放在第一个循环的标签中,之后我将添加部分元素查询。但是,上面的命令是不符合我的期望。



所以我需要你的建议,以正确地得到这些结构。先谢谢你。我希望我清楚地告诉你们所有人。假设文件名为assemble.xml。

解决方案

 < chapters> {
let $ doc:= doc(assemble.xml)
for $ chapter in $ doc / user_manual / embed-language_part / language_part / embed-user_manual_part / user_manual_part / embed-chapter / chapter
返回
< chapter>
title:$ chapter $ embed-section / section




$ b $ return $ lt; section> ; title:{data($ section / embed-title_module / title_module / title)}< / section>
}
< / chapter>
}< / chapter>

我在互联网上搜索,发现如何进行内部循环操作。下面的代码完全符合我的期望。


I've a sample xml file such as :

<user_manual document-type="IU" ts-cover="Language Part alt" ts-lang="-" ts-multi="Language Part, Chapter" ts-overview="-" metadata1="9211" brand="Bosch" material-number="dummy_9000727237" vib="RW404960" language-codes="de, en" production-date="2012-12-03" layout-type="-" id="SL9761901">
<embed-language_part id="SL14686180">
<language_part id="ecls_bio_becls_a3_a14686169.SL9761756.7" role="-" lang="de">
<embed-user_manual_part id="ecls_bio_becls_a3_a14686169.SL14686446.10">
<?ecls-start-embedded-resource resource="ecls_bio_becls_a3_a17144756"?> 
<user_manual_part id="ecls_bio_becls_a3_a17144756.SL9765760.11" role="-" document-type="IU">
<embed-chapter id="ecls_bio_becls_a3_a17144756.SL9762101.13">
<?ecls-start-embedded-resource resource="ecls_bio_becls_a3_a30660983"?> 
<chapter id="ecls_bio_becls_a3_a30660983.SL2300626.14" role="-" toctitle="yes" footrowtitle="no" type="security">
<embed-title_module id="ecls_bio_becls_a3_a30660983.SL2361816.15">
<?ecls-start-embedded-resource resource="ecls_bio_becls_a3_a611713"?> 
<title_module id="ecls_bio_becls_a3_a611713.SL873735.16" role="-">
<title id="ecls_bio_becls_a3_a611713.SL873736.17">Sicherheits- und Warnhinweise</title> 
</title_module>
<?ecls-end-embedded-resource resource="ecls_bio_becls_a3_a611713"?> 
</embed-title_module>
<embed-section id="ecls_bio_becls_a3_a30660983.SL10400094.18">
<?ecls-start-embedded-resource resource="ecls_bio_becls_a3_a11752692"?>  
<section id="ecls_bio_becls_a3_a11752692.SL1742298.19" footrowtitle="no" role="-" toctitle="yes">
<?Pub Caret1?> 
<embed-title_module id="ecls_bio_becls_a3_a11752692.SL1742291.20">
<?ecls-start-embedded-resource resource="ecls_bio_becls_a3_a16181733"?> 
<title_module id="ecls_bio_becls_a3_a16181733.SL984238.21" role="-">
<title id="ecls_bio_becls_a3_a16181733.SL984239.22">Bevor Sie das Gerat in Betrieb nehmen</title> 
<para id="ecls_bio_becls_a3_a16181733.SL984240.23">Lesen Sie Gebrauchs- und Montageanleitung aufmerksam durch! Sie enthalten wichtige Informationen �ber Aufstellen, Gebrauch und Wartung des Ger�tes.</para> 
<para id="ecls_bio_becls_a3_a16181733.SL984241.24">Der Hersteller haftet nicht, wenn Sie die Hinweise und Warnungen der Gebrauchsanleitung missachten. Bewahren Sie alle Unterlagen f�r sp�teren Gebrauch oder f�r Nachbesitzer auf.</para> 
</title_module>
<?ecls-end-embedded-resource resource="ecls_bio_becls_a3_a16181733"?> 
</embed-title_module>
</section>
<?Pub *0000000275?> 
<?ecls-end-embedded-resource resource="ecls_bio_becls_a3_a11752692"?> 
</embed-section>
</chapter>
</embed-chapter>
</user_manual_part>
</embed-user_manual_part>
</language_part>
</embed-language_part>
</user_manual>

I want to use the XQuery language but i am really new to this query language.

The infrastructure I need is this : I want to get the chapter's and its' sections with their titles, for example:

The chapter is like in this xml file :

    <chapter id="ecls_bio_becls_a3_a30660983.SL2300626.14" role="-" toctitle="yes" footrowtitle="no" type="security">
    <embed-title_module id="ecls_bio_becls_a3_a30660983.SL2361816.15">
    <?ecls-start-embedded-resource resource="ecls_bio_becls_a3_a611713"?> 
    <title_module id="ecls_bio_becls_a3_a611713.SL873735.16" role="-">
    <title id="ecls_bio_becls_a3_a611713.SL873736.17">Sicherheits- und Warnhinweise</title>
...

In that example Sicherheits- ind Warnhinweise is the title of chapter element. Chapters can have many sections and in our example section's title is :

       <section id="ecls_bio_becls_a3_a11752692.SL1742298.19" footrowtitle="no" role="-" toctitle="yes">
        <?Pub Caret1?> 
        <embed-title_module id="ecls_bio_becls_a3_a11752692.SL1742291.20">
        <?ecls-start-embedded-resource resource="ecls_bio_becls_a3_a16181733"?> 
        <title_module id="ecls_bio_becls_a3_a16181733.SL984238.21" role="-">
        <title id="ecls_bio_becls_a3_a16181733.SL984239.22">Bevor Sie das Gerat in Betrieb nehmen</title> 
...

Bevor Sie das Gerat in Betrieb nehmen.

The expected structure :

<chapter> title:"Chapter's title"
<section>title:"First section's title"</section>
<section>title:"Second section's title"</section>
</chapter>

For that example chapter has just one section but I just configured this one to reach a solution of my file, it is most readable configuration I guess...

I tried below xquery query :

<chapter
    let $doc := doc("Test.xml")
    for $chapter in $doc/user_manual/embed-language_part/language_part/embed-user_manual_part/user_manual_part/embed-chapter/chapter
        let $chapter_title := $chapter/embed-title_module/title_module/title
        for $section in $chapter/embed-section/section
        return <chapter>title:{data($chapter_title)} <section> title:{data($section/embed-title_module/title_module/title)}</section></chapter>

with two nested loops, but this code makes my structure like :

<chapter>title:"Chapter's title"<section>"First section's title"</section></chapter>
<chapter>title:"Chapter's title"<section>"Second section's title"</section></chapter>

The problem was looping but when I try to edit code like this :

let $doc := doc("assemble.xml")
    for $chapter in $doc/user_manual/embed-language_part/language_part/embed-user_manual_part/user_manual_part/embed-chapter/chapter
        let $chapter_title := $chapter/embed-title_module/title_module/title
        <chapter> title:{data($chapter_title)} {
        for $section in $chapter/embed-section/section
        return <section> title:{data($section/embed-title_module/title_module/title)}</section>
        }</chapter>

I just want to put the tag in the first loop ones, after that i will add section elements to query. But the above command is not working for my expectation.

So i need your suggestions to get these structure properly. Thank you in advance. I wish i was clearly told the situation to all of you. The name of file assumed as "assemble.xml".

解决方案

<chapters>{
let $doc := doc("assemble.xml")
for $chapter in $doc/user_manual/embed-language_part/language_part/embed-user_manual_part/user_manual_part/embed-chapter/chapter
return
    <chapter>
    title:{data($chapter/embed-title_module/title_module/title)}
        {
            for $section in $chapter/embed-section/section
                return <section>title: {data($section/embed-title_module/title_module/title)}</section>
        }
    </chapter>
}</chapters>

I searched around the internet and i found how to make inner loop operation. The code below is working perfectly for my expectations.

这篇关于XQuery嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆