我有一个包含标签</w:rPr>
多次的xml文件。这样使用
<w:rPr><w:rFonts w:ascii="Symbol" w:hAnsi="Symbol" w:hint="default"/></w:rPr>
但是,标签本身之间的内容有时会有所不同。是否可以使用sed或其他方法删除<w:rPr>
和之间的所有内容</w:rPr>
,然后再删除两个标签?
相关名称空间
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
以及文件本身的一部分(格式化的有效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>
当然,这是xmlstarlet(适当的XML解析器)和他的朋友xpath的任务,如下所示:
xmlstarlet ed -L \
-N w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" \
-d '//w:rPr' file.xml
-L
编辑文件对飞象sed -i
-N
设置XML命名空间(如果需要)-d
删除匹配xpath
表达式的节点查看 xmlstarlet edit --help
请,永远不要使用sed来完成这项任务!
每当你使用sed
的html
还是xml
,你杀了一只小猫咪
根据编译理论,不能使用基于有限状态机的正则表达式来解析XML / HTML 。由于XML / HTML的层次结构,您需要使用下推式自动机并使用YACC之类的工具来处理LALR语法。
您可以使用以下之一:
xmllint通常在默认情况下与libxml2
xpath1一起安装
xmlstarlet可以编辑,选择,转换...默认情况下未安装,xpath1
通过perl的模块XML :: XPath,xpath1安装的xpath
xidel xpath3
saxon-lint我自己的项目,@ Michael Kay的Saxon-HE Java库xpath3的包装器
python的lxml
(from lxml import etree
)
Perl中的XML::LibXML
,XML::XPath
,XML::Twig::XPath
,HTML::TreeBuilder::XPath
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句