我有一个关于使用XPath获取属性值的问题。我知道使用@
可以获取属性名称 - 值对,但是如果我只想获取值呢?
我举个例子: Fr这个链接:http://www.campbellskitchen.com/?pd=yes&sekw=10941064280
如果我使用Chrome并使用$x("//a[contains(.,'BRAND')]/following-sibling::div//a/@title")
它将返回一系列类似的内容:
[title="Campbell's Condensed soup", title="Campbell's Gravies", title="Swanson Broth and Stock" ...]
这不是我想要的,我希望返回数组为:
[Campbell's Condensed soup, Campbell's Gravies, Swanson Broth and Stock ...]
<li role="menuitem" class="parent"><a href="/Brands?fm=link_navigated" title="BRANDS"
role="link" tabindex="10" accesskey="b">
BRANDS</a>
<div class="subnavi dropdown_5columns">
<!-- Begin 5 columns container -->
<div class="ddHeader">
<div class="leftCorner">
<div class="rightCorner">
</div>
</div>
</div>
<div class="leftShadow">
<div class="rightShadow">
<div class="ddContent clearfix">
<div class="col_1_logo">
<a href="/WisestKid/Home?fm=link_navigated" title="Campbell’s® Condensed soup">
<img width="65" height="27" alt="Campbell’s® Condensed soup" src="~/media/OurBrands/Logos/campbells.ashx?mh=38&mw=65"></a>
</div>
<div class="col_1_logo">
<a id="plhheader_0_rptBrands_lnkBrand_0" title="Campbell's® Gravies" href="/Brands/Campbells%20Gravies?fm=link_navigated"><img src="/~/media/OurBrands/Logos/campbells_gravy.ashx?h=38&mh=38&mw=65&w=57" alt="Campbells Gravy" width="57" height="38" /></a>
</div>
<div class="col_1_logo">
<a id="plhheader_0_rptBrands_lnkBrand_1" title="Swanson® Broth and Stock" href="http://www.campbellskitchen.com/Swanson/Home.aspx?fm=theater"><img src="/~/media/OurBrands/Logos/SwansonFB_llogo.ashx?h=36&mh=38&mw=65&w=65" alt="Swanson" width="65" height="36" /></a>
</div>
<div class="col_1_logo">
<a id="plhheader_0_rptBrands_lnkBrand_2" title="Pace® Sauces" href="/Brands/Pace%20Sauces?fm=link_navigated"><img src="/~/media/OurBrands/Logos/pace.ashx?h=38&mh=38&mw=65&w=60" alt="Pace" width="60" height="38" /></a>
</div>
<div class="col_1_logo">
<a id="plhheader_0_rptBrands_lnkBrand_3" title="Prego® Sauce" href="/Brands/Prego%20Sauce?fm=link_navigated"><img src="/~/media/OurBrands/Logos/prego.ashx?h=25&mh=38&mw=65&w=65" alt="Prego" width="65" height="25" /></a>
</div>
<div class="col_1_logo">
<a id="plhheader_0_rptBrands_lnkBrand_4" title="Pepperidge Farm®" href="/Brands/Pepperidge%20Farm?fm=link_navigated"><img src="/~/media/OurBrands/Logos/pepperidge_farm.ashx?h=22&mh=38&mw=65&w=65" alt="Pepperidge Farms" width="65" height="22" /></a>
</div>
<div class="col_1_logo">
<a id="plhheader_0_rptBrands_lnkBrand_5" title="Soup Plus" href="/Brands/Soup%20Plus?fm=link_navigated"></a>
</div>
<div class="col_3">
<a href="/Brands?fm=link_navigated" title="See All Our Brands">
See All Our Brands</a>
</div>
</div>
<!-- ddContent -->
</div>
</div>
<div class="ddFooter">
<div class="leftCorner">
<div class="rightCorner">
</div>
</div>
</div>
</div>
<!-- End 5 columns container -->
</li>
答案 0 :(得分:0)
您可以使用a[@title]/text()
获取文本节点。
表示:所有文本节点都是锚点/链接的子节点,具有属性&#34;标题&#34;而a/@title/text()
表示:找到所有锚点/链接,然后查找属性子节点,然后选择这些属性节点的文本节点children 。由于属性没有任何子节点,因此结果为空集。
正如评论中所提到的,BRANDS链接中有图像而不是文本节点。请考虑检索alt
代码的<img>
属性。
在讨论和编辑后,我理解:你确实想要标题attrubutes并将其转换为值数组。这是GreaseMonkey的一个例子
console.log
( GM_xpath({ path: "//a[contains(.,'BRAND')]/following-sibling::div//a/@title", all:true })
.map(function(item){ return item.value;})
);
如果从函数中得到的NodeList
不是数组,则可以使用map()
,因为它是可迭代列表的方法:Array.prototype.map.call(list, function(){})
答案 1 :(得分:0)
$x
返回选定的DOM节点,如果你想从这些节点中提取数据,你需要在JavaScript中提取第二步,例如。
$x(someXpath).map(function(n) { return n.nodeValue; });