2015-01-13 74 views
2

我試圖從xmltv文檔中使用bash腳本內部的xmllint查詢信息,並且遇到標題中帶有單引號的程序標題的問題。例如:在bash腳本中轉義單引號xmllint xpath查詢

XML實例:

<programme start="20150106090000 -0500" stop="20150106093000 -0500" channel="I53.28457646.microsoft.com"> 
     <title lang="en">Daniel Tiger's Neighborhood</title> 
     <sub-title lang="en">Safety Patrol; Safety at the Beach</sub-title> 
     <desc lang="en">Prince Tuesday visits the school in his crossing guard uniform and helps the children practice safety rules; Daniel and Katerina drift too far from Mom while playing at the beach.</desc> 
     <credits> 
      <actor>Jake Beale</actor> 
      <actor>Ted Dykstra</actor> 
      <actor>Heather Bambrick</actor> 
      <actor>Amariah Faulkner</actor> 
      <actor>Stuart Ralston</actor> 
      <actor>Zachary Bloch</actor> 
      <actor>Addison Holley</actor> 
      <actor>Nicholas Kaegi</actor> 
     </credits> 
     <date>20130715</date> 
     <category lang="en">Children</category> 
     <category lang="en">Educational</category> 
     <category lang="en">Episodic</category> 
     <category lang="en">Kids</category> 
     <category lang="en">Series</category> 
     <episode-num system="onscreen">130</episode-num> 
     <episode-num system="ms_progid">1.EP015507510029</episode-num> 
     <episode-num system="dd_progid">EP01550751.0029</episode-num> 
     <video> 
      <aspect>16:9</aspect> 
      <quality>HDTV</quality> 
     </video> 
     <audio> 
      <stereo>stereo</stereo> 
     </audio> 
     <previously-shown start="20130715000000" /> 
     <subtitles type="teletext" /> 
     <rating system="VCHIP"> 
      <value>TV-Y</value> 
     </rating> 
    </programme> 

xmllint行:

xmllint --xpath '/tv/programme[title='Daniel Tiger's Neighborhood']' xmltv.xml 

我試過幾個選項來嘗試一種逃避它包括包裹在雙引號,寫老虎 '' s和老虎&apos; s沒有我嘗試似乎幫助。

任何幫助表示讚賞。謝謝!

+0

你從標籤計劃需要哪些信息,標題爲「丹尼爾老虎的鄰居」? – Cyrus

+2

使用''/ tv/program [title ='丹尼爾虎的鄰居']「應該從shell的角度來看,但這不會幫助解決內部問題。 ''/ tv/program [title = \「Daniel Tiger's Neighbourhood \」]「'可能會這樣''/ tv/program [title ='丹尼爾老虎的鄰居']」'但它取決於xmllint和xml引用。 –

+0

@Cyrus最後的遊戲是在前面顯示的標籤中獲取start =「」屬性的內容,以使用節目標題,描述和開始時間作爲我的匹配標準來獲取原始播出日期。通過這種方式,我可以用Plex Media Server的原始日期來命名錄音,以提取並查詢TheTVDB.com信息。 – AndyRBlank

回答

1

嘗試了一下後,我找到了兩種方法。解決辦法是在xpath XPATHPRE''XPATHPOST之外跳過\'的單引號,並將它連接起來,同時保留圍繞着"的屬性/文本。

xmllint --xpath '/programme/title[text()="Daniel Tiger'\''s Neighborhood"]' xmltv.xml 
xmllint --xpath '/programme/title[text()="'Daniel Tiger\'s Neighborhood'"]' xmltv.xml 
+0

我剛試過這樣的結果:andrew @ andyrblank:$ xmllint --xpath '/ program/title [text()=「Daniel Tiger'\''s Neighborhood」]'xmltv.xml XPath set is empty andrew @ andyrblank:$ xmllint --xpath'/ program/title [text()='' 「老虎的鄰居'」]'xmltv.xml警告:無法加載外部實體「老虎的」警告:無法加載外部實體「鄰居」]「XPath錯誤:未完成的文字xmlXPathEval:評估失敗 XPath評估失敗 – AndyRBlank

+1

第二個建議的命令是不正確的,它需要'......'''丹尼爾老虎的鄰居''''......'我相信 –

+0

爲我的最小的例子兩個解決方案都工作正常,所以這裏是我得到: 最小的XML' pu'ps ' and the minimal script'#!/ bin/bash xmllint --xpath'/ foo/bar [text()=「pu'\' 'ps'和@ title =「ke'\''ks」]'foo.xml xmllint --xpath'/ foo/bar [text()=''pu \'ps'「and @title =」'ke 'ks'「]'foo.xml'。兩個命令最終都會顯示' pu'ps'。 @Etan Reisner你的解決方案也適用於我:) –

2

由於@EtanReisner這個工作!:

xmllint --xpath "/tv/programme[title=\"Daniel Tiger's Neighborhood\"]" xmltv.xml