首页 > 解决方案 > 从包含 vs 代码注释的 xml 文件中删除块

问题描述

我有这样的 xml 文件(10k 行):

<?xml version="1.0" encoding="UTF-8"?>
<Translations xmlns="http:...">
    <customApplications>
        <label><!-- Pricing Notifications --></label>
        <name>TEAM_Tesla</name>
    </customApplications>
    <customApplications>
        <label><!-- CRM --></label>
        <name>TEAM_Tender</name>
    </customApplications>
    <customApplications>
        <label>Actualization Portal</label>
        <name>Actualization_Portal</name>
    </customApplications>

我想删除包含注释的块(不仅是注释部分)


期望的输出:

<?xml version="1.0" encoding="UTF-8"?>
<Translations xmlns="http:...">
    <customApplications>
        <label>Actualization Portal</label>
        <name>Actualization_Portal</name>
    </customApplications>

标签: javaxmlvisual-studio-code

解决方案


XSLT 喜欢

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

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

</xsl:stylesheet>

将删除具有注释节点作为后代的根元素的任何子元素。您可以使用https://docs.oracle.com/javase/8/docs/api/javax/xml/transform/Transformer.html在 Java 中运行 XSLT 。


推荐阅读