Ajax ?xml-stylesheet

K

Kenpatchi

When I use Ajax to load in an xml document (text/xml) it has these
directives :

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="/xsl/result.xsl"?>

at the top of the .xml file.

Can anyone explain how to obtain the href xsl as a string from the
returned xmlhttprequest object or response object? I have tried
everything that looks obvious but the root element is the start of my
xml document, and the ?xml are not in the call headers. Close to
banging my noggin on the desk here.

The only other way I can obtain the .xsl reference is by stripping the
header in a Java servlet and making 2 calls and then a 3rd call to load
the .xsl file.
 
M

Martin Honnen

Kenpatchi wrote:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="/xsl/result.xsl"?>

at the top of the .xml file.

Can anyone explain how to obtain the href xsl as a string from the
returned xmlhttprequest object or response object?


The <?xml-styleheet?> in terms of the DOM is a processing instruction
node of nodeType 7. It is a direct child node of the document node
itself. The part
type="text/xsl" href="/xsl/result.xsl"
is available as
node.data
meaning you need to parse out the href value yourself from the data
string. Here is an example function doing that

function getStylesheetHref (xmlDocument) {
var child;
for (var i = 0, l = xmlDocument.childNodes.length; i < l; i++) {
child = xmlDocument.childNodes;
if (child.nodeType == 7 &&
child.target == 'xml-stylesheet')
{
var match, href;
if ((match = /href=["']([^"']*)["']/.exec(child.data)) &&
(href = match[1]))
{
return href;
}
}
}
return '';
}

So you could do e.g.

var href = getStylesheetHref(httpRequest.responseXML);

and in your example the variable href then has the string value
'/xsl/result.xsl'. If no xml-stylesheet child is found then the empty
string is returned.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top