首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >DomDoc/SimpleXML/XSLT:解析以向元素的每个唯一元素子元素添加自动递增的id属性

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

Stack Overflow用户
提问于 2011-07-27 15:34:46
回答 2查看 812关注 0票数 4

我已经对此进行了一段时间的故障排除,而且我对编程还有点陌生。即使当我发现一个错误,它是很难找到如何纠正它。现在,我正试图弄清楚我是如何使用xpath错误的,因为有人告诉我我使用了xpath错误。如果我做错了什么,我希望有人能告诉我我做错了什么,特别是迭代。这是我在这个项目上工作的最后一晚,如果可以的话,我真的想完成它。所以我真的需要帮助。下面是我使用的代码,附带注释:

代码语言:javascript
运行
复制
$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文件:

代码语言:javascript
运行
复制
<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>

期望产出:

代码语言:javascript
运行
复制
<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>

代码语言:javascript
运行
复制
<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文档的现实世界摘录,以更改数据结构:

代码语言:javascript
运行
复制
<?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>

所需输出示例的真实示例:

代码语言:javascript
运行
复制
<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>

代码语言:javascript
运行
复制
<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>

--更新--存在两个元素共享相同条目ID的问题

在另一个XML文档中,

代码语言:javascript
运行
复制
<entry id="1">
  <word>blue</word>
  <word>beryl</word>
  <word lang="SP">azul</word>
</entry>

我希望输出是

数据表:

代码语言:javascript
运行
复制
<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>

中介表:

代码语言:javascript
运行
复制
<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>
EN

Stack Overflow用户

发布于 2011-07-27 16:50:56

为了澄清,您正在尝试获取一个输入XML文档,使用XSL/T将其转换为另一个(格式不同的) xml文档,然后获取结果XML并将其存储在MySQL数据库中?

我对堆栈溢出很陌生,所以我不知道如何在原来的帖子中添加注释。

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6847111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档