简体   繁体   English

需要在xpath中使用php变量的帮助

[英]Need some help using a php variable in xpath

Thanks to your site and its helpful members I am almost completely finished with this project apart from one little problem . 感谢您的网站及其乐于助人的成员,除了一个小问题,我几乎已经完成了该项目。

Background .. 背景 ..

I have a form that enters data to an xml file and a page that displays the information based on 2 pieces of criteria it needs to match today's date and have the status out . 我有一个将数据输入xml文件的表单,以及一个根据2条符合今天的日期和状态为out的条件显示信息的页面。

Problem is the code I am using gives me the data when it matches only status and not both I need it to match both before showing the data . 问题是我使用的代码仅在与状态匹配时才为我提供数据,而在显示数据之前我并不需要两者都将其匹配。

XML Structure .... XML结构....

<information>
<entry>
<date></date>
<name></name>
<status></status>
<notes></notes>
<comments></comments>
</entry>
</information>

Code For Retrieving And Displaying Data ... 检索和显示数据的代码...

$lib  = simplexml_load_file("sample.xml");
$today = date('m/d/y');
$query = $lib->xpath("//entry[.//date[contains(., '   $today')]]|//entry[.//status[contains(., 'out')]]");
if( $query ) {
foreach($query as $node){
      echo "<div id='one'>$node->name</div>
<div id='two'>$node->notes</div> 
<div id='three'><div class='front'>$node->comments</div></div>";

}
}
else {
// Echo the special div.
}

So as you can see from the code I have it querying whether or not it contains today's date via the $today function (which is not working ) and querying whether or not it contains out in status but as of yet I can not get it to work to match both it only matches status. 因此,如您从代码中看到的那样,我可以通过$ today函数查询它是否包含今天的日期(不起作用),并查询它是否包含状态,但是到目前为止,我无法获取它匹配两者的工作只匹配状态。

I apologise if this has been answered somewhere before and I thank you all in advance for any help given on this matter . 抱歉,如果您在以前的某个地方已经回答了该问题,我在此先感谢大家的帮助。 I also apologise if the question is too vague or I have not been forthcoming enough with details . 如果问题太模糊,或者细节不够充分,我也深表歉意。

the | | in your xpath-expression means or , try this instead: 在您的xpath-expression表示or ,请尝试以下方法:

... xpath("/information/entry[date = '01/23/2014' and status='out']")

It will work great without the contains() on 如果没有contains() ,它将很好用

<date>01/23/2014</date>
<status>out</status>

use this 用这个

... xpath("/information/entry[date[contains(., '01/23/2014')] and status[contains(., 'out')]]")

on this: 在此:

<date>foo01/23/2014</date>
<status>barout</status>

see it working: https://eval.in/93642 看到它工作了: https : //eval.in/93642

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM