在Bash脚本中转义单引号xmllint xpath查询

时间:2015-01-13 16:30:36

标签: xml bash xpath escaping xmllint

我正在尝试使用bash脚本中的xmllint从xmltv文档查询信息,而我在标题中有单引号的程序标题时遇到问题。例如:

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 line:

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

我尝试了几种方法试图逃避它,包括用双引号括起来并编写Tiger'和Tiger &apos;,我尝试的任何东西似乎都没有帮助。

感谢任何帮助。谢谢!

2 个答案:

答案 0 :(得分:2)

感谢@EtanReisner这有效!:

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

答案 1 :(得分: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