'xml_grep get value of first instace

i use xml_grep to export text from xml documents.

So i have an xml document like this:

<?xml version="1.0" encoding="UTF-8"?>
<Mediainfo version="0.7.52">
<File>
<track type="General">
<Complete_name>grm.mov</Complete_name>
<Format>MPEG-4</Format>
<Format_profile>QuickTime</Format_profile>
<Codec_ID>qt  </Codec_ID>
<File_size>10.1 MiB</File_size>
<Duration>3s 600ms</Duration>
<Overall_bit_rate_mode>Variable</Overall_bit_rate_mode>
<Overall_bit_rate>23.5 Mbps</Overall_bit_rate>
<Encoded_date>UTC 2012-08-01 09:33:54</Encoded_date>
<Tagged_date>UTC 2012-08-01 09:34:00</Tagged_date>
<Writing_library>Apple QuickTime</Writing_library>
<TIM>00:00:00:00</TIM>
<TSC>25</TSC>
<TSZ>1</TSZ>
</track>

<track type="Video">
<ID>1</ID>
<Format>JPEG</Format>
<Codec_ID>mjpa</Codec_ID>
<Duration>3s 600ms</Duration>
<Bit_rate_mode>Variable</Bit_rate_mode>
<Bit_rate>23.5 Mbps</Bit_rate>
<Width>1 920 pixels</Width>
<Height>1 080 pixels</Height>
<Display_aspect_ratio>16:9</Display_aspect_ratio>
<Frame_rate_mode>Constant</Frame_rate_mode>
<Frame_rate>25.000 fps</Frame_rate>
<Scan_type>Interlaced</Scan_type>
<Scan_order>Bottom Field First</Scan_order>
<Compression_mode>Lossy</Compression_mode>
<Bits__Pixel_Frame_>0.453</Bits__Pixel_Frame_>
<Stream_size>10.1 MiB (100%)</Stream_size>
<Language>English</Language>
<Encoded_date>UTC 2012-08-01 09:33:54</Encoded_date>
<Tagged_date>UTC 2012-08-01 09:34:00</Tagged_date>
</track>

<track type="Menu">
<ID>2</ID>
<Language>English</Language>
<Encoded_date>UTC 2012-08-01 09:34:00</Encoded_date>
<Tagged_date>UTC 2012-08-01 09:34:00</Tagged_date>
</track>

</File>
</Mediainfo>

There are 2 elements named 'Duration'. One under <track type="General"> and the other under <track type="Video">. How can i export each one separately ?

xml_grep 'Duration'

returns both elements. I know this is a noobish question but i couldn't figure it out.

Thank you in advance.



Solution 1:[1]

you schould rather use xpath

like

$value1 = $xml->xpath('/file/track/@Video/Duration');

$value2 = $xml->xpath('/file/track/@General/Duration');

just an idea.

or soemthing like this:

xml_grep -t '*[@attrib="pickme"]' *.xml or xml_grep2 -t '//*[@attrib="pickme"]' *.xml

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Barta Tamás