Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP / XML - replace chunk of XML in middle of file
    text
    copied!<p>Okay, I'm a bit stumped. I'm more familiar with Python when manipulating XML, but I would like to use PHP in this instance if possible. </p> <p>Is there a relatively straightforward way to replace a chunk of text - all the children and values for a given node - in an XML file with another set of elements?</p> <p>Starting with this:</p> <pre><code>&lt;rdf:RDF xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:fedora="info:fedora/fedora-system:def/relations-external#" xmlns:myns="http://www.nsdl.org/ontologies/relationships#"&gt; &lt;rdf:Description rdf:about="info:fedora/cfai:EB01a004"&gt; &lt;fedora:isMemberOfCollection rdf:resource="info:fedora/cfai:collection"/&gt; &lt;/rdf:Description&gt; &lt;/rdf:RDF&gt; </code></pre> <p>And I would like it to looks like this:</p> <pre><code>&lt;rdf:RDF xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:fedora="info:fedora/fedora-system:def/relations-external#" xmlns:myns="http://www.nsdl.org/ontologies/relationships#"&gt; &lt;rdf:Description rdf:about="info:fedora/cfai:EB01a004"&gt; &lt;element&gt;These are new elements that I paste in.&lt;/element&gt; &lt;element&gt;This is another, basically just a string of elements&lt;/element&gt; &lt;/rdf:Description&gt; &lt;/rdf:RDF&gt; </code></pre> <p>I've got it loaded with <strong>simple_xml_loadfile</strong>, I've got the namespaces cooking with <strong>registerXPathNamespace</strong>, but I'm having an embarassingly hard time figuring out how to 1) remove nodes, and 2) insert my own chunk of text in their place.</p> <p>Any help would be greatly appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload