In Perl using Mojo :: DOM in XML mode, how can I combine subtitle content?

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>

(This is ONIX , if you're interested.)

I want to extract the type 01 header. I tried:

say $dom->at('TitleDetail[TitleType="01"] > TitleElement > TitleText')

but that will not work. It seems that the syntax tag[attr=value]really only works for attributes.

Is there an easy way to do what I want?

+4
source share
2 answers

This can be done using Mojo :: DOM, but it takes a long time. There are Mojo :: Collections here a few times, so you need to get the first element.

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;
+5
source

Perhaps my problem is that I use a hammer to insert a screw ...

Mojo:: DOM , , , XML, XML:: XPath:

my $xp = XML::XPath->new(xml=>$xml);
say $xp->findvalue('//TitleDetail[TitleType=01]/TitleElement/TitleText');

, .

+5

Source: https://habr.com/ru/post/1669306/


All Articles