jqueryajaxxmlxinclude

jQuery Ajax calling XML with Xinclude


So I got three levels of XML:

app.xml includes 4 categories (XML-files). Each category includes a number of entries.

The including part is right now done with Xinclude.

My script runs an Ajax function getting the first xml, app.xml right now, but won't see through the Xinclude. To clarify, I get my alerts 'yes' and 'app' but not 'cat'. In the console.dir xml I see the xi:include, but not what's in the files.

Do I need to parse it somehow? Does my Xinclude not work, or can I do the include in a better way?

script.js

$.ajax({
    type: "GET",
    url: "xml/app.xml",
    dataType: "xml",
    success: function(xml) {
        alert('yes');

        $(xml).find('app').each(function(){

            alert('app');
            var test = $(this);
            console.dirxml(xml);
            $(this).find('category').each(function(){
                alert('cat');
            });
        });
    },

    error: function() {
        alert("The XML File could not be processed correctly.");
    }
});

app.xml

<?xml version="1.0" encoding="UTF-8"?>

<app xmlns:xi="http://www.w3.org/2001/XInclude">
    <xi:include href="cat1.xml" parse="xml"/>
    <xi:include href="cat2.xml" parse="xml"/>
    <xi:include href="cat3.xml" parse="xml"/>
    <xi:include href="cat4.xml" parse="xml"/>
</app>

Example of a category - XML

<?xml version="1.0" encoding="UTF-8"?>
<category xmlns:xi="http://www.w3.org/2001/XInclude">
    <title>Title</title>
    <introtext>Introtext introtext introtext introtext</introtext>

    <xi:include href="cat1/entry.xml"/>
</category>

Example of an entry - XML

<?xml version="1.0" encoding="UTF-8"?>
<entry>
    <rubrik>Entry title</rubrik>
    <text>Text text text</text>
    <bild>pic_name</bild>
</entry>

Solution

  • XML is just text to the AJAX request, so you need to request the original file, then loop through the xi:includes and request each of those files and so on.