Необходимо добавить атибуты в XML-файл.

Статус
В этой теме нельзя размещать новые ответы.

djodofin

Постоялец
Регистрация
6 Июл 2017
Сообщения
328
Реакции
139
Необходимо добавить атибуты в XML-файл.

Вот структура XML-файла:
XML:
<root>
  <property>
    <images>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/6-1.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/6-2.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/6-3.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/6-4.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/6-5.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/6-6.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/6-7.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/6-8.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/6-9.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/6-10.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/6-11.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/6-12.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/6-13.jpg</url></image>
    </images>
  </property>
  <property>
    <images>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/3-1.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/3-2.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/3-3.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/3-4.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/3-5.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/3-6.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/3-7.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/3-8.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/3-9.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/3-10.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/3-11.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/3-12.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/3-13.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/3-14.jpg</url></image>
    </images>
  </property>
  <property>
    <images>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/1-1.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/1-2.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/1-3.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/1-4.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/1-5.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/1-6.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/1-7.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/1-8.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/1-9.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/1-10.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/1-11.jpg</url></image>
      <image><url>https://mi-site.com/wp-content/uploads/2023/06/1-12.jpg</url></image>
    </images>
  </property>
</root>

Прописал путь от корня файла ('/root/property/images/image'), атрибуты добавляються. Но не так как надо!

Вот код php:
PHP:
<?php
// Загрузка XML-файла
$xml = simplexml_load_file('kyero-mivo.xml');

// Получение списка элементов <image>
$imageElements = $xml->xpath('/root/property/images/image');

// Проход по каждому элементу и добавление атрибута id
foreach ($imageElements as $index => $imageElement) {
    $imageElement->addAttribute('id', $index + 1);
}

// Сохранение изменений обратно в файл
$xml->asXML('kyero.xml');
?>

Вот результат выполнения выходного XML-файла:
XML:
<root>
  <property>
    <images>
      <image id="1"><url>https://mi-site.com/wp-content/uploads/2023/06/6-1.jpg</url></image>
      <image id="2"><url>https://mi-site.com/wp-content/uploads/2023/06/6-2.jpg</url></image>
      <image id="3"><url>https://mi-site.com/wp-content/uploads/2023/06/6-3.jpg</url></image>
      <image id="4"><url>https://mi-site.com/wp-content/uploads/2023/06/6-4.jpg</url></image>
      <image id="5"><url>https://mi-site.com/wp-content/uploads/2023/06/6-5.jpg</url></image>
      <image id="6"><url>https://mi-site.com/wp-content/uploads/2023/06/6-6.jpg</url></image>
      <image id="7"><url>https://mi-site.com/wp-content/uploads/2023/06/6-7.jpg</url></image>
      <image id="8"><url>https://mi-site.com/wp-content/uploads/2023/06/6-8.jpg</url></image>
      <image id="9"><url>https://mi-site.com/wp-content/uploads/2023/06/6-9.jpg</url></image>
      <image id="10"><url>https://mi-site.com/wp-content/uploads/2023/06/6-10.jpg</url></image>
      <image id="11"><url>https://mi-site.com/wp-content/uploads/2023/06/6-11.jpg</url></image>
      <image id="12"><url>https://mi-site.com/wp-content/uploads/2023/06/6-12.jpg</url></image>
      <image id="13"><url>https://mi-site.com/wp-content/uploads/2023/06/6-13.jpg</url></image>
    </images>
  </property>
  <property>
    <images>
      <image id="14"><url>https://mi-site.com/wp-content/uploads/2023/06/3-1.jpg</url></image>
      <image id="15"><url>https://mi-site.com/wp-content/uploads/2023/06/3-2.jpg</url></image>
      <image id="16"><url>https://mi-site.com/wp-content/uploads/2023/06/3-3.jpg</url></image>
      <image id="17"><url>https://mi-site.com/wp-content/uploads/2023/06/3-4.jpg</url></image>
      <image id="18"><url>https://mi-site.com/wp-content/uploads/2023/06/3-5.jpg</url></image>
      <image id="19"><url>https://mi-site.com/wp-content/uploads/2023/06/3-6.jpg</url></image>
      <image id="20"><url>https://mi-site.com/wp-content/uploads/2023/06/3-7.jpg</url></image>
      <image id="21"><url>https://mi-site.com/wp-content/uploads/2023/06/3-8.jpg</url></image>
      <image id="22"><url>https://mi-site.com/wp-content/uploads/2023/06/3-9.jpg</url></image>
      <image id="23"><url>https://mi-site.com/wp-content/uploads/2023/06/3-10.jpg</url></image>
      <image id="24"><url>https://mi-site.com/wp-content/uploads/2023/06/3-11.jpg</url></image>
      <image id="25"><url>https://mi-site.com/wp-content/uploads/2023/06/3-12.jpg</url></image>
      <image id="26"><url>https://mi-site.com/wp-content/uploads/2023/06/3-13.jpg</url></image>
      <image id="27"><url>https://mi-site.com/wp-content/uploads/2023/06/3-14.jpg</url></image>
    </images>
  </property>
  <property>
    <images>
      <image id="28"><url>https://mi-site.com/wp-content/uploads/2023/06/1-1.jpg</url></image>
      <image id="29"><url>https://mi-site.com/wp-content/uploads/2023/06/1-2.jpg</url></image>
      <image id="30"><url>https://mi-site.com/wp-content/uploads/2023/06/1-3.jpg</url></image>
      <image id="31"><url>https://mi-site.com/wp-content/uploads/2023/06/1-4.jpg</url></image>
      <image id="32"><url>https://mi-site.com/wp-content/uploads/2023/06/1-5.jpg</url></image>
      <image id="33"><url>https://mi-site.com/wp-content/uploads/2023/06/1-6.jpg</url></image>
      <image id="34"><url>https://mi-site.com/wp-content/uploads/2023/06/1-7.jpg</url></image>
      <image id="35"><url>https://mi-site.com/wp-content/uploads/2023/06/1-8.jpg</url></image>
      <image id="36"><url>https://mi-site.com/wp-content/uploads/2023/06/1-9.jpg</url></image>
      <image id="37"><url>https://mi-site.com/wp-content/uploads/2023/06/1-10.jpg</url></image>
      <image id="38"><url>https://mi-site.com/wp-content/uploads/2023/06/1-11.jpg</url></image>
      <image id="39"><url>https://mi-site.com/wp-content/uploads/2023/06/1-12.jpg</url></image>
    </images>
  </property>
</root>
Требуемый результат выходного XML-файла. Как добиться требуемого результата?

XML:
<root>
  <property>
    <images>
      <image id="1"><url>https://mi-site.com/wp-content/uploads/2023/06/6-1.jpg</url></image>
      <image id="2"><url>https://mi-site.com/wp-content/uploads/2023/06/6-2.jpg</url></image>
      <image id="3"><url>https://mi-site.com/wp-content/uploads/2023/06/6-3.jpg</url></image>
      <image id="4"><url>https://mi-site.com/wp-content/uploads/2023/06/6-4.jpg</url></image>
      <image id="5"><url>https://mi-site.com/wp-content/uploads/2023/06/6-5.jpg</url></image>
      <image id="6"><url>https://mi-site.com/wp-content/uploads/2023/06/6-6.jpg</url></image>
      <image id="7"><url>https://mi-site.com/wp-content/uploads/2023/06/6-7.jpg</url></image>
      <image id="8"><url>https://mi-site.com/wp-content/uploads/2023/06/6-8.jpg</url></image>
      <image id="9"><url>https://mi-site.com/wp-content/uploads/2023/06/6-9.jpg</url></image>
      <image id="10"><url>https://mi-site.com/wp-content/uploads/2023/06/6-10.jpg</url></image>
      <image id="11"><url>https://mi-site.com/wp-content/uploads/2023/06/6-11.jpg</url></image>
      <image id="12"><url>https://mi-site.com/wp-content/uploads/2023/06/6-12.jpg</url></image>
      <image id="13"><url>https://mi-site.com/wp-content/uploads/2023/06/6-13.jpg</url></image>
    </images>
  </property>
  <property>
    <images>
      <image id="1"><url>https://mi-site.com/wp-content/uploads/2023/06/6-1.jpg</url></image>
      <image id="2"><url>https://mi-site.com/wp-content/uploads/2023/06/6-2.jpg</url></image>
      <image id="3"><url>https://mi-site.com/wp-content/uploads/2023/06/6-3.jpg</url></image>
      <image id="4"><url>https://mi-site.com/wp-content/uploads/2023/06/6-4.jpg</url></image>
      <image id="5"><url>https://mi-site.com/wp-content/uploads/2023/06/6-5.jpg</url></image>
      <image id="6"><url>https://mi-site.com/wp-content/uploads/2023/06/6-6.jpg</url></image>
      <image id="7"><url>https://mi-site.com/wp-content/uploads/2023/06/6-7.jpg</url></image>
    </images>
  </property>
  <property>
    <images>
      <image id="1"><url>https://mi-site.com/wp-content/uploads/2023/06/6-1.jpg</url></image>
      <image id="2"><url>https://mi-site.com/wp-content/uploads/2023/06/6-2.jpg</url></image>
      <image id="3"><url>https://mi-site.com/wp-content/uploads/2023/06/6-3.jpg</url></image>
      <image id="4"><url>https://mi-site.com/wp-content/uploads/2023/06/6-4.jpg</url></image>
      <image id="5"><url>https://mi-site.com/wp-content/uploads/2023/06/6-5.jpg</url></image>
      <image id="6"><url>https://mi-site.com/wp-content/uploads/2023/06/6-6.jpg</url></image>
    </images>
  </property>
</root>

Спасибо за любую помощь.
 
PHP:
<?php
$xml = simplexml_load_string(file_get_contents('input.xml'));
$properties = $xml->xpath('/root/property');
foreach ($properties as $property) {
    $childElements = $property->children();
    $index = 1;
    foreach ($childElements->images->image as $i => $image) {
        $image->addAttribute('id', $index++);
    }
}
$xml->asXML('output.xml');
?>
 
Имя входящего файла изменен на мой, пробовал прописыват полный путь к xml файлу, без результатно.
Лог ошибок error_log - Все чисто.

В чем может быть ошибка?
Еще раз спасибо за вашу помощь.

В название выполняющего файла РНР был цифровой символ (test2.php), убрал "2" = (test.php), все заработало.
Большое спасибо kSM за помощь.
 
Последнее редактирование модератором:
В название выполняющего файла РНР был цифровой символ (test2.php), убрал "2" = (test.php), все заработало.
Большое спасибо kSM за помощь.
странно, в первые слышу чтобы скрипт с цифрой в имени отказывался выполняться)
 
Статус
В этой теме нельзя размещать новые ответы.
Назад
Сверху