Solved

delete nodes from xml using xpath

Posted on 2011-09-23
4
312 Views
Last Modified: 2012-05-12
I'm searching for the best way to delete nodes from an xml file and I'm trying to avoid having to loop through the entire xml structure.  

Take a look at the code snippets.  Is there a way, in xpath, to remove all of the <item> nodes that have the attribute "delete='true'" so that I have a new XML structure with just the <item> nodes that I want?

It seems I should be able to do this using xpath in PHP, but I can't figure out the correct syntax.

Thank you!
Marc
<category id='1' title='category 1'>
  <item id='a' title='item a' delete='true' />
  <item id='b' title='item b' delete='false' />
</category>

Open in new window

<category id='1' title='category 1'>
  <item id='b' title='item b' delete='false' />
</category>

Open in new window

0
Comment
Question by:marcparillo
  • 2
4 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
As far as I recall, there is not way to delete nodes in XML - you simply either do not add them in originally or you ignore them when processing.

An alternative is to convert the XML to a string and use preg_replace like so

<?php

$d = "<?xml version='1.0' ?>
<test>
<category id='1' title='category 1'>
  <item id='a' title='item a' delete='true' />
  <item id='b' title='item b' delete='false' />
</category>
</test>
";

$pattern = '#(<item.*?delete=\'true\'.*?/>)#si';

$newXml = preg_replace( $pattern, '', $d );

echo htmlentities($newXml);

Open in new window

which produces

<?xml version='1.0' ?>
<test>
<category id='1' title='category 1'>
    <item id='b' title='item b' delete='false' />
</category>
</test>


If empty newlines are a problem after the replace then try changing the pattern to

$pattern = '#(<item.*?delete=\'true\'.*?/>\\n)#si';

Open in new window

0
 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
Actually, REGEX is greedy so this might be a better pattern

$pattern = '#(<item[^>]+delete=\'true\'[^>]+>)#si';
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
This appears to work, but throws a warning indicating there is a bug in PHP.
http://www.laprbass.com/RAY_xml_delete_nodes.php

As far as looping through the entire XML stricture, that is necessary.  You have to inspect every element.  XPath does this under the covers, but it still does it.
<?php // RAY_xml_delete_nodes.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA FROM THE POST AT EE
$xml = <<<XML
<category id='1' title='category 1'>
  <item id='a' title='item a' delete='true' />
  <item id='b' title='item b' delete='false' />
</category>
XML;

// CAPTION THE XML CORRECTLY
$xml = '<?xml version="1.0" ?>' . $xml;

// MAKE AN OBJECT
$obj = SimpleXML_Load_string($xml);

// VISUALIZE THE OBJECT
var_dump($obj);

// USE AN ITERATOR
$key = 0;
foreach ($obj as $item)
{
    if ($item["delete"] == 'true') unset($obj->item[$key]);
    $key++;
}

var_dump($obj);
$new = $obj->asXML();
echo htmlentities($new);

Open in new window

0
 
LVL 3

Author Closing Comment

by:marcparillo
Comment Utility
Exactly what I needed, thank you!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now