我们有这样的结构:
<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
注意 - 我使用的是PHP 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 :(得分:1)
问题是因为not(@includeclass)
对于此上下文中的每个true
始终评估为node()
,因为article
的子元素都没有属性includeclass
您应该使用self
轴来引用当前上下文节点,即使用self::node()
而不是node()
,因为includeclass
属性属于当前上下文元素{{ 1}},而不是子节点:
article