0

I have this XML file (part of) and I need to get version and modify date with jQuery.

<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf"
     version="3.0"
     xml:lang="nl"
     unique-identifier="isbn-id"
     prefix="cc: http://creativecommons.org/ns# rendition: http://www.idpf.org/vocab/rendition/# ibooks: http://vocabulary.itunes.apple.com/rdf/ibooks/vocabulary-extensions-1.0/">
  <metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
        <meta refines="#title" property="title-type">main</meta>
        <meta property="ibooks:version">1.0.440</meta>
        <meta property="dcterms:modified">2014-06-08T21:15:17Z</meta>
...

This is the javascript/jQuery function that reads the version and date from above XML file:

$(document).ready(function(){

$.ajax({
type: "GET",
url: "package.opf",
dataType: "xml",
success: function(xml) {
    var opf = $.parseXML( xml );

    $("span#datum").html($(opf).find("meta[property='dcterms:modified']").text());
$("span#versie").html($(opf).find("meta[property='ibooks:version']").text());
  },
error: function(jqHXR, textStatus, errorThrown) {
    console.log(jqHXR);
    console.log(textStatus);
    console.log(errorThrown);}     
});
});

which puts version and date in an existing XHTML file, part of which is this:

 <p style="margin: 2em;" class="noind">Versie: <span id="versie">1.0.497</span>
 <br />Datum: <span id="datum">2014-06-05T22:15:50Z</span>

However: I get blanks. Version and date are overwritten by blanks. How is that possible?

yes it is, I found. But like this:

$(document).ready(function(){
   $.get ('package.opf', function(xml) {
      $("span#datum").html($(xml).find("meta[property='dcterms\\:modified']").text());
      $("span#versie").html($(xml).find("meta[property='ibooks\\:version']").text());
    });
 });

where the .opf file is in the same directory as the xhtml file that contains the javascript. But I don't think this works for an EPUB3 book in iBooks, because there, I see no change.

Erik
  • 894
  • 1
  • 8
  • 25

1 Answers1

0

It's jQ's handling of XML nodes & attributes with colons in their name; you need to escape them as such:

$("span#datum").html($(opf).find("meta[property='dcterms\\:modified']").text());
$("span#versie").html($(opf).find("meta[property='ibooks\\:version']").text());

EDIT

Updated to add jsFiddle: http://jsfiddle.net/7cu7g/

Stevangelista
  • 1,799
  • 1
  • 10
  • 15
  • With double backslash? Then I see in Firebug "dcterms\:modified". SO I do one backslash. however, with one or two, i still get blanks. – Erik Jun 08 '14 at 21:53
  • Yes, double backslash: http://stackoverflow.com/questions/853740/jquery-xml-parsing-with-namespaces. I use jQ for parsing namespaced XML a lot (thanks SharePoint!) and so I'm used to it by now, though it wasn't obvious to me at first either. – Stevangelista Jun 08 '14 at 21:55
  • Still only blanks...grrrrr. Maybe it's got something to do with namespaces? – Erik Jun 08 '14 at 22:09
  • Try outputting .length - do you get a 0 or a 1? That would tell you if you're getting a match on your selector. – Stevangelista Jun 08 '14 at 22:16
  • console.log($(opf).find("meta[property='dcterms\\:modified']").text().length); gives me 0 – Erik Jun 08 '14 at 22:19
  • 1
    Sorry, I meant .length instead of .text() - the length value will tell you how many matching objects jQ found based on the selector specified. – Stevangelista Jun 08 '14 at 22:24
  • 0. No matches at all. – Erik Jun 08 '14 at 22:27
  • neither does console.log($(opf).length); – Erik Jun 08 '14 at 22:30
  • The latter may be an indication of the problem then – Stevangelista Jun 08 '14 at 22:31
  • Yep. The package.opf does not get read at all, it seems. Well, tomorrow will bring light. First sleep on it. Thanks a lot. – Erik Jun 08 '14 at 22:37
  • 1
    @Erik - I created a quick jsFiddle to demonstrate this working; see edited answer. – Stevangelista Jun 09 '14 at 10:56
  • Seen it. Thanks. So, it appears that the file is not read at all. Somehow it cannot find it, or there is an error in it. I keep fiddling... – Erik Jun 09 '14 at 21:09
  • See my update of the original post. The parsing is not necessary. – Erik Jun 10 '14 at 09:59