2

I am really new to xpath and was hoping that I could get some guidance. I have the following XML:

<dd class="conten1">
    <li class="">
      <b> Some text here </b>
      <b> another text here </b>
      <span class="date-range">2014 – Present</span>
    </li>
</dd>

I have run the following xpath:

.//dd[contains(@class, 'conten1')]/li

and it returns:

Some text here another text here2014 - Present

How do I remove the "span" portion and only get

Some text here another text here

I have run

substring-before(.//dd[contains(@class, 'conten1')]/li,'201') 

but this is not ideal as some of the entrees do not contain that span portion thus, would not be retrieved at all.

What I wish to grab is only the "Some text here another text here" portion consistently.

Really hope for some help and thank you in advance.

3
  • Try /text() on the end of your path... Commented Jul 8, 2016 at 19:39
  • Thank you for this and it helps a lot. What if the text comes in the following format <b> text 1 </b> <b> text 2 </b> <span class = ....... For do I select all of the b Commented Jul 8, 2016 at 19:52
  • 1
    What is your criterion for knowing what to grab? Do you want all the text from <b> elements only? All the text from non-<span> elements? The XPath expression 'Some text here another text here' will give you what you asked for, but I assume that's not what you want. Commented Jul 8, 2016 at 21:51

1 Answer 1

2

If you need only the b nodes, then XPath may look like

//dd[contains(@class, 'conten1')]/li/b

If you need all nodes except the span, then the XPath may look like

//dd[contains(@class, 'conten1')]/li/*[name(.)!='span']
Sign up to request clarification or add additional context in comments.

1 Comment

+1. I would replace the *[name(.)!='span'] at the end with *[not(self::span)], which may reduce string comparisons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.