简体   繁体   English

删除XML中特定标签之间的标签(Notepad ++)

[英]Delete tags between specific tags in XML (Notepad++)

I already used the search function but I didn't find a answer to my question. 我已经使用了搜索功能,但没有找到问题的答案。

I have a XML structure like the following (example): 我有一个类似下面的XML结构(示例):

<Task name="1B">
 <Person type ="XX" name="YY" height="ZZ"/>
 <Person type ="XX" name="YY" height="ZZ"/> 
 <Person type ="XX" name="YY" height="ZZ"/> 
 </Task>

 <Task name="1C">
 <Person type ="XX" name="YY" height="ZZ"/>
 <Person type ="XX" name="YY" height="ZZ"/> 
 <Person type ="XX" name="YY" height="ZZ"/> 
 </Task>

Now I want to delete via Notepad++ the tag with Name "1B" and all the tags between the open and closing tag. 现在,我想通过Notepad ++删除名称为“ 1B”的标签以及打开和关闭标签之间的所有标签。 Is there a way in Notepad? 记事本中有办法吗? I already tried with RegEx Pattern but I didn't get the right way. 我已经尝试过RegEx Pattern,但没有找到正确的方法。

Using regex with HTML is strongly discouraged since that leads to many issues and unnecessary questions. 强烈建议不要将regex与HTML一起使用,因为这会导致许多问题和不必要的问题。 See RegEx match open tags except XHTML self-contained tags . 请参阅RegEx匹配开放标签,XHTML自包含标签除外 Using XSLT to transform XML is the tool you really need here. 使用XSLT转换XML是您真正需要的工具。

Create a UTF8 encoded file with a remove_xml_tag.xsl sample name and paste this into it: 使用remove_xml_tag.xsl示例名称创建一个UTF8编码的文件,并将其粘贴到其中:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="Task[@name='1B']"/>
</xsl:stylesheet>

The XSL processes each node and attribute ( node()|@* ) and when it encounters a Task element with name attribute equal to 1B ( "Task[@name='1B']" ) it just does not write it into the output. XSL处理每个节点和属性( node()|@* ),并且遇到name属性等于1BTask元素( "Task[@name='1B']" )时,它不会将其写入输出中。

Then run the XML Tools plugin -> XSL Transformation . 然后运行XML Tools插件-> XSL Transformation You will see: 你会看见:

在此处输入图片说明

Click the ... button on the right and browse the XSL file. 单击右侧的...按钮,然后浏览XSL文件。

Click the Transform button. 单击变换按钮。

A fallback solution in case you have a malformed XML that will work only if you have no nested Task nodes: 一个后备解决方案,以防您拥有格式错误的XML,该格式仅在没有嵌套的Task节点时才有效:

<Task\s+name="1B">[^<]*(?:<(?!/Task>)[^<]*)*</Task>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM