关于 xml:XQuery 嵌套 For 循环

XQuery Nested For Loop

我有一个示例 xml 文件,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<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">

<language_part id="ecls_bio_becls_a3_a14686169.SL9761756.7" role="-" lang="de">

<?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">

<?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">

<?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_module>
<?ecls-end-embedded-resource resource="ecls_bio_becls_a3_a611713"?>
</embed-title_module>

<?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?>

<?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
<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>

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

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

章节就像在这个xml文件中:

1
2
3
4
5
6
    <chapter id="ecls_bio_becls_a3_a30660983.SL2300626.14" role="-" toctitle="yes" footrowtitle="no" type="security">
   
    <?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
...

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

1
2
3
4
5
6
7
       <section id="ecls_bio_becls_a3_a11752692.SL1742298.19" footrowtitle="no" role="-" toctitle="yes">
        <?Pub Caret1?>
       
        <?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
...

Bevor Sie das Gerat in Betrieb nehmen.

预期的结构:

1
2
3
4
<chapter> title:"Chapter's title"
<section>title:"First section's title"</section>
<section>title:"Second section's title"</section>
</chapter>

对于那个示例章节只有一个部分,但我只是配置了这一章来解决我的文件,我猜这是最易读的配置...

我在下面的 xquery 查询中尝试过:

1
2
3
4
5
6
<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>

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

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

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

1
2
3
4
5
6
7
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>

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

所以我需要你的建议来正确获得这些结构。先感谢您。我希望我被清楚地告诉你们所有人的情况。假定为"assemble.xml"的文件名。


1
2
3
4
5
6
7
8
9
10
11
12
<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>

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