Solved

delete nodes from xml using xpath

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
str_replace not working in php script 4 25
php construct 5 24
google analytics in wordpress 4 31
How would I do this...? 2 27
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

813 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