1

I would like to create a XPath expression that would return the title of the highest grossing movie, but I'm unable to do so.

I already looked at this and other similiar questions and while they were helpful I couldn't make any of those solutions work for my case. I tried to modify the XPath, but it returns all titles and I don't really what to do to fix it.

Here is my .xml file and below is my XPath expression

<movie_database>
  <movies>

    <movie>
      <title>Movie 1</title>
      <finance_records>
        <gross>56,000,000 $</gross>
      </finance_records>      
    </movie>
    
    <movie>
      <title>Movie 2</title>
      <finance_records>
        <gross>150,100,055 $</gross>
      </finance_records>       
    </movie>
    
    <movie>
      <title>Movie 3</title>
      <finance_records>
        <gross>100,577,000 $</gross>
      </finance_records>       
    </movie>
  </movies>
</movie_database>

./movie_database/movies/movie[finance_records[not(../finance_records/gross/number(translate(.," ,$","")) > gross/number(translate(.," ,$","")))]]/title

1 Answers1

0

For xpath 1.0 try:

//movie[not(..//movie//gross/number(translate(.," ,$","")) > .//gross/number(translate(.," ,$","")))]/title

With xpath > 1.0 try:

//movie[.//gross/number(translate(.," ,$","")) = max(..//movie//gross/number(translate(.," ,$","")))]/title

In either case, Movie 2 should be returned.

Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • @ObéznyPäťdesiatnik Glad it worked for you! – Jack Fleeting Apr 04 '21 at 11:19
  • 2
    Your XPath 1.0 expression would actually require XPath 2.0 due to its use of a function call in the final step of the expression. Also, this question has been asked many times in the past -- easy to find a duplicate for it. – kjhughes Apr 04 '21 at 14:15