I’m trying to create an xml file from an array. This works more or less. But how can I set the root with parameters.
Xml::create(string $props, string $name = 'root', bool $head = true, int $level = 0): string
I can change the root name, but if I want to add a parameter it doesn’t work
$root = 'Document xmlns="iso..."'
outputs:
<Document xmlns="iso...">...</Document xmlns="iso...">
which is not a valid XML. Is there another way to add a parameter?
And also some parts of the array get striped away after xml::create(). It adds empty values even if there is a value in the array.
According to the source code, there should be an attributes array with the attributes in the props array.https://github.com/getkirby/kirby/blob/3.3.3/src/Toolkit/Xml.php#L65
I have seen that. But I’m not sure how I can add these attributes to my array.
Should be something like this:
$props = [
'whatever' => 'whatever',
'@attributes' => [
'xmlns' => 'iso...',
// ...
],
// ...
];
Edit: Must be @attributes
dump(Xml::create($props, 'document', true, $level = 0));
Result:
<!--?xml version="1.0" encoding="utf-8"?-->
<document xmlns="iso">
<whatever>whatever</whatever>
</document>
Thanks @texnixe. That works almost as expected.
Unfortunately all attribute names gets converted to lowercase: in Html::attr() (https://getkirby.com/docs/reference/tools/html/attr).
Is there a way to keep my attributes how a want them without hardcoding it? I need it like:
<Document Xmlns="iso...">
with capital letters.
texnixe
6
The only way I can think of is to extend the XML class with your custom class and change it so that is doesn’t use the html::attr() method.