I have the following XML:
<Product>
...
<TitleDetail>
<TitleType>01</TitleType>
<TitleElement>
<TitleElementLevel>01</TitleElementLevel>
<TitleText>This is the title I'm looking for</TitleText>
</TitleElement>
</TitleDetail>
...
</Product>
(It's ONIX, if you're curious.)
I want to extract the title, of type 01. I've tried:
say $dom->at('TitleDetail[TitleType="01"] > TitleElement > TitleText')
but that doesn't work. Looks like the tag[attr=value]
syntax really only works for attributes.
Is there a simple way to do what I want?
It can be done with Mojo::DOM, but it's long. A couple of times there are Mojo::Collections in there, so you need to get the first element out.
use Mojo::DOM;
my $dom = Mojo::DOM->new->xml(1)->parse($xml);
say $dom->find("TitleType")->grep(sub{ $_->text eq "01"})->first
->following("TitleElement")->first->at("TitleText")->text;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With