in reply to Re^2: LibXML setNodeName error
in thread LibXML setNodeName error
I remember doing battle with XML::LibXML in a similar situation a few years ago. IIRC, what I found back then is that the problem is namespaces - note how the document originally has only one namespace (http://www.w3.org/1999/XSL/Transform) with only one namespace prefix (xsl). What you're asking to do is move the node into a completely different namespace, specifically in this case the default one. I remember not being able to find the appropriate combinations of API calls to achieve this so that the resulting XML looked clean, and playing around by adding $element->setNamespace(...); to choroba's code seems to confirm this, the best I can do so far is <foo xmlns="...">. It seemed that once a node has been created in a namespace, it's stuck there you can't get it back into the default namespace cleanly. Back then I ended up writing a routine to create a new node in the new namespace. Now since this was a while ago, it's possible I missed the appropriate API function, but even now going through the XML::LibXML docs and Googling a bit I still don't see an obvious way.
<update2> Caveat: The below will completely ignore any other attributes in the element being replaced, so for example if you've got <xsl:element namespace=... or use-attribute-sets, that will be ignored and lost! </update2>
Here's what my workaround would look like (based in part on choroba's code):
use warnings; use strict; use XML::LibXML; my $xml = <<'END_XML'; <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Tr +ansform"> <xsl:element name="foo"> <xsl:value-of select="bar"/> </xsl:element> </xsl:stylesheet> END_XML my $doc = XML::LibXML->load_xml(string => $xml) or die; my $xpc = XML::LibXML::XPathContext->new($doc); for my $el ($xpc->findnodes('//xsl:element')) { my $newel = $doc->createElement($el->getAttribute('name')); $newel->appendChild($_) for $el->childNodes; $el->replaceNode($newel); } print $doc; __END__ <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" versi +on="1.0"> <foo> <xsl:value-of select="bar"/> </foo> </xsl:stylesheet>
Update: Made description more accurate.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: LibXML setNodeName error
by geddie2001 (Novice) on Jun 26, 2017 at 17:47 UTC | |
|
Re^4: LibXML setNodeName error
by geddie2001 (Novice) on Jun 26, 2017 at 20:07 UTC | |
by haukex (Archbishop) on Jun 26, 2017 at 20:32 UTC | |
by geddie2001 (Novice) on Jun 26, 2017 at 20:49 UTC | |
by haukex (Archbishop) on Jun 26, 2017 at 20:59 UTC | |
by Anonymous Monk on Jun 27, 2017 at 14:53 UTC | |
| |
|
Re^4: LibXML setNodeName error (updated)
by Anonymous Monk on Jun 27, 2017 at 15:56 UTC |