XQuery嵌套for循环

3

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>

我希望使用XQuery语言,但我对这种查询语言非常陌生。

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

章节在此xml文件中如下所示:

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

在这个例子中,Sicherheits- und Warnhinweise 是章节元素的标题。章节可以有许多小节,在我们的例子中,小节的标题是:
       <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> 
...

在启动设备之前。

预期的结构:

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

这个例子只有一个章节,但我刚刚配置了这个章节以找到我的文件解决方案,我认为这是最易读的配置...

我尝试了以下的xquery查询:

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

使用两个嵌套的循环,但这段代码使我的结构看起来像:

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

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

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
我在你的查询中没有看到任何尝试按不同值分组的内容。你能更清楚地说明你想要什么吗? - Charles Duffy
1
一般情况下,您需要使用XQuery 3.0的“group by”表达式,或者如果您正在使用较早版本的语言,则需要使用“distinct-values()”过滤器。 - Charles Duffy
1
但是目前,我真的不明白你期望什么。拥有一个难以阅读的巨大文档也没有帮助——如果您能够使用3-4行XML文件和相应缩短的代码重现问题,那将是有帮助的,并符合http://stackoverflow.com/help/mcve的要求。 - Charles Duffy
顺便说一下,http://docs.basex.org/wiki/XQuery_3.0#Group_By 是 XQuery 3.0 语法的好参考资料,并且StackOverflow上已经有很多关于旧版本语言使用distinct-values()模式的问题和答案,其中一个是http://stackoverflow.com/questions/23745868/grouping-in-xquery - Charles Duffy
抱歉在编辑帖子后解释错误,我快要下班了,需要赶去接班车,所以忘记编辑主题和我的期望。 - Hayra
1个回答

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

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


我在这段代码中没有看到任何分组操作(无论是使用直接的XQuery 3.0语法还是任何有效的解决方法来弥补之前版本中缺少的功能),这让我更加好奇你的问题实际上是什么意思。 - Charles Duffy
让我这样解释,首先我认为我可以通过它们的ID对数据进行分组,但后来我意识到这对我来说并不需要,所以我编辑了我的问题,然后解释了我实际需要的东西。我的第一种方法是错误的,如果你仔细阅读我的编辑,你会发现我删除了关于分组的行。我提到了如何同时控制外循环和内循环... - Hayra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接