php – DomDoc / SimpleXML / XSLT:解析为元素的每个唯一元素子元素添加自动递增id属性

我一直在对此进行故障排除,我对编程很陌生.即使我发现错误,也很难弄清楚如何纠正错误.现在,我试图弄清楚我是如何使用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>

我也看到过许多非常相似的问题,我从他们那里收集了一些资源并阅读:

> http://www.ibm.com/developerworks/xml/library/x-xmlphp1/index.html
> http://www.php.net/manual/en/simplexmlelement.addattribute.php
> http://www.learn-xslt-tutorial.com/
> http://msdn.microsoft.com/en-us/library/ms256103.aspx
> http://php.net/manual/en/class.domdocument.php

这些是最有用的链接:

> http://www.capcourse.com/Library/NormalizingXML/Part1.html
> http://forums.tizag.com/showthread.php?t=17821

我发现这些问题的应用都没有能够产生我想要达到的结果.但是,例外是capcourse.com链接.这是针对一个渐进的CS观众,看起来他们正在做同样的事情,除了他们使用的ID不是自动增量.他们使用的算法非常复杂,他们根本没有评论他们的代码.由于某种原因,他们在命名空间中使用了命名空间,即使它是我能找到的最接近的命名空间,但我无法轻易地重现它.

更新

来自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>

–UPDATE–存在两个元素共享相同条目ID的问题

在我的另一个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>

最佳答案 – 更新

假设像这样的xml:

<catalog>
  <cd>
    <entry id="1">
      <word>blue</word>
      <word>beryl</word>
      <word lang="SP">azul</word>
    </entry>
  </cd>
  <cd>
    <entry id="2">
  ...

试试这个:

$super = array();
$url = "original.xml";
if ($xml = @simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA)) {
  foreach($xml->cd as $cd) {
     foreach ($cd->entry as $entry) {
      $id = (string)$entry['id'];
        foreach($entry->word as $word) {
            $lang = isset($word['lang']) ? (string)$word['lang'] : 'EN';
            $super[$id][$lang][] = (string)$word;
        }
     }
  }
}

显示使用:

print "<pre>";
print_r($super);
print "</pre>";

注意:这是另一种方法,基本上是你在使用xml对象时需要理解的,一般来说,对于数组更多的是你可以通过创建基于父级的结构化层次结构来存储数据 – >儿童;在这种情况下,我已经创建了一个像这样的数组$super [$id] [$lang] [] =(string)$word;其中$id是$lang的父项,它是$word的父项,它们分别是两者的子项;这会生成一个这样的数组:

Array
(
    [1] => Array
        (
            [EN] => Array
                (
                    [0] => blue
                    [1] => beryl
                )

            [SP] => Array
                (
                    [0] => azul
                )

        )
      ...

其他要考虑的事情是,

>如何获取匹配标签的属性,如id或lang,在我的示例中,我使用$entry [‘id’]但$cd-> entry [‘id’]也有效.
>如何将xml-dom-object转换为有效的字符串,以便可以将其重用为数组索引或值,如(string)$word

从我的例子可以看出:

<catalog>
  <cd>
    <ent_seq>1</ent_seq>
    <title>For Your Love</title>
    <artist>
    ...

试试这个

$super = array();
$url = "original.xml";
if ($xml = @simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA)) {
  $xml_array = @json_decode(@json_encode($xml), 1);
  foreach ($xml_array['cd'] as $val) {
  $key = $val['ent_seq'];
    if (is_array($val)) {
      foreach ($val as $k1 => $v1) {
        if (is_array($v1)) {
          switch ($k1) {
            case 'artist':
              foreach ($v1 as $k2 => $v2) {
                if (is_array($v2)) {
                  foreach ($v2 as $v3) {
                    $super[$k2][$key] = $v3;
                  }
                }
                else {
                  $super[$k2][$key] = $v2;
                }
              }
              break;
          }
        }
        else {
          switch ($k1) {
            case 'title':
              $super[$k1][$key] = $v1;
              break;
          }
        }
      }
    }
  }
}

显示迭代数组的结果,如下所示:

foreach( $super as $key => $val) {
  echo "<table id='{$key}'>\n";
   foreach($val as $key2 => $val2) {
    echo "<$key id='$key2'> " . $val2." </$key>\n";
    }
    echo "</table>\n";                

}

为了更好地查看数组结构,您可以像这样打印它:

print "<pre>";
print_r($super);
print "</pre>";

这将显示如下数组:

Array
(
    

=> Array ( [1] => For Your Love [2] => Splish Splash [3] => How Great Thuo Art [4] => Big Willie style [5] => Empire Burlesque ) [name] => Array ( [1] => Eric Clapton [2] => Roberto Carlos [3] => Elvis Presley [4] => Will Smith [5] => Boby Rockhammer ) [nickname] => Array ( [1] => Slowhand [2] => The King [3] => The King of Rock 'n Roll ) [band] => Array ( [4] => Will Smith [5] => Bob Dylan and Boby Rockhammer ) )

注意:你可以看到我使用了switch-case,因为你的xml-tags并不总是相同的一致性,并且在某些情况下它们具有相似的名称,如< company>< name>和< artist>< name&gt ;;你可以创建自己的案例. 但是就像现在一样,它可以很好地处理你想要抓取的字段,就像在例子中一样.

点赞