XPath group selected node with next sibling

主宰稳场 提交于 2021-01-29 16:50:58

问题


I use "net.sf.saxon" % "Saxon-HE" % "9.9.1-6" and i'm trying to group some nodes of a HTML Site with XPath.

HTML Page

    <div class="item-list">
        <div class="row-title"><h3>Item 1</h3></div>
        <div class="row-body"><div>nested stuff for Item 1</div></div>
    
        <div class="row-title"><h3>Item 2</h3></div>
        <div class="row-body"><div>nested stuff for Item 2</div></div>
    
        <div class="row-title"><h3>Item 3</h3></div>
        <div class="row-body"><div>nested stuff for Item 3</div></div>
    </div>

Expected result (array)

 1. <div class="row-title"><h3>Item 1</h3></div><div class="row-body"><div>nested stuff for Item 1</div></div>
 2. <div class="row-title"><h3>Item 2</h3></div><div class="row-body"><div>nested stuff for Item 2</div></div>
 3. <div class="row-title"><h3>Item 3</h3></div><div class="row-body"><div>nested stuff for Item 3</div></div>

This is one of my tries/attempts.

//div[contains(@class, 'item-list')]/div[contains(@class, 'row-body') or contains(@class, 'row-title')]

But the result (see below) ist not as expected. (I need the results e.g. 1. and 2. together.)

1. <div class="row-title"><h3>Item 1</h3></div>
2. <div class="row-body"><div>nested stuff for Item 1</div></div>
3. <div class="row-title"><h3>Item 2</h3></div>
4. <div class="row-body"><div>nested stuff for Item 2</div></div>
5. <div class="row-title"><h3>Item 3</h3></div>
6. <div class="row-body"><div>nested stuff for Item 3</div></div>

I also tried something with "following-sibling", but unfortunately without success.

来源:https://stackoverflow.com/questions/62436083/xpath-group-selected-node-with-next-sibling

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!