Section: Generating XML content
« Generating plain text | ^ Responses: generating content | Generating a RSS feed » |
− Table of content
jResponseXML allows to send an XML response to the client-browser. Its alias is "xml".
To create a jResponseXML object, pass 'text' as argument to getResponse()
method in your controller's action.
$resp = $this->getResponse('xml');
Two solutions exist to set the XML contents.
- *Warning** : the XML should be correctly formated in any case, or the response will generate an error.
Generation with a template ¶
The $content
property contains a jTpl
object by default : it's up to you to set the template-selector in property $contentTpl
.
$resp->contentTpl = 'myModule~myxml';
$resp->content->assign('foo','bla bla bla');
jResponseXml handles generation of the <?xml ?>
tag: it shouldn't be in the template-file.
If you don't want that jReponseXml
generates the xml tag, set the $sendXMLHeader
to false:
$resp->sendXMLHeader = false;
Without a template ¶
It's possible to set the XML content as a string value in property $content.
$rep->content = '<mydoc> <title>jelix</title> </mydoc>';
jResponseXml handles generation of the <?xml ?>
tag: you shouldn't set it explicitely.
If you don't want that jReponseXml generates the xml tag, set the $sendXMLHeader
to false :
$resp->sendXMLHeader = false;
Using style-sheets ¶
It's possible to attach CSS or XSLT style-sheets to the generated XML document using method addCSSStyleSheet()
or addXSLStyleSheet()
.
Both expect the same first mandatory argument : the URL to the style-sheet. Both expect the same second optional argument : an associative array defining the pseudo-attributes for the generated processing instruction.
$rep->addCSSStyleSheet('my.css', array('title'=>'Excellent!!'));
$rep->addXSLStyleSheet('my.xsl');