views:

154

answers:

3

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?

A: 

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.

andygeers
A: 

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

streetpc
So run a domxpath query for xmlns:xsl="http://www.w3.org/1999/XSL/Transform"???actually from memory I think domxpath querying elements with a colon in it such as xmlns:xsl simply won't work? (Because nobody has updated it in a very long time!) Which means I'd have to do a regular expression to find it... bugger! Is there another way to do it?
EddyR
...or would finding the root xsl element as @andygeers suggest be easier?
EddyR
@EddyR: domxpath has namespace support, have a look at the `registerNameSpace()` function. You'd have to register the "xsl" namespace prefix prior to the query, but it would work.
Tomalak
"xsl" isn't the mandatory prefix, you can choose anything you want. The URI is the namespace identifier, the prefix is more like a variable name containing it.
streetpc
A: 

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
streetpc
Thanks guys, I didn't know any of these. Will try them all out tomorrow.
EddyR
@streetpc - not to sure about the xsl schema thing... is there a default schema if you don't reference one?Also I just checked out the xsl namespace and I didn't realize there are versions 1 and 2. I think I've only ever used version 1. Which one should I be using and why?
EddyR
I've decide that this should be new a question of validating xsl since we have to load the entire contents of the file anyway. The current question has been taken as far as it will go. Thanks!
EddyR
you're welcome :)
streetpc