如何根据当前节点属性 return 添加节点
How to return additional node depending on current node attribute
我们有这样的结构:
<class>
<class-intro>
<indication>Some content</indication>
</class-intro>
<article>
<indication>Special content</indication>
</article>
<article includeclass="no">
<indication>Different content</indication>
</article>
</class>
我正在尝试 select 这些 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",
),
);
问题是因为在此上下文中,对于每个 node()
,not(@includeclass)
始终计算为 true
,因为 article
的子元素的 none 具有属性 includeclass
.
您应该使用 self
轴来引用当前上下文节点,即使用 self::node()
而不是 node()
,因为 includeclass
属性属于当前上下文元素 article
, 不是到子节点:
self::node()[not(@includeclass) or @includeclass='yes']/ancestor::class/.....
我们有这样的结构:
<class>
<class-intro>
<indication>Some content</indication>
</class-intro>
<article>
<indication>Special content</indication>
</article>
<article includeclass="no">
<indication>Different content</indication>
</article>
</class>
我正在尝试 select 这些 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",
),
);
问题是因为在此上下文中,对于每个 node()
,not(@includeclass)
始终计算为 true
,因为 article
的子元素的 none 具有属性 includeclass
.
您应该使用 self
轴来引用当前上下文节点,即使用 self::node()
而不是 node()
,因为 includeclass
属性属于当前上下文元素 article
, 不是到子节点:
self::node()[not(@includeclass) or @includeclass='yes']/ancestor::class/.....