views:

30

answers:

2

Hi,

In my XSLT file. I use the document() function

<xsl:variable name="Publicity" select="document('./publicity.xml')" />

and that works but if I try to link a PHP script that generate the XML dynamically,

<xsl:variable name="Publicity" select="document('./publicity.php')" />

I get a

Warning: XSLTProcessor::transformToXml() [xsltprocessor.transformtoxml]: file:///C:/wamp/www/XSLT/test.php:3: parser error : Start tag expected, '<' not found in ... on line ...

Which consist of the < from <?php

It looks like the XSLTProcessor isn't requesting the file like via a HTTP request so it's not executed by Apache / PHP.

I know I could simply include that XML structure to my main XML but I'm trying to avoid this... until someone tell me there is no other way.

Thank you!

+1  A: 

Right the document() function just reads a file off the disk, it does not make an HTTP request. So the PHP doesn't execute.

You'll have to use an URL instead of just the filename as the argument to your document() function.

<xsl:variable name="Publicity" 
     select="document('http://example.com/publicity.php')" />

I believe that using an URL in this way is a common feature in most XSLT processors, but I have not tested it with PHP, so your mileage may vary.

Bill Karwin
hmm for unknown reason it's not working with PHP. The pages just time out. I am not making any mistake with the URL. I guess it just can't be done in PHP / XSLTProcessor. Thank you though
Cybrix
Then you may have to run your PHP script as an offline task to create the XML file periodically so your XSLT script can read it. Sorry, you can't get there from here. :)
Bill Karwin
A: 

If the first argument of the document() function is a relative URI, this is resolved off the base-uri of the second argument. If there is no second argument, then the base URI of the XSLT stylesheet containing the call to the document() function is used.

Here is an excerpt from the W3C XSLT spec.:

"The URI reference may be relative. The base URI (see [3.2 Base URI]) of the node in the second argument node-set that is first in document order is used as the base URI for resolving the relative URI into an absolute URI. If the second argument is omitted, then it defaults to the node in the stylesheet that contains the expression that includes the call to the document function. Note that a zero-length URI reference is a reference to the document relative to which the URI reference is being resolved; thus document("") refers to the root node of the stylesheet; the tree representation of the stylesheet is exactly the same as if the XML document containing the stylesheet was the initial source document."

This works well when the XSLT stylesheet does have a URI (file url or http url). However, a dynamically generated stylesheet is in memory and has no base URI. In this case a relative URI as the first and only argument of the document() function cannot be resolved successfully.

The solution is to provide the complete (absolute) URI of the XML document.

Dimitre Novatchev
And what would be my solution? Thank you
Cybrix
@Cybrix: As I said: "The solution is to provide the complete (absolute) URI of the XML document".
Dimitre Novatchev
@Dimitre: I see. But that is what Bill Karwin suggested and unfortunatly the PHP's XSLTProcessor can't make the transformation. Thank you for your answer though.
Cybrix
@Cybrix: It would be good if you caould provide in a separate question the cases of using the `document()` function, I believe with avsolute-URL argument, that PHP's XSLT processor "can't make". Maybe there is something else ...
Dimitre Novatchev