我有具有以下結構的HTML頁面副標題解析HTML頁面:與使用XQuery
<div id="content">
<h2><span class="heading">Section A</span></h2>
<p>Content of the section</p>
<p>More content in the same section</p>
<div>We can also have divs</div>
<ul><li>And</li><li>Lists</li><li>Too</li></ul>
<h3><span class="heading">Sub-section heading</span></h3>
<p>The content here can be a mixture of divs, ps, lists, etc too</p>
<h2><span class="heading">Section B</span></h2>
<p>This is section B's content</p>
and so on
</div>
我想創建以下XML結構:
<sections>
<section>
<heading>Section A</heading>
<content>
<p>Content of the section</p>
<p>More content in the same section</p>
<div>We can also have divs</div>
<ul><li>And</li><li>Lists</li><li>Too</li></ul>
</content>
<sub-sections>
<section>
<heading>Section B</heading>
<content>
<p>This is section B's content</p>
</content>
</section>
</sub-sections>
</section>
</sections>
困難我正在創建<sub-section>
標籤。這是我迄今爲止的,但B節出現在A節的<content>
節點內。我還爲B節獲得了<section>
節點,但它沒有內容。
let $content := //div[@id="content"]
let $headings := $content/(h2|h3|h4|h5|h6)[span[@class="heading"]]
return
<sections>
{
for $heading in $headings
return
<section>
<heading>{$heading/span/text()}</heading>
<content>
{
for $paragraph in $heading/following-sibling::*[preceding-sibling::h2[1] = $heading]
return
$paragraph
}
</content>
</section>
}
</sections>
在此先感謝您的任何幫助或指針。
非常感謝,這使我走上了正確的道路。 – Stu