Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony DomCrawler with XML and XPath

I am trying to get all the title elements and save them in an array.

XML:

<?xml version="1.0" encoding="UTF-8"?>
<mylist>
    <element>
        <id>1</id>
        <title>Example 1</title>
        <status>2</status>
        <my_status>2</my_status>
    </element>
    <element>
        <id>2</id>
        <title>Example 2</title>
        <status>1</status>
        <my_status>1</my_status>
    </element>
    <element>
        <id>3</id>
        <title>Example 3</title>
        <status>2</status>
        <my_status>6</my_status>
    </element>
    <element>
        <id>4</id>
        <title>Example 4</title>
        <status>3</status>
        <my_status>6</my_status>
    </element>
    <element>
        <id>5</id>
        <title>Example 5</title>
        <status>1</status>
        <my_status>6</my_status>
    </element>
</mylist>

PHP:

$crawler = new Crawler();
$crawler->addXmlContent($data);

$result = $crawler->filterXPath('/mylist/element[not(status=3) and my_status=6]/title/text()');

The elements nodes needs to satisfy some conditions, so calling $result->count() should print 2 (Example 3 & Example 5), but it prints 0.

like image 469
allnex Avatar asked Jan 19 '26 19:01

allnex


1 Answers

The XPath should be:

$result = $crawler->filterXPath('//mylist/element[not(status=3) and my_status=6]/title/text()');

as per OP @allnex's prior edit to the question.

like image 180
vinzee Avatar answered Jan 22 '26 07:01

vinzee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!