1

我有一个 web.xml,其中定义了大约 10 个 servlet,并定义了一些基本配置,如下所示:

    <servlet>
    <servlet-name>dummyServlet</servlet-name>
    <servlet-class>com.abc.Servlet</servlet-class>
    <init-param>
        <param-name>target</param-name>
        <param-value>foo.com</param-value>
    </init-param>
    <init-param>
        <param-name>log</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>dummyServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

        <servlet>
    <servlet-name>dummyServlet2</servlet-name>
    <servlet-class>com.abc.Servlet2</servlet-class>
    <init-param>
        <param-name>target</param-name>
        <param-value>foo.com</param-value>
    </init-param>
    <init-param>
        <param-name>log</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>dummyServlet2</servlet-name>
    <url-pattern>/dummy</url-pattern>
</servlet-mapping>

        <servlet>
    <servlet-name>actualServlet</servlet-name>
    <servlet-class>com.abc.ActualServlet</servlet-class>
    <init-param>
        <param-name>target</param-name>
        <param-value>foo.com</param-value>
    </init-param>
    <init-param>
        <param-name>log</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>dummyServlet</servlet-name>
    <url-pattern>/actual</url-pattern>
</servlet-mapping>

现在,根据某些条件,从我的 ant 目标中,我需要删除两个 dummy-servlet 及其映射,但需要保留实际 servlet 及其映射。有人可以建议最好/最简单的方法吗?我应该使用ant <replace>xmltask 还是其他一些功能?替换整个 web.xml 不是一种选择。

4

1 回答 1

2

我冒昧地在提供的 XML 中添加了一个根元素以使其格式正确。

请通过 XSLT 尝试以下解决方案。

XSLT 使用所谓的身份转换模式。

您需要为您的真实 XML 修改两个模板:并提供要删除的虚拟 Servlet 列表。

虚拟 Servlet 之外的所有其他内容都将保持不变。

请记住,XML 区分大小写,包括实际值。

输入 XML

<root>
    <servlet>
        <servlet-name>dummyServlet</servlet-name>
        <servlet-class>com.abc.Servlet</servlet-class>
        <init-param>
            <param-name>target</param-name>
            <param-value>foo.com</param-value>
        </init-param>
        <init-param>
            <param-name>log</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dummyServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>dummyServlet2</servlet-name>
        <servlet-class>com.abc.Servlet2</servlet-class>
        <init-param>
            <param-name>target</param-name>
            <param-value>foo.com</param-value>
        </init-param>
        <init-param>
            <param-name>log</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dummyServlet2</servlet-name>
        <url-pattern>/dummy</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>actualServlet</servlet-name>
        <servlet-class>com.abc.ActualServlet</servlet-class>
        <init-param>
            <param-name>target</param-name>
            <param-value>foo.com</param-value>
        </init-param>
        <init-param>
            <param-name>log</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dummyServlet</servlet-name>
        <url-pattern>/actual</url-pattern>
    </servlet-mapping>
</root>

XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="servlet[servlet-name=('dummyServlet','dummyServlet2')]">
    </xsl:template>
        <xsl:template match="servlet-mapping[servlet-name=('dummyServlet','dummyServlet2')]">
    </xsl:template>
</xsl:stylesheet>

输出 XML

<root>
  <servlet>
    <servlet-name>actualServlet</servlet-name>
    <servlet-class>com.abc.ActualServlet</servlet-class>
    <init-param>
      <param-name>target</param-name>
      <param-value>foo.com</param-value>
    </init-param>
    <init-param>
      <param-name>log</param-name>
      <param-value>true</param-value>
    </init-param>
  </servlet>
</root>
于 2021-06-01T15:21:07.460 回答