0

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.

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?

Starting with this:

<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#">
    <rdf:Description rdf:about="info:fedora/cfai:EB01a004">
        <fedora:isMemberOfCollection rdf:resource="info:fedora/cfai:collection"/>
    </rdf:Description>
</rdf:RDF>

And I would like it to looks like this:

<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#">
    <rdf:Description rdf:about="info:fedora/cfai:EB01a004">
        <element>These are new elements that I paste in.</element>
        <element>This is another, basically just a string of elements</element>
    </rdf:Description>
</rdf:RDF>

I've got it loaded with simple_xml_loadfile, I've got the namespaces cooking with registerXPathNamespace, 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.

Any help would be greatly appreciated.

ghukill
  • 1,136
  • 17
  • 42
  • 1
    Are you trying to work with this as RDF? If you are, working with it as XML is very prone to error. Since the same RDF graph can be serialized in XML in many different ways, trying to write XML-based methods to manipulate it is very difficult to do right. For instance, in the example you've given (which I recognize is probably just a mock-up example), since there's no XML base defined, you've got triples of the form ` <...element> "These are new elements that I just paste in"` where `<...element>` could be something relative to the file that things are stored… – Joshua Taylor May 14 '13 at 23:56
  • … in, relative to the URL where you got the document, or whatever other best guess that the recipient can make. I haven't worked with RDF using PHP, but a quick Google search turns up some libraries, e.g., [EasyRDF](http://www.easyrdf.org/). – Joshua Taylor May 15 '13 at 00:00
  • I am working with RDF, but in kind of a unique circumstance here. I am making blanket changes to RDF in XML form; basically adding/removing/editing triples in bulk, which then get index in a Mulgara datastore. But I hear you, that's how I'd interact with RDF most of the time. – ghukill May 15 '13 at 01:27
  • OK, glad to hear it. I've seen so many people do such brittle things by working with the XML instead of the RDF. I wish there were a counterpart to the ["can't parse HTML with regex" answer](http://stackoverflow.com/a/1732454/1281433) for "can't handle RDF with XML tools". So long as you know what you're doing though, best luck to you! – Joshua Taylor May 15 '13 at 02:16
  • Yeah, for sure. Thanks for the response. Ended up serializing the XML, using regex to find and replace those chunks, and it's working like a charm. – ghukill May 15 '13 at 02:32

2 Answers2

0

You could use unset() to remove a node:

unset($xml->node);

And then add an element:

$node = $xml->addChild('node');
$node->addChild('title', 'i'm a node');
$node->addChild('title2', 'another node');
  • It's a strange circumstance, but I need to retain the node because it has a unique attribute that I won't know while scripting. I could grab the attribute, then unset the node, then create it again. But I'd love to just empty it - remove all the children - then dump a string of XML in there if possible... – ghukill May 14 '13 at 23:55
  • @ghukill: [PHP Simple XML remove all Children](http://stackoverflow.com/a/9453956/367456) and [Insert XML into a SimpleXMLElement](http://stackoverflow.com/a/14831397/367456) (the later is an answer, I dunno if there is an *exact* existing question so far for that) - These two things show you code-example how to do both with SimpleXML in PHP: Removed all child-elements and how to import an XML fragment (as string) into an existing SimpleXML Element. This should be better than taking the regex. Let me know if you still have questions. – hakre May 15 '13 at 14:28
0

Ended up serializing the XML, then using regex to find and replace everything inside the element. Kind of a strange way of going about it, but fits this scenario well. Thanks everyone for the suggestions though, helped move things along.

ghukill
  • 1,136
  • 17
  • 42