Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
How to select all elements in Xml document with specific inner fields

For example I have xml:

<Cars>
    <Car>
        <Name>BMW</Name>
        <Color>Blue</Color>
    </Car>
    <Car>
        <Name>Mercedes</Name>
        <Color>Yellow</Color>
    </Car>
    <Car>
        <Name>Mercedes</Name>
        <Color>Blue</Color>
    </Car>
</Cars>

And I need to choose all cars with color Blue. Is there a way I can do it efficiently with selectors or/and loops?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The :contains() selector and parent function of jQuery help for this goal.

The :contains() selector, selects all elements that contain the specified text. More information: https://api.jquery.com/contains-selector/#contains1

The .parent() method, gets the parent of each element in the current set of matched elements, optionally filtered by a selector. More information: https://api.jquery.com/parent/#parent-selector

In the following code objs contains two elements:

var objs = $('Car>Color:contains("Blue")').parent()
console.log(objs.eq(0).html())
console.log(objs.eq(1).html())
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<Cars>
    <Car>
        <Name>BMW</Name>
        <Color>Blue</Color>
    </Car>
    <Car>
        <Name>Mercedes</Name>
        <Color>Yellow</Color>
    </Car>
    <Car>
        <Name>Mercedes</Name>
        <Color>Blue</Color>
    </Car>
</Cars>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!