skip to Main Content

Here is my example:

<div>
  <p><em>Emphasized text</em> followed by any <span>other text</span> and ending with an <em>emphasized text</em></p>
</div>

What I wan is to catch this paragraph which starts with em and ends with em. Between these tags may be text including other inline tags like span, img, a, etc.

A real world example can be some academic and newspaper writing rules when you have to emphasize a text inside an already emphasized text then you cancel the emphasis which is inside the emphasis, i.e. author presentation or bibliography:

If we have this phrase: — John Doe is the writer of "Emphasis inside emphasis" book. — One printing rule is to write it like this:

John Doe is the writer of Emphasis inside emphasis book.

But when you have to print this paragraph emphasized, it will become:

John Doe is the writer of Emphasis inside emphasis book.

Same thing with bibliography presentation:

John Doe, Emphasis inside emphasis, NY, Publisher Name, 2024.

Which emphasized becomes:

John Doe, Emphasis inside emphasis, NY, Publisher Name, 2024.

Consider that some of this text (author’s, name, publisher’s name or book’s name) may be anchored to a link.

This question is an extension of a previous one where we were seeking for just a p started and ended by an em. Some answers were provided covering simple or more complex cases dealing with p followed by one or more em and no other text between the tags. The difference here is that we have some text between the tags but the main purpose is the same: the paragraph starts and ends emphasized whatever may be in the middle.

2

Answers


  1. This xpath would get such paragraphs

    //*[./*[1][name()="em"] and ./*[last()][name()="em"]]
    

    same using child axis

    //*[child::*[1][name()="em"] and child::*[last()][name()="em"]]
    
    Login or Signup to reply.
  2. This XPath,

    //p[node()[1][self::em]][node()[last()][self::em]]
    

    will select all p elements whose children start with and end with em elements, as requested

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search