Solved

delete nodes from xml using xpath

Posted on 2011-09-23
4
321 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
ID: 36588783
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
ID: 36588825
Actually, REGEX is greedy so this might be a better pattern

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

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 36590887
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
ID: 36598321
Exactly what I needed, thank you!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

808 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