[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Xpath invalid expression that WORK!

Posted on 2014-08-05
6
Medium Priority
?
536 Views
Last Modified: 2014-08-13
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

0
Comment
Question by:Eslovo
  • 3
  • 2
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40241098
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40241173
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
 

Author Comment

by:Eslovo
ID: 40241216
@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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40241241
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
 

Author Comment

by:Eslovo
ID: 40241747
@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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 40241806
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 16 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question