perl - XML ( LibXML ) : Easiest way to Replace an xhtml:h1 tag with an xhtml:h2 tag with the same contents -
Lt; Div & gt; With tag & lt; H1 & gt; , & Lt; H2 & gt; e.t.c.
Now, I'm going to remove divs from there and they have the title & lt; Xhtml: h2 / & gt; Which I & lt; Xhtml: h1 / & gt; I want to change to start with (I.e.: H2HA, H3, H, H2, H4, H.3, etc.).
How can I do this by using XML :: LibXML?
Even I have so far:
foreach my $ h_idx (2 .6) {foreach my $ h_tag ($ scene_xpc-> Finder (qq {/ Xhtml: h $ h_idx})) {my $ replacement = $ h_tag- & gt; Clonnod (1); #Today: How do I set the name? $ Replacement- & gt; Set $ h_tag- & gt; Replace node ($ replacement); As you've guessed from your variable name, $ scene_xpc}}
, you need to define XML :: LibXML :: XPathContext , where the xhtml namespace is defined after it You can use that context as you can specify the normal XML :: LibXML object, except that you can now specify the namespace registered in your element and attribute names. I'm not clear why you are cloning each node, as far as I can tell, you have to do setNodeName on each node. It is necessary that all the local names are: the element will maintain its original namespace. This small program shows this idea.
Use strict; Use warnings; Use 5.014; # Use XML :: LibXML for non destructive replacement; My $ filename = 'xhtml.html'; My $ xml = XML :: libXML- & gt; new; My $ doc = $ xml- & gt; Parse_file ($ filename); My $ xpc = XML :: LibXML :: XPathContext- & gt; New ($ doc); $ Xpc- & gt; Register N ('xhtml', 'http://www.w3.org/1999/xhtml'); For my $ head ('h2' .. 'h6') ($ my $ newhead = $ head = ~ / s / (\ d) / $ 1-1 / er; My $ node ($ xpc- & gt; SearchNode ( "$ Node- & gt; setNodeName ($ newhead);}} print $ doc- & gt; toString;
Comments
Post a Comment