-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attributes for root elements #148
Comments
Hi @ilyachase , The library is optimized for the 'happy path', while still allowing more complex features if needed. Writing encoding="utf-8"This is not done, because it's the default for XML and thus unnecessary. Parsers should not need this, so we won't add a way to add this by default. To implement this yourself, you can subclass the Adding an attribute to the root element.The easiest way to solve this is to not write a PHP array in your Example class: class MyRootElem implements \Sabre\Xml\XmlSerializable {
public $value;
function __construct($value) {
$this->value = $value;
}
public function xmlSerialize(\Sabre\Xml\Writer $writer) {
$writer->writeAttribute('version', '1.00');
$writer->write($value);
}
} If you called this before: $service->write('uclassify', $value); Now you would call: $service->write('uclassify', new MyRootElem($value)); You don't have to implement $service->classMap['MyRootElem2'] = function( $writer, $value) {
$writer->writeAttribute('version', '1.00');
$writer->write($value);
}
$service->write('uclassify', new MyRootElem2($value)); The difference between these 2 cases is with the I think a nice feature request for this library could be to allow callbacks to be passed directly to the serializer, but this does NOT work today: $service->write('uclassify', function ($writer) {
$writer->writeAttribute('version', '1.00');
$writer->write($value);
}); |
I see. Thanks for the explanation. I can try to do pull request for callback if you want. |
@evert do you want me to try to do Pull Request? |
Hi guys. Awesome library. But I've faced problem - how can I set attributes for root elements?
Let me give you and example. I needed to build XML with following structure:
And I didn't found the way to do two things:
encoding
attribute for<?xml version="1.0" encoding="utf-8" ?>
version
foruclassify
element. This caused me problems, because server-side validator tells it's required.I noticed that second argument of \Sabre\Xml\Service::write() function can take and render a lot of things, but as first argument it only takes a string so I couldn't set attributes for it.
The text was updated successfully, but these errors were encountered: