Martin
Martin

Reputation: 21

How to validate XSL file?

could anyone help me with XSL file validation? I don't want to validate it for start/closed tags but for "inside atributes". For examle: My XSL file contains line <td><xsl:value-of select="year"/></td> I need check this one for syntax errors like <td><xsl:vae-of select="year"/></td>.

Is there any way how to do this with javascript or jQuery?

Upvotes: 1

Views: 5213

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167401

An XSLT processor checks the stylesheet code so in Mozilla browsers you could pass a DOM document with your stylesheet code to the importStylesheet method of an XSLTProcessor object e.g.

var proc = new XSLTProcessor();
var sheet = new DOMParser().parseFromString([
    '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">',
    '<xsl:template match="/"><xsl:vae-of select="year"/></xsl:template>',
    '</xsl:stylesheet>'
    ].join('\n'), 'application/xml');
try {
    proc.importStylesheet(sheet);
}
catch(e) {
    // handle error here e.g.
    console.log(e.message);
}

outputs "Component returned failure code: 0x80600001 [nsIXSLTProcessor.importStylesheet]".

But be aware that an XSLT 1.0 processor is supposed to process stylesheet code with a version greater than 1.0 in forwards compatible mode and that way if you wanted to validate user input code as XSLT 1.0 you would need to make sure that it has version="1.0" to catch any errors against the version 1.0 of the XSLT language.

And of course that error message is not very helpful to identify what's wrong in the code.

Upvotes: 1

Related Questions