Question

I'm using PHP+curl to retrieve an XSL file, but first I want to do some minimum validation that the file is really a XSL file.

I make sure the file exists, has status code 200 OK, and that it has the correct file extension, but then I want to check the header for the Content-Type. I do this using curl_getinfo() function. All fine.

But XSL files always return "application/xml" or "text/xml" as the Content-Type, which is for XML files. I believe an XSL header should be "application/xslt+xml". So what's going on here and how can I make sure it's an XSL document?

Was it helpful?

Solution

You can check the XSLT namespace in the file. If PHP version >= 5.1.2, use SimpleXMLElement::getNamespaces()

OTHER TIPS

Are you using this to access third-party sites or a site that you configure yourself? If it's your own site, you'd need to set up Apache to serve the appropriate mime types depending on the file extension.

If it's third party sites, I'm not sure you can rely on the mime types, since they could serve whatever they want. Best bet is probably to just get the first few lines of the file and check that the root element is xsl.

Different approach, for PHP 5:

  1. You could try to load the stylesheet in an XSLTProcessor. Maybe an exception is thrown if the stylesheet is incorrect
  2. (Better IMHO) Validate the XSL stylesheet against its schema
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top