2016-02-01 2 views
1

У нас есть структуры, как это:Как вернуть дополнительный узел в зависимости от текущего атрибута узла

<class> 
    <class-intro> 
    <indication>Some content</indication> 
    </class-intro> 

    <article> 
    <indication>Special content</indication> 
    </article> 

    <article includeclass="no"> 
    <indication>Different content</indication> 
    </article> 
</class> 

Я пытаюсь выбрать их с XQuery/XPath на основе статьи в:

indication | node()[not(@includeclass) | @includeclass='yes']/ancestor::class/class-intro/indication 

Примечание - Я использую РНР http://php.net/manual/en/class.domxpath.php

// $xpath is a DOMXPath for the above document 
$articles = $xpath->query("//article"); 

$indications = array(); 
foreach ($articles as $article) { 
    $indications[] = $xpath->query(
    "indication | node()[not(@includeclass) | @includeclass='yes']/ancestor::class/class-intro/indication", 
    $article 
); 
} 

var_dump($indications); 

Я ожидал получить:

array(
    0 => array(
    0 => "Some content", 
    1 => "Special content", 
), 
    1 => array(
    0 => "Different content", 
), 
); 

Но я получаю:

array(
    0 => array(
    0 => "Some content", 
    1 => "Special content", 
), 
    1 => array(
    0 => "Some content", 
    1 => "Different content", 
), 
); 
+0

'$ статьи = $ не xpath-> запрос ("// статья [@ includeclass = 'нет']/индикация"); ' – splash58

ответ

1

Проблема заключалась в том, потому что not(@includeclass) всегда имеет значение true для каждого node() в этом контексте, поскольку ни один из дочернего элемента article не приписывать includeclass.

Вы должны уже использовали self ось ссылок текущего контекста узла т.е. использовать self::node() вместо node(), потому что includeclass атрибут принадлежит текущий контекст элемента article, а не к дочернему узлу:

self::node()[not(@includeclass) or @includeclass='yes']/ancestor::class/..... 
Смежные вопросы