我试图通过PHP脚本来转换XML文件。我有以下结构:用PHP编辑XML结构
<?xml version="1.0"?>
<persons>
<person id="1">
<periods>3</periods>
<name id="John"/>
<times>
<time>
<day id="1"/>
<time>35:28</time >
<length>8000</length>
</time>
<time>
<day id="4"/>
<time>8:28</time >
<length>2000</length>
</time>
<time>
<day id="5"/>
<time>3:03</time >
<length>1000</length>
</time>
</times>
</person>
<person id="2">
<periods>3</periods>
<name id="James"/>
<times>
<time>
<day id="3"/>
<time>45:20</time >
<length>15000</length>
</time>
<time>
<day id="5"/>
<time>4:48</time >
<length>1500</length>
</time>
</times>
</person>
etc…
etc…
</persons>
每个人都有一个或多个时间元素。我想转换结构,因此每个人元素只有一个时间元素(如下面的结构)。
<?xml version="1.0"?>
<persons>
<person id="1">
<periods>3</periods>
<name id="John"/>
<times>
<time>
<day id="1"/>
<time>35:28</time >
<length>8000</length>
</time>
</times>
</person>
<person id="1">
<periods>3</periods>
<name id="John"/>
<times>
<time>
<day id="4"/>
<time>8:28</time >
<length>2000</length>
</time>
</times>
</person>
<person id="1">
<periods>3</periods>
<name id="John"/>
<times>
<time>
<day id="5"/>
<time>3:03</time >
<length>1000</length>
</time>
</times>
</person>
<person id="2">
<periods>2</periods>
<name id="James"/>
<times>
<time>
<day id="3"/>
<time>45:20</time >
<length>15000</length>
</time>
</times>
</person>
<person id="2">
<periods>2</periods>
<name id="James"/>
<times>
<time>
<day id="5"/>
<time>4:48</time >
<length>1500</length>
</time>
</times>
</person>
</persons>
有没有办法做到这一点?
那么,就克隆要复制的节点,改变你从中删除的时间节点(我用'DOMDocument'&有点xpath'本的',但你可以创建和XSLT为了这)。但是:您的原始结构似乎比您想要的输出更可行。是否有一个特殊的原因需要输出? – Wrikken
@Wrikken是的,有一个原因。我需要将其转换为JSON。它使JSON代码更加可行(对我而言)。 – Jorik