Jump to content

XSL syntax validation.


Guest vishwas

Recommended Posts

Guest vishwas

Hi,I want to validate the xsl syntax. I'm using the dom parser as of now and passing the string to it as an InputSource.It is only checking for well formedness but i want it to be checked for validity. Please let me know as to how I could do that.The xsl syntax is as follows:<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:otxsl='http://xml.gxs.com/otxsl' version='1.0'><xsl:value-of select='otxsl:errcode()'></xsl:value-of></xsl:stylesheet>The code is as follows: InputSource source = new InputSource(new StringReader(args[0]));DOMParser parser = new DOMParser();parser.parse(source);I have also used the following:DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();// Always turn on namespace awarenessfactory.setNamespaceAware(true);// Turn on validationfactory.setValidating(true);DocumentBuilder parser = factory.newDocumentBuilder();parser.parse(new InputSource(new StringReader(xml)));but none of the above seem to be validating. Please suggest...Thanks in advance,Vishwas.

Link to comment
Share on other sites

If you want to validate any XML document then you first need a schema. I am not sure there is any schema for XSLT 1.0, a schema for XSLT 2.0 exists: http://www.w3.org/2007/schema-for-xslt20.xsd.And, assuming you are using Java to perform the validation then I think the SetValidating method is meant for DTD based validation. If you want to use a schema then you don't need that method, instead you need to use a Schema object. Here is some sample code snippet:

			SchemaFactory xsdFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);			Schema xslt20Schema = xsdFactory.newSchema(new File("W3CXSLT20Schema.xml"));			String sheet = "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"2.0\">" +						   "  <xsl:ouput method=\"xml\"/>" +						   "  <xsl:template match=\"/\">" +						   "	<xsl:value-of select=\"foo\" separater=\" | \"/>" +						   "  </xsl:template>" +						   "</xsl:stylesheet>";			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();			docBuilderFactory.setNamespaceAware(true);			docBuilderFactory.setSchema(xslt20Schema);			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();			docBuilder.setErrorHandler(new ErrorHandler() {				public void error(SAXParseException e) { System.out.println(e.getMessage()); }				public void fatalError(SAXParseException e) { System.out.println(e.getMessage()); }				public void warning(SAXParseException e) {}			});			Document sheetDoc = docBuilder.parse(new InputSource(new StringReader(sheet)));

W3CXSLT20Schema.xml is a local copy of the schema file mentioned above. The sample stylesheet contains two typos resulting in an invalid element named "xsl:ouput" and an invalid attribute named "separater" so the above snippet outputs

cvc-complex-type.2.4.a: Invalid content was found starting with element 'xsl:ouput'. One of '{"http://www.w3.org/1999/XSL/Transform":import, "http://www.w3.org/1999/XSL/Transform":declaration, "http://www.w3.org/1999/XSL/Transform":variable, "http://www.w3.org/1999/XSL/Transform":param, WC[##other:"http://www.w3.org/1999/XSL/Transform"]}' is expected.cvc-complex-type.3.2.2: Attribute 'separater' is not allowed to appear in element 'xsl:value-of'.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...