为什么这个 XMLStarlet 查询不工作?

为什么这个 XMLStarlet 查询不工作?

我正在尝试编写一个简单的 bash 脚本,用于解析 eBay 开发人员 API 搜索结果中的价格信息。以下是“Detective Comics 700”的 XML 搜索结果示例:

<findItemsAdvancedResponse xmlns="http://www.ebay.com/marketplace/search/v1/services">
<ack>Success</ack>
<version>1.12.0</version>
<timestamp>2014-06-21T19:03:49.943Z</timestamp>
<searchResult count="1">
<item>
<itemId>301209856743</itemId>
<title>
DETECTIVE COMICS (1937 Series) #700 Near Mint Comics Book
</title>
<globalId>EBAY-US</globalId>
<primaryCategory>
<categoryId>77</categoryId>
<categoryName>Other</categoryName>
</primaryCategory>
<galleryURL>
http://thumbs4.ebaystatic.com/m/mBYOI1SLUSGe0DL1FmHjdCw/140.jpg
</galleryURL>
<viewItemURL>
http://www.ebay.com/itm/DETECTIVE-COMICS-1937-Series-700-Near-Mint-Comics-Book-/301209856743?pt=US_Comic_Books
</viewItemURL>
<paymentMethod>PayPal</paymentMethod>
<paymentMethod>VisaMC</paymentMethod>
<paymentMethod>Discover</paymentMethod>
<autoPay>false</autoPay>
<location>USA</location>
<country>US</country>
<shippingInfo>
<shippingServiceCost currencyId="USD">4.95</shippingServiceCost>
<shippingType>Flat</shippingType>
<shipToLocations>Worldwide</shipToLocations>
<expeditedShipping>true</expeditedShipping>
<oneDayShippingAvailable>false</oneDayShippingAvailable>
<handlingTime>3</handlingTime>
</shippingInfo>
<sellingStatus>
<currentPrice currencyId="USD">6.0</currentPrice>
<convertedCurrentPrice currencyId="USD">6.0</convertedCurrentPrice>
<sellingState>Active</sellingState>
<timeLeft>P17DT7H31M1S</timeLeft>
</sellingStatus>
<listingInfo>
<bestOfferEnabled>false</bestOfferEnabled>
<buyItNowAvailable>false</buyItNowAvailable>
<startTime>2014-06-09T02:34:50.000Z</startTime>
<endTime>2014-07-09T02:34:50.000Z</endTime>
<listingType>StoreInventory</listingType>
<gift>false</gift>
</listingInfo>
<returnsAccepted>true</returnsAccepted>
<galleryPlusPictureURL>
http://galleryplus.ebayimg.com/ws/web/301209856743_1_0_1.jpg
</galleryPlusPictureURL>
<isMultiVariationListing>false</isMultiVariationListing>
<topRatedListing>false</topRatedListing>
</item>
</searchResult>
<paginationOutput>
<pageNumber>1</pageNumber>
<entriesPerPage>1</entriesPerPage>
<totalPages>111</totalPages>
<totalEntries>111</totalEntries>
</paginationOutput>
<itemSearchURL>
http://www.ebay.com/sch/63/i.html?LH_TitleDesc=1&_nkw=detective+comics+700&_ddo=1&_ipg=1&_pgn=1
</itemSearchURL>
</findItemsAdvancedResponse>

我本质上只想解析标题、价格,也许还有运费。

初步研究表明这xmlstarlet是一个明智的选择,但它不起作用(我知道我一定做错了什么)。

当我尝试搜索时,我得到空白结果:

[foouser@foobox fooapp]# cat xmlsample | xmlstarlet sel -t -v "//title"
[foouser@foobox fooapp]#

[foouser@foobox fooapp]# xmlstarlet sel -t -v "//findItemsAdvancedResponse/searchResult/item/title" xmlsample
[foouser@foobox fooapp]#

关于我误入歧途的地方有什么想法吗?

答案1

当我尝试重现您的步骤时,我遇到两个问题:

  • EntityRef: expecting ';'

看起来源文档&在应该使用的地方使用了&amp;.

我使用 修复了这个问题sed -i -e 's/&/&amp;/g' xmlresult

  • None of the XPaths matched; to match a node in the default namespace use '_' as the prefix (see section 5.1 in the manual).

下列的第 5.1 节,我尝试添加-N services=http://www.ebay.com/marketplace/search/v1/services并放入services:XPath 查询,现在我得到了一些有用的东西。

$ xmlstarlet sel -N services=http://www.ebay.com/marketplace/search/v1/services -t -v '//services:title' result.xml

DETECTIVE COMICS (1937 Series) #700 Near Mint Comics Book

相关内容