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

EslovoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
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

0
Ray PaseurCommented:
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
0
EslovoAuthor Commented:
@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"
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Ray PaseurCommented:
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
0
EslovoAuthor Commented:
@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
0
Ray PaseurCommented:
I think you've got two issues in play here.  First, there are no errors displayed on the production server because the server is suppressing the display of errors.  The errors are still happening, but PHP is not telling you about them.  That's why you don't see the messages, but the log still fills up.

Second, the error messages are generated because the PHP code is incorrect.  I found lots of incorrect ways to do it when I was testing, but this version worked correctly.  If you correct the PHP, you can get the data without the error messages.  When you install this and run it, use "view source" to see what it returns.

<?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
// REF http://www.php.net/manual/en/simplexmlelement.xpath.php

// A SIMULATED XML DOCUMENT FROM THE POST AT EE
$str = <<<XML
<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>
XML;

$obj = new SimpleXMLElement($str);
$flagValue =2;

$path = "/document/child[flag=$flagValue]/data";
foreach ($obj->xpath($path) as $someData)
{
    echo $someData->asXML();
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.