2

Ciao,

I'm working with XPath and I need to retrieve all elements by specific conditions.

Below one example of my XML document:

<?xml version="1.0" encoding="UTF-8"?>
<persone>
             
    <persona>
        <nome>Mario</nome>
        <cognome>Rossi</cognome>
    </persona>

    <persona>
        <nome>Mario</nome>
        <cognome>Bianchi</cognome>
    </persona>

    <persona>
        <nome>Marco</nome>
        <cognome>Verdi</cognome>
    </persona>
             
</persone>

I need to create an XPath query that return all elements with these two conditions true:

  • The element should contain one element with content equals to "Mario"
  • The element should contain one element with content equals to "Bianchi"

How can I achieve this goal?

1 Answer 1

1

I've resolved my problem using a query like this:

//persona[nome[text()='Mario'] and cognome[text()='Bianchi']]
Sign up to request clarification or add additional context in comments.

2 Comments

Shorter: //persona[nome = 'Mario' and cognome = 'Bianchi']
Also note that this approach will fail for names like D'Angelo. When you need to search for strings that can contain single- or double quotes in XPath, things get a little bit more complicated.

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.