Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] XmlEncoder does not parse namespace for node without children #52385

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

Closed
Eydamos opened this issue Oct 31, 2023 · 0 comments
Closed

Comments

@Eydamos
Copy link

Eydamos commented Oct 31, 2023

Symfony version(s) affected

6.3

Description

When using XmlEncoder::decode on a node that does not have children and the node has a namespace defined then the namespace is not in the array even if it is needed for attributes on that node.
This results in an invalid xml when encoding it again to XML.

In the following example on how to reproduce there is a namespaced attribute xsi:noNamespaceSchemaLocation but after reading the xml and writing it again this is now invalid xml and throws a warning when trying to read it:
PHP Warning: XMLReader::read(): test.xml:2: namespace error : Namespace prefix xsi for noNamespaceSchemaLocation on property is not defined

How to reproduce

$encoder = new XmlEncoder();

$data = $encoder->decode(
    <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<property xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://domain.com/path/to/schema.xsd" modification_timestamp="2009-11-10T10:52:16.0Z"></property>
XML,
    'array'
);

var_export($data);
// array (
//  '@xsi:noNamespaceSchemaLocation' => 'http://domain.com/path/to/schema.xsd',
//  '@modification_timestamp' => '2009-11-10T10:52:16.0Z',
//  '#' => '',
//)

echo $encode->encode($data, 'xml');
// <response xsi:noNamespaceSchemaLocation="http://domain.com/path/to/schema.xsd" modification_timestamp="2009-11-10T10:52:16.0Z"></response>

Possible Solution

The method XmlEncoder::parseXmlAttributes could check if an attribute has a namespace and at least fetch this namespace from the $node like it is done in XmlEncoder::decode in case the root node has child nodes:

foreach ($xpath->query('namespace::*', $dom->documentElement) as $nsNode) {
    $data['@'.$nsNode->nodeName] = $nsNode->nodeValue;
}

Additional Context

No response

@Eydamos Eydamos added the Bug label Oct 31, 2023
fabpot added a commit that referenced this issue Nov 23, 2023
…tarld)

This PR was merged into the 5.4 branch.

Discussion
----------

[Serializer] Fix XML attributes not added on empty node

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #52385
| License       | MIT

Add XML namespace attributes on node whether they have chlidren or not.

Commits
-------

4ee28fb [Serializer] Fix XML attributes not added on empty
@fabpot fabpot closed this as completed Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants