2

I'm trying to Query information from an xmltv document using xmllint inside of a bash script and I'm having trouble with program titles that have single quotes in the title. For example:

XML Example:

<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

I've tried several options to try an escape it including wrapping in double quotes and writing Tiger''s and Tiger&apos;s and nothing I try seems to help.

Any help is appreciated. Thanks!

4
  • Which information do you need from tag programme with title "Daniel Tiger's Neighborhood"? Commented Jan 13, 2015 at 16:48
  • 2
    Using "/tv/programme[title='Daniel Tiger's Neighborhood']" should work from the shell's perspective but that isn't going to help the internal problem. "/tv/programme[title=\"Daniel Tiger's Neighborhood\"]" might work so might "/tv/programme[title='Daniel Tiger\'s Neighborhood']" but it depends on xmllint and xml quoting. Commented Jan 13, 2015 at 17:07
  • @Cyrus the end game is to get the content of the start="" attribute in the previously-shown tag to get the original air date using show title, description, and start time as my matching criteria. That way I can name the recordings with the original air date for Plex Media Server to pick up and query TheTVDB.com info. Commented Jan 13, 2015 at 20:49
  • @EtanReisner This worked!: xmllint --xpath "/tv/programme[title=\"Daniel Tiger's Neighborhood\"]" xmltv.xml Commented Jan 13, 2015 at 20:54

2 Answers 2

2

Thanks to @EtanReisner This worked!:

xmllint --xpath "/tv/programme[title=\"Daniel Tiger's Neighborhood\"]" xmltv.xml
Sign up to request clarification or add additional context in comments.

Comments

1

After trying a bit i found two ways. The solution is to escape the single quote by \' outside the xpath XPATHPRE''XPATHPOST and concatenate it, while holding attribute/text surrounding with ".

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

4 Comments

I just tried those lines with this result: andrew@andyrblank:$ xmllint --xpath '/programme/title[text()="Daniel Tiger'\''s Neighborhood"]' xmltv.xml XPath set is empty andrew@andyrblank:$ xmllint --xpath '/programme/title[text()="'Daniel Tiger\'s Neighborhood'"]' xmltv.xml warning: failed to load external entity "Tiger's" warning: failed to load external entity "Neighborhood"]" XPath error : Unfinished literal xmlXPathEval: evaluation failed XPath evaluation failure
That second suggested command is not correct. It would need to be ..."'"Daniel Tiger's Neighboorhood"'"... I believe.
for my minimal example both solutions worked fine. so here is what i got: minimal XML <foo> <bar title="ke'ks">pu'ps</bar> </foo> and the minimal script #!/bin/bash xmllint --xpath '/foo/bar[text()="pu'\''ps" and @title="ke'\''ks"]' foo.xml xmllint --xpath '/foo/bar[text()="'pu\'ps'" and @title="'ke\'ks'"]' foo.xml. Both commands end up in displaying <bar title="ke'ks">pu'ps</bar>. @Etan Reisner your solution works for me as well :)
Lack of spaces in your sample causes the problem there. You don't need to quote to keep the word together for your case but you do for @AndyRBlank's case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.