E
E
Evgeny Ilin2011-12-06 11:56:34
PHP
Evgeny Ilin, 2011-12-06 11:56:34

Generating xml file with included xslt table without XsltProcessor php?

The usual generation mechanism using xslt_processor is:

$template_out = '&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;';<br/>
$template = file_get_contents($template);<br/>
 $proc = new XsltProcessor;<br/>
$xmlDoc = new DomDocument;<br/>
$xslDoc= new DomDocument;<br/>
$xmlDoc-&gt;loadXML($template_out);<br/>
$xslDoc-&gt;loadXML($template);<br/>
$proc-&gt;importStyleSheet($xslDoc);<br/>
$result = $proc-&gt;transformToXML($xmlDoc);

You need something like:
$template_out = '&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br/>
&lt;?xml-stylesheet href=&quot;default.xsl&quot; type=&quot;text/xsl&quot; ?&gt;';<br/>
 echo $template_out;

and a valid page is displayed (it is clear that it is not displayed in this version).
Is there a solution?
Right now I'm using this mechanism:
$template = 'path/to/xsl-stylesheet';<br/>
$xsl = new DomDocument();<br/>
$xsl-&gt;load($template);<br/>
$inputDom = new DomDocument();<br/>
$inputDom-&gt;loadXML($template_out);<br/>
$inputDom-&gt;formatOutput = true;<br/>
$proc = new XsltProcessor();<br/>
$xsl = $proc-&gt;importStylesheet($xsl);<br/>
$newDom = $proc-&gt;transformToDoc($inputDom);<br/>
print $newDom-&gt;saveXML();

However, it does not work, as I understand it, due to the inclusions nested in default.xsl. I'm trying to fix the bugs, but it feels like I'm doing something wrong.
Is this the correct solution or are there others?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
cat_crash, 2011-12-06
@geirby

As it is not very clear the essence of the issue. If you mean that your script is displayed in the browser as HTML and not as XML, then you need to specify the text/xml content type in the header:
header("Content-type: text/xml");

C
cat_crash, 2011-12-06
@cat_crash

then I guess you need to wrap the output in CDATA php.net/manual/en/function.domdocument-create-cdata-section.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question