Link to home
Start Free TrialLog in
Avatar of Eslovo
EslovoFlag for Russian Federation

asked on

Xpath invalid expression that WORK!

Hi, My code works and correctly processed by XSLT, but I keep getting warrning "SimpleXMLElement::xpath(): Invalid expression in" on this xpath expression

<document>
    <child>
        <flag>1</flag>
        <data>First Item is to be Ignored</data>   
    </child>
    <child>
        <flag>2</flag>
        <data>Second Item Data</data>   
    </child>
</document>

$flagValue =2;

foreach ($sxml->xpath('/document/child[flag='.$flagValue.']/data') as $someData) {

}

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

Maybe try exchanging one set of single quotes for double?

e.g.

foreach ($sxml->xpath('/document/child[flag=".$flagValue."]/data') as $someData) {

Open in new window

Greetings, Eslovo.  Here are some general guidelines to make your use of this site more enjoyable and productive for you...

At EE, the experts exchange answers and advice for points.  If you look at the questions awaiting answers in this zone, you will see a lot of 500 point questions.  Your question is competing for the experts' attention among those high-point questions.  So as a matter of simple economics you might be able to envision which questions will get the experts' attention first.   Just a thought.

We are experts, but not mind readers.  Inquiries that are vague may not get answers that are as succinct and effective as inquiries that have actual URLs, complete code examples, and clearly expressed questions.  Whenever possible, please provide the inputs and tell us what you want for the outputs.  An incredibly important concept is the SSCCE; please read the online page and embrace the SSCCE strategy.  If you do not have the SSCCE, stop what you're doing and create one, and post it with your question.  In the instant case, it looks like there is a mixup between the XML document and the PHP code.  Probably your setup doesn't really include that mixup, and we would be better able to help if you showed us the actual test data and the actual code.

Best regards, ~Ray
Avatar of Eslovo

ASKER

@kaufmed: Maybe try exchanging one set of single quotes for double?
$sxml->xpath("/document/child[flag=".$flagValue."]/data")

Open in new window

Double quotas did not help.. Still get "Warning: SimpleXMLElement::xpath(): Invalid expression in"
Here is how I might do it: http://iconoun.com/demo/temp_eslovo.php

<?php // demo/temp_eslovo.php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28490788.html

// A SIMULATED XML DOCUMENT FROM THE POST AT EE
$xml = <<<EOD
<document>
    <child>
        <flag>1</flag>
        <data>First Item is to be Ignored</data>
    </child>
    <child>
        <flag>2</flag>
        <data>Second Item Data</data>
    </child>
</document>
EOD;

// THE SEARCH VALUE
$flagvalue = 2;

// MAKE AN OBJECT FROM THE XML DOCUMENT
$obj = SimpleXML_Load_String($xml);

// SEARCH THE OBJECT AND SHOW THE FINDINGS, IF ANY
foreach ($obj->child as $child)
{
    if ($child->flag != $flagvalue) continue;
    var_dump($child);
}

Open in new window

HTH, ~Ray
Avatar of Eslovo

ASKER

@Ray Paseur It is not exactly what I was asking. Xpath is very powerful, but for some reason libxml on my laptop  with Fedora generates those annoying error messages. Xdebug just goes insane. Your solution simply puts xpath out of code.

I would like to keep xpath and be rid of errors. Though they do not appear in production environment. There're just no errors allowed to be desplayed. But web server log file is filled with this crap.

I wonder, may be I should remove php-topic and add linux instead? 'Cos the problem probably have something to do with installed libxml library...

libxml2-2.9.1-2.fc20.i686
libxml++-2.37.1-1.fc20.i686
libxml2-python-2.9.1-2.fc20.i686
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial