English 中文(简体)
PHP的Xpath 父母选择
原标题:Xpath parent selector in PHP
  • 时间:2012-04-19 18:59:34
  •  标签:
  • php
  • xml
  • xpath

<>><>>背景范围:>>

,,

I m 为MVC的申请在PHP中设置一条路子,并决定该结构在XML中。 我有一份XML文件,其中载有该系统的所有有效路线(页)及其相关控制器和药房;行动。

还有一点要指出,在《世界投资倡议》结束时是否存在变数,以及分配给它的不同名称(我知道!)

我所做的事情是看RequestST_。 URI, 并利用PHP的探索功能,将其转化为一系列路线要素,然后我为这些要素建造了 que。

这里有一些XML样本:

<routes>    
    <route>
        <url>blog</url>
        <params>
            <controller>blogController</controller>
            <action>indexAction</action>
        </params>

        <route>
            <url>entry</url>
            <params>
                <controller>blogController</controller>
                <action>entryAction</action>
                <param>entryId</param>
            </params>
        </route>
    </route>
 </routes>

这里还有这样一个问题:

/routes/route/url[text()="blog"]/../route/url[text()="entry"]/..

这似乎总是在PHPs XPath退回0个点,但使用在线表达测试器,我获得了相应的入境路线。

谁能解释什么可能是错误的? 页: 1 PHP s Xpath parser了解这一辛奈。 我也尝试了 ::* 方法。

Cheers!

最佳回答

X智,我得出与DevNull一样的结论,只是略加补充选择第一种对应办法:

/routes/route[url="blog"]/route[url="entry"][1]

物体接口:

$routes = new RoutesXML($xml);

var_dump($routes->fromPath( blog/entry ));  # The SimpleXMLElement

var_dump($routes->fromPath( blog/entry2 )); # NULL

例 执行:

class RoutesXML
{
    private $xml;
    public function __construct($xml) {
        $this->xml = simplexml_load_string($xml);
    }
    public function fromPath($path) {
        $expression =  /routes ;
        foreach(explode( / , $path) as $element)
            $expression .= "/route[url= $element ]";
        $expression .=  [1] ;
        list($route) = $this->xml->xpath($expression) + array(NULL);
        return $route;
    }
}
问题回答

页: 1

相反:

/routes/route[url="blog"]/route[url="entry"]

您不需要<代码>文本(),但我也不知道购买力平价。 如果没有上述工作,则尝试:

/routes/route[url/text()="blog"]/route[url/text()="entry"]




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签