【发布时间】:2014-10-24 06:13:01
【问题描述】:
请帮我写代码。 @hakre
我有以下 XML 文件。
<?xml version="1.0"?>
<Results>
<Recordset setCount="3">
<Record setEntry="0">
<TI>Test-1</TI>
<MO>Mo-1</MO>
<AU>One</AU>
<AU>Two</AU>
<AU>three</AU>
<JF>OK</JF>
<JT />
</Record>
<Record setEntry="1">
<TI>Test-2</TI>
<MO>Mo-2</MO>
<AU>One</AU>
<AU>Two</AU>
<AU>Three</AU>
<AU>Four</AU>
<AU>Five</AU>
<AU>Six</AU>
<AU>Seven</AU>
<JF />
<JT />
</Record>
<Record setEntry="2">
<TI>Test31</TI>
<MO>Mo-3</MO>
<AU>One</AU>
<AU />
<JF />
<JT />
</Record>
</Recordset>
</Results>
我希望它像下面的 XML 文件。
<?xml version="1.0"?>
<Results>
<Recordset setCount="3">
<Record setEntry="0">
<TI>Test-1</TI>
<MO>Mo-1</MO>
<AU>One; Two; Tree</AU>
<JF>OK</JF>
<JT />
</Record>
<Record setEntry="1">
<TI>Test-2</TI>
<MO>Mo-2</MO>
<AU>One; Two; Three; Four; Five; Six; Seven</AU>
<JF />
<JT />
</Record>
<Record setEntry="2">
<TI>Test31</TI>
<MO>Mo-3</MO>
<AU>One</AU>
<JF />
<JT />
</Record>
</Recordset>
</Results>
这些是我需要完成的事情。
将 SIMILAR XML 标记中的值连接成一个标记,用“;”分隔。 (给定示例中的“AU标签”)
保留空白 XML 标记。
PHP 创建并保存名为“SECOND.XML”的第二个 XML 文件。
【问题讨论】:
-
{XML} foreach ($xml->xpath('//*[not() and not(preceding -sibling::)]') as $firstLeaf) { $followingWithSameName = 'following-sibling::*[name(.) = name(preceding-sibling::*[last()])]'; // 改变第一个叶子的文本 $firstLeaf[0] = implode(', ', $firstLeaf->xpath(".|$followingWithSameName")); // 删除所有后续同名叶子 foreach ($firstLeaf->xpath( $followingWithSameName) as $leaf) { unset($leaf[0]); } } echo $xml->asXML();
-
@Sanda 编辑您的问题并将该代码添加到其中。