如何从 XML 文件中删除节点作为命令行?

Fel*_*lix 4 xml sed

我有一个包含</w:rPr>多次标记的 xml 文件。它是这样使用的

<w:rPr><w:rFonts w:ascii="Symbol" w:hAnsi="Symbol" w:hint="default"/></w:rPr>
Run Code Online (Sandbox Code Playgroud)

然而,标签本身之间的内容有时是不同的。莫不是用sed的方式或其他东西删除之间的一切<w:rPr></w:rPr>,然后这两个标签呢?

相关的命名空间

xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
Run Code Online (Sandbox Code Playgroud)

以及文件本身的一部分(格式化的、有效的 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>
Run Code Online (Sandbox Code Playgroud)

Gil*_*not 11

当然,这是(一个合适的 XML 解析器)和他的朋友,就像这样:

xmlstarlet ed -L \
              -N w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" \
              -d '//w:rPr' file.xml
Run Code Online (Sandbox Code Playgroud)

一些解释:

  • -L即时编辑文件例如sed -i
  • -N如果需要,设置XML 命名空间
  • -d删除节点匹配xpath表达式

查看 xmlstarlet edit --help

TL; 博士

永远不要将用于此任务!

每次使用sedforhtml或 时xml,都会杀死一只小猫

理论 :

根据编译理论,不能使用基于有限状态机的正则表达式来解析 XML/HTML 。由于 XML/HTML 的层次结构,您需要使用下推自动机并使用YACC 等工具操作LALR语法。

realLife©®™日常工具:

您可以使用以下方法之一:

xmllint通常默认使用libxml2, xpath1安装

xmlstarlet可以编辑、选择、转换...默认没有安装,xpath1

xpath通过 perl 的模块 XML::XPath, xpath1 安装

xidel xpath3

saxon-lint我自己的项目,包装@Michael Kay 的 Saxon-HE Java 库 xpath3

或者您可以使用高级语言和适当的库,我认为:

lxml( from lxml import etree)

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

检查这个例子

DOMXpath检查这个例子


检查:使用带有 HTML 标签的正则表达式

在此处输入图片说明