Forum OpenACS Q&A: Deleting XML nodes using ns_xml

Hi,

I have a rather simple question, but I couldn't solve is searching Google, so here it goes: How do a delete an XML node (and it's siblings) using ns_xml?

Thanks and a happy new year to you...
Steffen

Collapse
Posted by Dave Bauer on
ns_xml does not allow manipulation of the XML document. You can add nodes, but deleting is not implemented.

This is another reason to move to using tdom istead of ns_xml.

Collapse
Posted by Steffen Tiedemann Christensen on
:(

Is there a reason for this lacking? I've found ns_xml to be rather helpful in my current project and only need the delete feature to complete it.

Is there any "nice" way to go around this? Someone who have invented some brilliant regsub?

Thanks,
Steffen

Collapse
Posted by Steffen Tiedemann Christensen on
I fixed up a temporary solution. It's not pretty but it gets the job done:

set node_name [ns_xml node name $node_id]
ns_xml node setattr $node_id "delete_this_node_now" "true"

set xml_data [ns_xml doc render $mod(doc_id)]

regsub -all -- "<\/${node_name}>" $xml_data \x81 xml_data
regsub -all -- "<${node_name}\[^>\]+delete_this_node_now=\"true\"\[^>\]*>\[^\x81\]*\x81" $xml_data "" xml_data
regsub -all -- "\x81" $xml_data "" xml_data
Collapse
Posted by Jamie Rasmussen on
Have you tried the version of nsxml from SourceForge (1.5)? It is newer than the OpenACS version. According to the source code comments you can delete a node with:
ns_xml node delete node_id
I haven't used it, so I don't know if it breaks anything in OpenACS (there were API changes).