Skip to content
Snippets Groups Projects
Commit 88c4f71d authored by Ivan Doroshenko's avatar Ivan Doroshenko
Browse files

Issue #3028162 by Matroskeen, DuaelFr, p-andrei: XML Parser - multiple...

Issue #3028162 by Matroskeen, DuaelFr, p-andrei: XML Parser - multiple non-string values reduced to a single value
parent 6d017f71
No related branches found
No related tags found
1 merge request!62Issue #3028162: Added a test for multiple values.
......@@ -75,7 +75,7 @@ class SimpleXml extends DataParserPluginBase {
foreach ($this->fieldSelectors() as $field_name => $xpath) {
foreach ($target_element->xpath($xpath) as $value) {
if ($value->children() && !trim((string) $value)) {
$this->currentItem[$field_name] = $value;
$this->currentItem[$field_name][] = $value;
}
else {
$this->currentItem[$field_name][] = (string) $value;
......
......@@ -243,7 +243,7 @@ class Xml extends DataParserPluginBase {
// and has children then return the whole object for the process
// plugin or other row manipulation.
if ($value->children() && !trim((string) $value)) {
$this->currentItem[$field_name] = $value;
$this->currentItem[$field_name][] = $value;
}
else {
$this->currentItem[$field_name][] = (string) $value;
......
<?xml version="1.0" encoding="utf-8"?>
<items>
<item>
<Id>item_1</Id>
<Name>Item 1</Name>
<Values1>
<SubItem>
<Id>1</Id>
<Name>Name 1</Name>
</SubItem>
<SubItem>
<Id>2</Id>
<Name>Name 2</Name>
</SubItem>
</Values1>
<Values2>
<SubItem>3</SubItem>
<SubItem>4</SubItem>
</Values2>
</item>
</items>
<?xml version="1.0" encoding="utf-8"?>
<persons>
<person>
<id>1</id>
<name>Elizabeth</name>
<children>
<child age="8">
<name>Elizabeth Junior</name>
<name>Jane</name>
</child>
</children>
</person>
<person>
<id>2</id>
<name>George</name>
<children>
<child>
<name>George Junior</name>
<age>10</age>
</child>
</children>
</person>
<person>
<id>3</id>
<name>Peter</name>
<children>
<child>
<name age="6">Lucy</name>
</child>
</children>
</person>
</persons>
......@@ -117,7 +117,7 @@ abstract class BaseXmlTest extends KernelTestBase {
*/
public function testSingleValueWithAttributes() {
$urls = [
$this->path . '/tests/data/xml_data_parser.xml',
$this->path . '/tests/data/xml_persons.xml',
];
$this->configuration['urls'] = $urls;
$this->configuration['item_selector'] = '/persons/person';
......@@ -143,6 +143,41 @@ abstract class BaseXmlTest extends KernelTestBase {
$this->assertEquals($expected_names, $names);
}
/**
* Tests retrieval a value with multiple items.
*/
public function testMultipleItems(): void {
$this->configuration['urls'] = [
$this->path . '/tests/data/xml_multiple_items.xml',
];
$this->configuration['fields'] = [
[
'name' => 'id',
'label' => 'Id',
'selector' => 'Id',
],
[
'name' => 'sub_items1',
'label' => 'Sub items 1',
'selector' => 'Values1/SubItem',
],
[
'name' => 'sub_items2',
'label' => 'Sub items 2',
'selector' => 'Values2/SubItem',
],
];
$parser = $this->getParser();
$parser->next();
// Transform SimpleXMLELements to arrays.
$item = json_decode(json_encode($parser->current()), TRUE);
$sub_items1 = array_column($item['sub_items1'], 'Id');
$this->assertEquals(['1', '2'], $sub_items1);
$this->assertEquals(['3', '4'], $item['sub_items2']);
}
/**
* Parses and asserts the results match expectations.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment