[英]XQuery Nested For Loop
我有一個示例xml文件,例如:
<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>
...
在該示例中,Sicherheitsind in Warnhinweise是Chapter元素的標題。 各章可以包含許多部分,在我們的示例中,該部分的標題為:
<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>
...
Betrieb nehmen的Bevor Sie das Gerat。
預期結構:
<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”的文件名。
<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>
我在互聯網上搜索,發現了如何進行內循環操作。 以下代碼非常適合我的期望。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.