Blanket431
Blanket431

Reputation: 572

XPath to get element with maximum/minimum attribute value

For this XML,

<MediaFiles>
  <MediaFile id="" bitrate="670" type="video/mp4">
    http://example.com/test.mp4
  </MediaFile>
  <MediaFile id="" bitrate="1800" type="video/mp4">
    http://example.com/test2.mp4
  </MediaFile>
  <MediaFile id="" bitrate="600" type="video/x-flv">
    http://example.com/test3.mp4
  </MediaFile>
  <MediaFile id="" bitrate="450" type="video/x-flv">
    http://example.com/test4.mp4
  </MediaFile>
</MediaFiles>

My question is how to get Mediafile with the highest @bitrate (in this case I should get http://example.com/test2.mp4 as the @bitrate attribute is 1800 the highest).

For example, I'm able to get an array with the Mediafiles

/VAST/Ad/InLine/Creatives/Creative/Linear/MediaFiles/MediaFile

Upvotes: 1

Views: 1363

Answers (1)

kjhughes
kjhughes

Reputation: 111591

Here's how to use XPath to select the element with the maximum or minimum attribute values:

Maximum

//MediaFile[not(@bitrate < ../MediaFile/@bitrate)][1]

Minimum

//MediaFile[not(@bitrate > ../MediaFile/@bitrate)][1]

(Drop the trailing [1] if you want all MediaFile elements tied with a max/min @bitrate value.)

Upvotes: 2

Related Questions