问题描述
我似乎无法通过 xmlstarlet 获得这个基本的 xslt 查询.
I can't seem to get this basic xslt query working via xmlstarlet.
我确定我遗漏了一些明显的东西,但对于我的一生,我无法弄清楚这个语法,所以请有人照亮我.
I'm sure I'm missing something obvious, but for the life of me I cannot figure out this syntax, so someone please illuminate me.
XML Starlet 命令:
XML Starlet Command:
xml sel -t -m "//rdf:RDF/item" -v link -v description -v link ./sss.rdf
sss.rdf:
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:admin="http://webns.net/mvcb/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:ev="http://purl.org/rss/1.0/modules/event/" xmlns:syn="http://purl.org/rss/1.0/modules/syndication/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/">
<channel rdf:about="http://baltimore.craigslist.org/search/sss?catAbb=sss&amp;format=rss&amp;maxAsk=150&amp;minAsk=50&amp;query=ipod%20touch%205g&amp;srchType=A">
<title>craigslist baltimore | all for sale / wanted search "ipod touch 5g"</title>
<link>http://baltimore.craigslist.org/search/sss?catAbb=sss&amp;maxAsk=150&amp;minAsk=50&amp;query=ipod%20touch%205g&amp;srchType=A</link>
<description />
<dc:language>en-us</dc:language>
<dc:rights>&copy; 2013 craigslist</dc:rights>
<dc:publisher>[email protected]</dc:publisher>
<dc:creator>[email protected]</dc:creator>
<dc:source>http://baltimore.craigslist.org/search/sss?catAbb=sss&amp;format=rss&amp;maxAsk=150&amp;minAsk=50&amp;query=ipod%20touch%205g&amp;srchType=A</dc:source>
<dc:title>craigslist baltimore | all for sale / wanted search "ipod touch 5g"</dc:title>
<dc:type>Collection</dc:type>
<syn:updateBase>2013-09-20T09:23:41-07:00</syn:updateBase>
<syn:updateFrequency>1</syn:updateFrequency>
<syn:updatePeriod>hourly</syn:updatePeriod>
<items>
<rdf:Seq>
<rdf:li rdf:resource="http://baltimore.craigslist.org/ele/4039527375.html" />
</rdf:Seq>
</items>
</channel>
<item rdf:about="http://baltimore.craigslist.org/ele/4039527375.html">
<title><![CDATA[Unlocked Optimus Lg Phone (Baltimore) $150]]></title>
<link>http://baltimore.craigslist.org/ele/4039527375.html</link>
<description>OR WE CAN HAVE A SWAP FOR AN IPOD TOUCH 5g<![CDATA[
●Optimus Lg Phone For Sale At 150.00 The Original Price Was $180.00
●It Does Not Include The Charger, But You Can Find It At Walmart For $4.00
●The Phone Was Only Used For 2-3 Months
&# [...]]]></description>
<dc:date>2013-09-01T10:14:06-07:00</dc:date>
<dc:language>en-us</dc:language>
<dc:rights>&copy; 2013 craigslist</dc:rights>
<dc:source>http://baltimore.craigslist.org/ele/4039527375.html</dc:source>
<dc:title><![CDATA[Unlocked Optimus Lg Phone (Baltimore) $150]]></dc:title>
<dc:type>text</dc:type>
<dcterms:issued>2013-09-01T10:14:06-07:00</dcterms:issued>
</item>
</rdf:RDF>
我想要的输出:
Unlocked Optimus Lg Phone (Baltimore) $150
OR WE CAN HAVE A SWAP FOR AN IPOD TOUCH 5g
●Optimus Lg Phone For Sale At 150.00 The Original Price Was $180.00
●It Does Not Include The Charger, But You Can Find It At Walmart For $4.00
●The Phone Was Only Used For 2-3 Months
&# [...]
http://baltimore.craigslist.org/ele/4039527375.html
推荐答案
这个 XmlStarlet 命令:
xml sel -N purl="http://purl.org/rss/1.0/" -t -m "//rdf:RDF/purl:item" -v purl:title -n -v purl:description -n -v purl:link -n ./sss.rdf
产生所需的输出:
Unlocked Optimus Lg Phone (Baltimore) $150
OR WE CAN HAVE A SWAP FOR AN IPOD TOUCH 5g
&#9679;Optimus Lg Phone For Sale At 150.00 The Original Price Was $180.00
&#9679;It Does Not Include The Charger, But You Can Find It At Walmart For $4.00
&#9679;The Phone Was Only Used For 2-3 Months
&# [...]
http://baltimore.craigslist.org/ele/4039527375.html
说明:
关键是要注意输入文档有一个默认的命名空间,这会导致item
、title
、description
和链接
位于 http://purl.org/rss/1.0/
命名空间中.定义 -N purl="http://purl.org/rss/1.0/"
允许我们在 XPath 中指定这些元素时使用 purl
前缀.如果没有 purl
命名空间前缀,XPath 将不匹配.
The key is to notice that the input document has a default namespace, which causes item
, title
, description
, and link
to be in the http://purl.org/rss/1.0/
namespace. Defining -N purl="http://purl.org/rss/1.0/"
allows us to use the purl
prefix when specifying these elements in the XPaths. Without the purl
namespace prefixes, the XPaths weren't matching.
这篇关于XMLStarlet 和 RSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!