Sed XML – Remove Nodes with Namespace via Command Line

sedxml

I have an xml file that contains the tag </w:rPr> several times. It is used like this

<w:rPr><w:rFonts w:ascii="Symbol" w:hAnsi="Symbol" w:hint="default"/></w:rPr>

However the content between the tag itself is sometimes different. Could there be a way to use sed or something other to delete everything between <w:rPr> and </w:rPr> and then both tags as well?

The relevant namespace

xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"

And part of the file itself (formatted, valid XML)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:numbering xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14">
  <w:abstractNum w:abstractNumId="0" w15:restartNumberingAfterBreak="0">
    <w:nsid w:val="FFFFFF89"/>
    <w:multiLevelType w:val="singleLevel"/>
    <w:tmpl w:val="CB2CEC0E"/>
    <w:lvl w:ilvl="0">
      <w:start w:val="1"/>
      <w:numFmt w:val="bullet"/>
      <w:pStyle w:val="Aufzhlungszeichen"/>
      <w:lvlText w:val="ï‚·"/>
      <w:lvlJc w:val="left"/>
      <w:pPr>
        <w:tabs>
          <w:tab w:val="num" w:pos="360"/>
        </w:tabs>
        <w:ind w:left="360" w:hanging="360"/>
      </w:pPr>
      <w:rPr>
        <w:rFonts w:ascii="Symbol" w:hAnsi="Symbol" w:hint="default"/>
      </w:rPr>
    </w:lvl>
  </w:abstractNum>

  <!-- ... -->

 <w:abstractNum w:abstractNumId="16" w15:restartNumberingAfterBreak="0">
    <w:nsid w:val="6F8046F9"/>
    <w:multiLevelType w:val="hybridMultilevel"/>
    <w:tmpl w:val="1F3A6CE4"/>
    <w:lvl w:ilvl="0" w:tplc="DE32BBA8">
      <w:start w:val="1"/>
      <w:numFmt w:val="lowerLetter"/>
      <w:lvlText w:val="%1)"/>
      <w:lvlJc w:val="left"/>
      <w:pPr>
        <w:ind w:left="682" w:hanging="567"/>
      </w:pPr>
      <w:rPr>
        <w:rFonts w:ascii="Arial" w:eastAsia="Arial" w:hAnsi="Arial" w:cs="Arial" w:hint="default"/>
        <w:spacing w:val="-1"/>
        <w:w w:val="100"/>
        <w:sz w:val="22"/>
        <w:szCs w:val="22"/>
        <w:lang w:val="de-DE" w:eastAsia="de-DE" w:bidi="de-DE"/>
      </w:rPr>
    </w:lvl>

    <!-- ... -->

    <w:lvl w:ilvl="8" w:tplc="E4341C34">
      <w:numFmt w:val="bullet"/>
      <w:lvlText w:val="•"/>
      <w:lvlJc w:val="left"/>
      <w:pPr>
        <w:ind w:left="7581" w:hanging="567"/>
      </w:pPr>
      <w:rPr>
        <w:rFonts w:hint="default"/>
        <w:lang w:val="de-DE" w:eastAsia="de-DE" w:bidi="de-DE"/>
      </w:rPr>
    </w:lvl>
  </w:abstractNum>

  <!-- ... -->

  <w:num w:numId="1">
    <w:abstractNumId w:val="15"/>
  </w:num>
  <w:num w:numId="2">
    <w:abstractNumId w:val="6"/>
  </w:num>

  <!-- ... -->

</w:numbering>

Best Answer

Sure, it's a task for (a proper XML parser) and his friend , like this:

xmlstarlet ed -L \
              -N w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" \
              -d '//w:rPr' file.xml

A bit of explanations :

  • -L edit the file on the fly like sed -i
  • -N set the XML namespace, if needed
  • -d remove nodes matching xpath expression

Check xmlstarlet edit --help

TL;DR

please, never ever use for this task !

Everytime you use sed for html or xml, you kill a kitty

theory :

According to the compiling theory, XML/HTML can't be parsed using regex based on finite state machine. Due to hierarchical construction of XML/HTML you need to use a pushdown automaton and manipulate LALR grammar using tool like YACC.

realLife©®™ everyday tool in a :

You can use one of the following :

xmllint often installed by default with libxml2, xpath1

xmlstarlet can edit, select, transform... Not installed by default, xpath1

xpath installed via perl's module XML::XPath, xpath1

xidel xpath3

saxon-lint my own project, wrapper over @Michael Kay's Saxon-HE Java library, xpath3

or you can use high level languages and proper libs, I think of :

's lxml (from lxml import etree)

's XML::LibXML, XML::XPath, XML::Twig::XPath, HTML::TreeBuilder::XPath

, check this example

DOMXpath, check this example


Check: Using regular expressions with HTML tags

enter image description here

Related Question