0

Preface: There are many questions similar to this that I've scoured through profusely. Nothing can quite answer what I need to do.

Here is my data structure. (obviously simplified)

<Upload>
    <tickets>
        <ticket>
            <ticket_number>123</ticket_number>
            <clerk>001</clerk>
            <property>
                <item>
                    <item_number>1</item>
                    <jewelry_metal>GOLD</jewelry_metal>
                </item>
                <item>
                    <item_number>2</item>
                    <jewelry_metal>SILVER</jewelry_metal>
                </item>
            </property>
        </ticket>
        <ticket>
            <ticket_number>456</ticket_number>
            <clerk>001</clerk>
            <property>
                <item>
                    <item_number>1</item>
                    <jewelry_metal></jewelry_metal>
                </item>
            </property>
        </ticket>
    <tickets>
<Upload>

I need to check the first item in each ticket, and if the <jewelry_metal> tag is empty, delete the entire <ticket>. I'm open to any suggestions at this point.

4

2 回答 2

1

您最好的选择是使用 XPath 和 DomDocument。尝试这个:

<?php
$xmlstr = <<<XML
<?xml version='1.0'?>
<Upload>
    <tickets>
        <ticket>
            <ticket_number>123</ticket_number>
            <clerk>001</clerk>
            <property>
                <item>
                    <item_number>1</item_number>
                    <jewelry_metal>GOLD</jewelry_metal>
                </item>
                <item>
                    <item_number>2</item_number>
                    <jewelry_metal>SILVER</jewelry_metal>
                </item>
            </property>
        </ticket>
        <ticket>
            <ticket_number>456</ticket_number>
            <clerk>001</clerk>
            <property>
                <item>
                    <item_number>1</item_number>
                    <jewelry_metal></jewelry_metal>
                </item>
            </property>
        </ticket>
    </tickets>
</Upload>
XML;

$doc = new DOMDocument();
$doc->loadxml($xmlstr);

$xpath = new DOMXpath($doc);

foreach($xpath->query('//tickets/ticket/property/item[1]') as $key => $item) {
    if (empty($item->getElementsByTagName('jewelry_metal')->item(0)->nodeValue)) {
        $item->parentNode->removeChild($item);  
    }
}

echo $doc->saveXML();

输出将是:

<?xml version="1.0"?>
<Upload>
    <tickets>
        <ticket>
            <ticket_number>123</ticket_number>
            <clerk>001</clerk>
            <property>
                <item>
                    <item_number>1</item_number>
                    <jewelry_metal>GOLD</jewelry_metal>
                </item>
                <item>
                    <item_number>2</item_number>
                    <jewelry_metal>SILVER</jewelry_metal>
                </item>
            </property>
        </ticket>
        <ticket>
            <ticket_number>456</ticket_number>
            <clerk>001</clerk>
            <property>

            </property>
        </ticket>
    </tickets>
</Upload>

希望这可以帮助!

于 2013-05-22T17:46:54.397 回答
0

我建议您为此使用 Xpath 语言。让我们翻译:

我需要检查每张票中的第一项,如果<jewelry_metal>标签为空,则删除整个<ticket>

进入xpath:

//ticket[.//item[1]/jewelry_metal = ""]

这将查询所有要删除的元素。在这个问题中解释了要在 PHP 中使用 simplexml 运行这样的 xpath 查询,然后删除元素:

确保您已启用错误报告,因为您提供的有问题的 XML 已完全损坏。以防万一你没有注意到。

于 2013-05-24T17:22:49.283 回答