XSLT或PHP超链接删除元素

A snippet of my XML file:

<list>
  <info>
    <title>My Title</title>
    <blah>My Blah</blah>
    <derp>My Derp</derp>
  </info>
  <info>
    <title>Second Title</title>
    <blah>My Blah2</blah>
    <derp>My 2nd Derp</derp>
  </info>
  <info>
    <title>123</title>
    <blah>444</blah>
    <derp>zzz</derp>
  </info>
</list>

Snippet of my XSLT file:

<xsl:for-each select="info">
<tr>
    <td><xsl:value-of select="title" /></td>
    <td><xsl:value-of select="blah" /></td>
    <td><xsl:value-of select="derp" /></td>
</tr>
</xsl:for-each>

This basically displays my title, blah, and derp of the info element in their own row in a neat table when I use XSLTProcessor in PHP.

But anyways, what i'm trying to do is have an X button in each of my rows of elements in my table. And when this X button is clicked the entire corresponding <info> element is deleted from the XML file. Problem is I have NO idea where to start about doing this. I don't know if it's possible to rewrite in the XML file using XSL or do I have to do it in PHP.

Thanks for the help.

How to remove an element in an XML document by position.

As written you can do that with an Xpath expression to query the elements to remove and then removing them.

The following is a quick PHP example of doing that, it's a bit extended because it also removes the text (in your document the whitespace) after the element as a bonus. This again shows how to use the position predicate:

$doc = new DOMDocument();
$doc->loadXML($xml);
$xp = new DOMXPath($doc);

$expression = '

    /list/info[2]   |  /list/info[2]/following-sibling::text()[1]
                                                                    ';
//   the element,          the whitespace after the element,
//   numbered              again numbered. just to clean up a bit
//                         and to give another example

$delete = $xp->query($expression);
foreach($delete as $element) {
    $element->parentNode->removeChild($element);
}

echo $doc->saveXML();

The result then looks like (Online Demo):

<?xml version="1.0"?>
<list>
  <info>
    <title>My Title</title>
    <blah>My Blah</blah>
    <derp>My Derp</derp>
  </info>
  <info>
    <title>123</title>
    <blah>444</blah>
    <derp>zzz</derp>
  </info>
</list>

I hope this is helpful.

I was writing about numbered predicates lately as well in an answer to select only one node and move on php. Maybe it sheds some more light.