0

How would I go about unwrapping an element from its immediate parent if it is an only child. My version of JSoup is 1.6.3 and I cannot upgrade it.

I have tried using the :only-child selector but I don't think this is available in my version of JSoup.

e.g.

<p>
    <span>Some text</span>
</p>

should become...

<span>Some text</span>

But...

<div>
    <p>Some text</p>
    <p>Some more text</p>
</div>

should stay as it is.

Thanks, Michael.

Update:

Desired outcomes...

In the following situation I do want the span to be selected (i.e. where the child element is the only child of its parent):

<p><span>Text</span></p>

In the following situations I do not want the span to be selected (i.e. (1) where the child element is the only child node of its parent but the parent also contains content (2) where the parent element contains more than one child):

<p>Some text <span>continued</span></p>

<p><span>Text</span><a href="#">Link</a></p>

Once I have selected the correct child items I wish to unwrap them from their parent elements. This can be done using the unwrap() method. My main issue is selecting the child elements to unwrap in the first place.

4

1 回答 1

1

下面的代码找到一个Element只有一个孩子的,无论HTML它们是什么标签。

 Elements items = doc.select("body *");

    for (Element item : items) {

        if(item.parent() != null && item.parent().children().size() == 1 &&  StringUtil.isBlank(item.parent().ownText()))
        {
            System.out.println(item.parent().tagName() + " -> " + item.tagName() + " [" + item.ownText() + "]");
        }

    }
于 2013-07-26T14:09:08.093 回答