Я уже некоторое время устраняю неполадки, и я немного новичок в программировании. Даже когда я нахожу ошибку, очень сложно понять, как ее исправить. Сейчас я пытаюсь понять, как я неправильно использовал xpath, потому что кто-то сказал мне, что я неправильно использую xpath. Я надеюсь, что кто-то может дать мне толчок, сказав мне, что я делаю неправильно, особенно с помощью итераций, если я делаю что-то не так. Это мой последний вечер работы над этим проектом, и я действительно хочу закончить его, если смогу. Итак, я действительно мог бы использовать помощь. Вот код, который я использую, с комментариями:
$xml = @simplexml_load_file("original.xml"); //Loading the original file, dubbed original.xml.
$array_key_target_parent = count($xml->xpath('/doc/*'); //Puts all of the children of <doc> into an _iterable_ array.
$key_targets = foreach($array_key_target_parent;){
foreach($array_key_target_parent as $single_target){ // I tried foreach($array_key_target_parent[$i]). It doesn't work, so don't even go there.
$current_target = current($single_target);
count($xml->xpath('/doc/$current_target/*');
}
} */ ////Puts the targets for keying into iterable arrays. =>1 makes the array start from 1, so the id's will be right.
/* At this point, we have multiple elements that we want to key, each having a unique name. There's <element_type1a> and <element_type1b>, etc. We want each one to have its own id set. So, we have to embed iteration within iteration. */
foreach($key_target){ //This will ensure that every unique element that we want to key gets its key set.
{
$id = current($key_target=>1); //This allows us to reset the id to 1 (=>1), each time the key algorithm starts for a new element.
foreach($key_target as $id){ //I tried for($i=0, $key_target[$i]; $i>$key_target; $i++), and it didn't work, so don't even go there.
addAttribute('id', '$id');
}
} //Adds an 'id' attribute and a unique number to each target.
$xml->asXML("new.xml"); //saves the output as a new xml document, new.xml
У меня также есть общий XML-файл:
<doc>
<info_type1>
<element_type1a>not_unique_data</element_type1a>
<element_type1b>unique_data</element_type1b>
<element_type2a>not_unique_data</element_type2a>
<element_type2b>not_unique_data</element_type2b>
<element_type2c lang="fr">not_unique_data</element_type2c>
<!-- ... --->
<element_typeNxM>unique_data</element_typeNxM>
</info_type1>
<info_type2>
<element_type1a>repeat_data(info_type1_element1a)</element_type1a>
<element_type2a>not_unique_data</element_type2a>
</info_type2>
<!-- ... --->
<info_typeN>
<descendants></descendants>
</info_typeN>
</doc>
Желаемый вывод:
<datatables>
<table id="element_type1">
<element_type1a id="1">unique_data</element_type1a>
<element_type1b id="2">unique_data</element_type1b>
<!-- ... --->
<element_type1N id="M">unique_data</element_type1N>
</table>
<table id="element_type2">
<element_type2a id="1">unique_data</element_type2a>
<element_type2b id="2">unique_data</element_type2b>
<!-- ... --->
<element_type2N id="M">unique_data</element_type2N>
</table>
<table id="element_type2_fr">
<element_type2a lang="fr" id="1">unique_data</element_type2a>
<element_type2b lang="fr" id="2">unique_data</element_type2>
<!-- ... (there are five languages) --->
<element_type2N lang="fr" id="M">unique_data</element_type2N>
</table>
<!-- ... --->
<table id="element_typeN">
<descendants></descendants>
</table>
</datatables>
и
<intermediary_tables>
<table id="intermediary_table_type1xtype2">
<element id="1">
<type1ID>1</type1ID>
<type2ID>1</type2ID>
</element>
<element id="2">
<type1ID>1</type1ID>
<type2ID>2</type2ID>
</element>
<element id="3">
<type1ID>2</type1ID>
<type2ID>1</type2ID>
</element>
<element id="4">
<type1ID>2</type1ID>
<type2ID>2</type2ID>
</element>
<!-- ... --->
<element id="N">
<type1ID>M</type1ID>
<type2ID>Z</type2ID>
</element_type2N>
</table>
<table id="intermediary_table_typeMxtypeN">
<descendants></descendants>
</table>
</intermediary_tables>
Я также видел много очень похожих вопросов, и у меня есть некоторые ресурсы, которые я собрал у них и прочитал:
Это самые полезные ссылки:
И я обнаружил, что ни одно из применений вопросов не могло привести к результату, которого я пытаюсь достичь. Исключением является ссылка на capcourse.com. Он ориентирован на выпускников CS-аудитории, и кажется, что они делают то же самое, за исключением того, что идентификаторы, которые они используют, не являются автоинкрементными. Алгоритм, который они используют, чрезвычайно сложен, и они вообще не прокомментировали свой код. По какой-то причине они используют пространство имен в своем пространстве имен, и хотя это самое близкое, что я могу найти, я не могу воспроизвести его ни в малейшей степени.
Обновление
Реальная выдержка из документа XML, который я хотел бы проанализировать для изменения структуры данных:
<?xml version="1.0"?>
<!DOCTYPE catalog [
<!ELEMENT catalog (entry*)>
<!ELEMENT entry (ent_seq, country*, arist+, info?, title+)><!-- Entries consist of the name of the album, artist, and more information about the CD. Each entry must contain an artist and an album title. -->
<!ELEMENT ent_seq (#PCDATA)><!-- A unique numeric sequence, showing the entry number -->
<!ELEMENT title (#PCDATA)><!-- The title of the album/the album name. -->
<!ELEMENT artist (band+, name, nickname*)><!-- The name of the band, and if there was a famous artist, his name and nickname. Must contain a band element. -->
<!ELEMENT band (#PCDATA)><!-- The name of the band. -->
<!ELEMENT name (#PCDATA)><!-- The name of any famous artist in the band. -->
<!ELEMENT nickname (#PCDATA)><!-- The nickname of the popular artist that precedes the nickname element, from the band. -->
<!ELEMENT country (#PCDATA)><!-- Specifies countries where the album was released -->
<!ELEMENT company (name, country)><!-- Company/producer info. The company's name is in the name element, and the country where the company originated is in the country element. -->
<!ELEMENT name (#PCDATA)><!-- The name of the producer -->
<!ELEMENT country (#PCDATA)><!-- The country where the company does its primary business -->
<!ELEMENT year (#PCDATA)><!-- The year of the album's release -->
<!ELEMENT info (link*, bibl*)><!-- Additional info, including links and bibliography information -->
<!ELEMENT link (#PCDATA)><!-- Links where people can read more about the album -->
<!ELEMENT bibl (#PCDATA)><!-- Bibliography text about the artist -->
]>
<catalog>
<cd>
<ent_seq>1</ent_seq>
<title>For Your Love</title>
<artist>
<name>The Yardbirds</name>
<name>Eric Clapton</name>
<nickname>Slowhand</nickname>
</artist>
<country>USA</country>
<country>UK</country>
<company>
<name>Sweet Music</name>
<country>USA</country>
</company>
<year>1965</year>
<info>
<link>http://en.wikipedia.org/wiki/For_Your_Love</link>
</info>
</cd>
<cd>
<ent_seq>2</ent_seq>
<title>Splish Splash</title>
<artist>
<name>Roberto Carlos</name>
<nickname>The King</nickname>
</artist>
<country>USA</country>
<country>Brazil</country>
<country>Italy</country>
<company>
<name>Sweet Music</name>
<country>Brazil</country>
</company>
<year>1965</year>
</cd>
<cd>
<ent_seq>3</ent_seq>
<title>How Great Thuo Art</title>
<artist>
<name>Elvis Presley</name>
<nickname>The King</nickname>
<nickname>The King of Rock 'n Roll</nickname>
</artist>
<country>USA</country>
<country>Canada</country>
<country>UK</country>
<company>
<name>Felton Jarvis</name>
<country>USA</country>
</company>
<year>1965</year>
</cd>
<cd>
<ent_seq>4</ent_seq>
<title>Big Willie style</title>
<artist>
<band>Will Smith</band>
<name>Will Smith</name>
</artist>
<country>USA</country>
<company>Columbia</company>
<year>1997</year>
</cd>
<cd>
<ent_seq>5</ent_seq>
<title>Empire Burlesque</title>
<artist>
<band>Bob Dylan and Boby Rockhammer</band>
<name>Bob Dylan</name>
<name>Boby Rockhammer</name>
</artist>
<country>USA</country>
<country>India</country>
<company>Columbia</company>
<year>1985</year>
</cd>
<cd> <!-- Update part 1: New Entry -->
<ent_seq>6</ent_seq>
<title>Merry Christmas</title>
<title>White Christmas</title>
<artist>
<name>Bing Crosby</name>
<artist>
<country>USA</country>
<company>MCA Records</company>
<year>1995</year>
</cd> <!-- End update part 1-->
</catalog>
Реальный пример желаемой выходной выборки:
<datatable>
<table id="album title">
<title id="1">For your Love</title>
<title id="2">Splish Splash</title>
<title id="3">How Great Thuo Art</title>
<title id="4">Big Willie style</title>
<title id="5">Empire Burlesque</title>
<title id="6">Merry Christmas</title> <!-- Update part 2: New output -->
<title id="7">White Christmas</title> <!-- Update part 2: New output -->
</table>
<table id="Band Name">
<artist id="1">The Yardbirds</artist>
<artist id="2">Roberto Carlos</artist>
<artist id="3">Elvis Presley</artist>
<artist id="4">Will Smith</artist>
<artist id="5">Bob Dylan and Boby Rockhammer</artist>
<artist id="6"> <!-- Update part 2: New output -->
</table>
<table id="artist name">
<artist id="1">Eric Clapton</artist>
<artist id="2">Roberto Carlos</artist>
<artist id="3">Elvis Presley</artist>
<artist id="4">Will Smith</artist>
<artist id="5">Bob Dylan</artist>
<artist id="6">Boby Rockhammer</artist>
<artist id="7">Bing Crosby</artist> <!-- Update part 2: New output -->
</table>
<table id="nickname">
<nickname id="1">Slowhand</nickname>
<nickname id="2">The King</nickname>
<nickname id="3">The King of Rock 'n Roll</nickname>
</table>
</datatable>
и
<intermediarytable>
<table id="artist by band name">
<entry id="1">
<band_id>1</band_id>
<artist_id>1</artist_id>
</entry>
<entry id="2">
<band_id>2</band_id>
<artist_id>2</artist_id>
</entry>
<entry id="3">
<band_id>3</band_id>
<artist_id>3</artist_id>
</entry>
<entry id="4">
<band_id>4</band_id>
<artist_id>4</artist_id>
</entry>
<entry id="5">
<band_id>5</band_id>
<artist_id>5</artist_id>
</entry>
<entry id="6">
<band_id>5</band_id>
<artist_id>6</artist_id>
</entry>
<entry id="7">
<band_id>6</band_id>
<artist_id>7</artist_id>
</entry>
</table>
<table id="artist by nickname">
<entry id="1">
<artist_id>1</artist_id>
<nickname_id>1</artist_id>
</entry>
<entry id="2">
<artist_id>2</artist_id>
<nickname_id>2</nickname_id>
</entry>
<entry id="3">
<artist_id>2</artist_id>
<nickname_id>3</nickname_id>
</entry>
<entry id="4">
<artist_id>3</artist_id>
<nickname_id>3</nickname_id>
</entry>
</table>
</intermediarytable>
- ОБНОВЛЕНИЕ-- Существует проблема, при которой два элемента имеют одинаковый идентификатор записи
В другом XML-документе, который я имею,
<entry id="1">
<word>blue</word>
<word>beryl</word>
<word lang="SP">azul</word>
</entry>
и я хочу, чтобы вывод был
Таблицы данных:
<table id="en">
<word lang="en" id="0">blue</word>
<word lang="en" id="1">beryl</word>
</table>
<table id="sp">
<word lang="sp" id="0">azul</word>
</table>
Таблица посредников:
<table id="translation id">
<en_sp id="0"> <!-- en_sp means English-to-Spanish -->
<en>0</en>
<sp>0</sp>
</en_sp>
<en_sp>
<en>1</en>
<sp>0</sp>
</en_sp>
</table>