XML路径列表-仅生成某些节点

时间:2020-05-22 20:24:44

标签: xml for-xml-path

我从此网页获得以下XML输入。

https://www.bike-king.com/product/specialized-mens-stumpjumper-st-comp-carbon-29-12-speed-344617-1.htm

<ol class="breadcrumb seProductBreadcrumb">
<li>
<a href="/" data-value="Home" data-ui="product-breadcrumbs">
<span>Home</span>
</a>
</li>
<li>
<a href="/catalog/bicycling-catalog-39/" data-value="Bicycling Catalog" data-ui="product-breadcrumbs">
<span>Bicycling Catalog</span>
</a>
</li>
<li>
<a href="/product-list/bikes-1000/" data-value="Bikes" data-ui="product-breadcrumbs">
<span>Bikes</span>
</a>
</li>
<li>
<a href="/product-list/bikes-1000/mountain-1006/" data-value="Mountain" data-ui="product-breadcrumbs">
<span>Mountain</span>
</a>
</li>
<li>
<a href="/product-list/bikes-1000/mountain-1006/29-inch-wheel-29ers-1009/" data-value="29-Inch Wheel (29ers)" data-ui="product-breadcrumbs">
<span>29-Inch Wheel (29ers)</span>
</a>
</li>
<li>
<a href="/product-list/bikes-1000/mountain-1006/29-inch-wheel-29ers-1009/?rb_br=349" data-value="Specialized" data-ui="product-breadcrumbs">
<span>Specialized</span>
</a>
</li>
<li class="hidden-xs active">
<span class="hidden-xs">
Men's Stumpjumper ST Comp Carbon 29 – 12-Speed
</span>
</li>
</ol>

我的XML路径

//ol[@class="breadcrumb seProductBreadcrumb"]/li/a

为我生成以下输出:

<a href="/" data:value="Home" data:ui="product:breadcrumbs">
<span>Home</span>
</a>
<a href="/catalog/bicycling:catalog:39/" data:value="Bicycling Catalog" data:ui="product:breadcrumbs">
<span>Bicycling Catalog</span>
</a>
<a href="/product:list/bikes:1000/" data:value="Bikes" data:ui="product:breadcrumbs">
<span>Bikes</span>
</a>
<a href="/product:list/bikes:1000/mountain:1006/" data:value="Mountain" data:ui="product:breadcrumbs">
<span>Mountain</span>
</a>
<a href="/product:list/bikes:1000/mountain:1006/29:inch:wheel:29ers:1009/" data:value="29:Inch Wheel (29ers)" data:ui="product:breadcrumbs">
<span>29:Inch Wheel (29ers)</span>
</a>
<a href="/product:list/bikes:1000/mountain:1006/29:inch:wheel:29ers:1009/?rb_br=349" data:value="Specialized" data:ui="product:breadcrumbs">
<span>Specialized</span>
</a>

结果是6个元素。我怎么只能说中间的2或3或4个元素?

1 个答案:

答案 0 :(得分:0)

要获取问题样本XML中的最后4个元素,请使用此xpath表达式

//ol[@class="breadcrumb seProductBreadcrumb"]//li[position() >= (last()-4)]//a

输出

<a data-ui="product-breadcrumbs"
   data-value="Bikes"
   href="/product-list/bikes-1000/">
   <span>Bikes</span>
</a>
<a data-ui="product-breadcrumbs"
   data-value="Mountain"
   href="/product-list/bikes-1000/mountain-1006/">
   <span>Mountain</span>
</a>
<a data-ui="product-breadcrumbs"
   data-value="29-Inch Wheel (29ers)"
   href="/product-list/bikes-1000/mountain-1006/29-inch-wheel-29ers-1009/">
   <span>29-Inch Wheel (29ers)</span>
</a>
<a data-ui="product-breadcrumbs"
   data-value="Specialized"
   href="/product-list/bikes-1000/mountain-1006/29-inch-wheel-29ers-1009/?rb_br=349">
   <span>Specialized</span>
</a>