Solved

delete nodes from xml using xpath

Posted on 2011-09-23
4
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 look for a specific file type in a local or remote server directory using PHP.

717 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