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
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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;
+1 −1
Original line number Diff line number Diff line
@@ -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;
+21 −0
Original line number Diff line number Diff line
<?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>
+32 −0
Original line number Diff line number Diff line
<?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>
+36 −1
Original line number Diff line number Diff line
@@ -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.
   *