4

I have xml files which make use of an XSL stylesheet to format it when view in a web browser. I save the xml files in a central location, a shared drive which any of my colleagues can access. If they open an XML file directly from the shared location, it renders correctly, however sometimes they take a copy of the XML, and only the XML file that they need. when they view the file in a browser it fails to locate the XSL stylesheet and hence doesn't render. Is there a way to say if the xsl stylesheet is available, make use of it, if not then simply ignore using the stylesheet and display the xml file as if there were not stylesheet. Basically this would mean no error would be seen when a local copy is taken. Can this be done

2 Answers2

0

You're presumably relying on the <?xml-stylesheet?> processing instruction. I don't know of any way to parameterize how this behaves if the stylesheet can't be located: I dare say it depends on the browser anyway.

Why not have the use an absolute URI that can fetch the stylesheet from anywhere on your network? You may run into cross-site scripting issues, but it's worth trying.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
-1

I suspect that there is no neat way to do it. You could use the use-when attribute on importation like so ...

 <xsl:import
   xmlns:fn="http://www.w3.org/2005/xpath-functions"
   href="'general.xslt'"
   use-when="fn:unparsed-text-available( 'general.xslt') />

The above element will import the style-sheet 'general.xslt' if it exists. The problem/ limitation of this solution is that for the @href attribute, the XSLT knows where to find general.xslt from your xslt's configuration (OASIS catalogs, command line parameters, environment variables - whatever. It is vendor specific). However the same location finding logic does not apply to unparsed-text-available(), which takes a URI parameter.

So you might like to parameterise the above element like this ...

 <xsl:import
   xmlns:fn="http://www.w3.org/2005/xpath-functions"
   href="$stylesheet-to-import"
   use-when="fn:unparsed-text-available( $uri-of-stylesheet-to-import) />

where the following is assumed:

  1. $stylesheet-to-import is a parameter/ variable for the style-sheet to be imported. It can be short form if it can be located thus by the XSLT engine, otherwise it should the same as $uri-of-stylesheet-to-import
  2. $uri-of-stylesheet-to-import is uri of the style-sheet to be imported if it exists.
  3. If the file pointed to by $uri-of-stylesheet-to-import exists, then it is a valid xslt file.

I think this solution only works for XSLT 2.0. I am not sure about XSLT 1.0.

Also read Dimitre's answer on this similar question: How do I check for the existence of an external file with XSL? .

NOTE: As an alternative to fn:unparsed-text-available(), you could also use fn:doc-available(). It will be slower as it checks that the document is valid XML, which may or may not be a good thing depending on your problem.

Community
  • 1
  • 1
Sean B. Durkin
  • 12,659
  • 1
  • 36
  • 65
  • does this mean i can use the xsl import statement as a line in my xml files, so that if the xml files get moved around a check will be made for the associated(imported) stylesheet? if the stylesheet exists it will render nicely, but if the stylesheet isn't found it will just display as raw xml? – Man Wa kileleshwa May 23 '12 at 05:41
  • 1
    This isn't going to work. Browsers only support XSLT 1.0, which doesn't have use-when. And the href attribute must be hard-coded, whether in 1.0 or 2.0. Downvoting. – Michael Kay May 23 '12 at 07:08