XPATH 的第一个元素
First Element of XPATH
这个问题看起来和 : XPath Get first element of subset 一样,但我认为有点不同。
这是以下博客:
http://www.mademoiselledeco.com/
我想得到每个post的第一张照片。为此,我想到了以下 xpath 查询:
//div[contains(@class,'type-post status-publish')]//img/@src
按照我之前提到的post的例子,我也尝试了:
//div[contains(@class,'type-post status-publish')](//img/@src)[1]
但这就是说
Warning: DOMXPath::query(): Invalid expression
有什么想法吗?
非常感谢
OK,我明白了,查了源码:每个<img>
都包含在一个<p>
中,所以img[1]
会匹配所有的图片,因为它们是,在上下文中段落的第一张图片。
在这种情况下,我宁愿尝试获取包含图像的第一段:
//div[contains(@class,'type-post status-publish')]//p[img][1]/img/@src
用这个 XPath 我得到 9 img/@src
。
//div[@class='post-content-container']//p[./img][1]/img
这不是最好的解决方案,但我认为它会起作用。
//div[@class='post-content-container']
应该得到每个post
//p[./img][1]/img
应该获取第一段,其中包含一张图片。然后选择图像。
实际上,您选择的重复问题并不遥远。它在 one of it's answers 中有一个解释,听起来很合理:
The []
operator has a higher precedence (binds stronger) than the //
abbreviation.
所以 //img
缩写挡住了你的路。让我们展开它:
/descendant-or-self::node()/child::img
在末尾添加 [1]
将 select 每个第一个 img child(这与其他人概述的完全一样)。这也是这里谓词优先级较高的原因。
Abbreviated Syntax section in Xpath 1.0 实际上用注释涵盖了这一点:
NOTE: The location path //para[1]
does not mean the same as the location path /descendant::para[1]
. The latter selects the first descendant para element; the former selects all descendant para
elements that are the first para
children of their parents.
也就是说:您不是在寻找 descendant-or-self
轴和其中的任何节点 children,而是在寻找 descendant
中的第一个 img
元素轴:
/descendant::img[1]
所以完整的 xpath 表达式:
//div[contains(@class,'type-post status-publish')]/descendant::img[1]/@src
您的示例结果 (10):
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/03/Couleur-FionaLynch-Caroline-St.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/02/2-OF-MO-cascade-lumineuse2-1024x398.jpg"
src="https://s-media-cache-ak0.pinimg.com/736x/2e/f7/eb/2ef7eb28dc3e6ac9830cf0f1be7defce.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/01/couleur-peinture-flamant-vert-trekking.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/01/Lily-of-the-Valley-Designed-by-Marie-Deroudilhe-02.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/01/shopping-decoration-jaune-bleu-delamaison-1024x866.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/01/wikao-cheminee-berlin-mademoiselledeco4.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/01/voeux2015-mademoiselledeco-blog.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2014/12/suite-novotel-constance-guisset-1.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2014/12/wish-list-decoration-noel-2014.jpg"
我希望这能有所启发。
这个问题看起来和 : XPath Get first element of subset 一样,但我认为有点不同。
这是以下博客: http://www.mademoiselledeco.com/
我想得到每个post的第一张照片。为此,我想到了以下 xpath 查询:
//div[contains(@class,'type-post status-publish')]//img/@src
按照我之前提到的post的例子,我也尝试了:
//div[contains(@class,'type-post status-publish')](//img/@src)[1]
但这就是说
Warning: DOMXPath::query(): Invalid expression
有什么想法吗?
非常感谢
OK,我明白了,查了源码:每个<img>
都包含在一个<p>
中,所以img[1]
会匹配所有的图片,因为它们是,在上下文中段落的第一张图片。
在这种情况下,我宁愿尝试获取包含图像的第一段:
//div[contains(@class,'type-post status-publish')]//p[img][1]/img/@src
用这个 XPath 我得到 9 img/@src
。
//div[@class='post-content-container']//p[./img][1]/img
这不是最好的解决方案,但我认为它会起作用。
//div[@class='post-content-container']
应该得到每个post
//p[./img][1]/img
应该获取第一段,其中包含一张图片。然后选择图像。
实际上,您选择的重复问题并不遥远。它在 one of it's answers 中有一个解释,听起来很合理:
The
[]
operator has a higher precedence (binds stronger) than the//
abbreviation.
所以 //img
缩写挡住了你的路。让我们展开它:
/descendant-or-self::node()/child::img
在末尾添加 [1]
将 select 每个第一个 img child(这与其他人概述的完全一样)。这也是这里谓词优先级较高的原因。
Abbreviated Syntax section in Xpath 1.0 实际上用注释涵盖了这一点:
NOTE: The location path
//para[1]
does not mean the same as the location path/descendant::para[1]
. The latter selects the first descendant para element; the former selects all descendantpara
elements that are the firstpara
children of their parents.
也就是说:您不是在寻找 descendant-or-self
轴和其中的任何节点 children,而是在寻找 descendant
中的第一个 img
元素轴:
/descendant::img[1]
所以完整的 xpath 表达式:
//div[contains(@class,'type-post status-publish')]/descendant::img[1]/@src
您的示例结果 (10):
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/03/Couleur-FionaLynch-Caroline-St.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/02/2-OF-MO-cascade-lumineuse2-1024x398.jpg"
src="https://s-media-cache-ak0.pinimg.com/736x/2e/f7/eb/2ef7eb28dc3e6ac9830cf0f1be7defce.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/01/couleur-peinture-flamant-vert-trekking.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/01/Lily-of-the-Valley-Designed-by-Marie-Deroudilhe-02.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/01/shopping-decoration-jaune-bleu-delamaison-1024x866.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/01/wikao-cheminee-berlin-mademoiselledeco4.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/01/voeux2015-mademoiselledeco-blog.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2014/12/suite-novotel-constance-guisset-1.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2014/12/wish-list-decoration-noel-2014.jpg"
我希望这能有所启发。