Solved

delete nodes from xml using xpath

Posted on 2011-09-23
4
315 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 108

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

910 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

16 Experts available now in Live!

Get 1:1 Help Now