為什麼這個 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

相關內容