XSL 模板不匹配

XSL Template Won't Match

提问人:robhyx 提问时间:10/13/2023 最后编辑:robhyx 更新时间:10/13/2023 访问量:36

问:

我有一个非常简单的 XSL,我正在尝试匹配一个 servlet 元素并添加一个子节点。实际上,我需要匹配一个特定的 servlet,但一次只匹配一件事......无论我使用什么模式,除了 *,模板都不会匹配。我已经尝试了有和没有命名空间。我还尝试了绝对路径和相对路径。无。我敢肯定,我在这里忽略了一些非常明显的东西。

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:jee="http://java.sun.com/xml/ns/javaee"
        xmlns="http://java.sun.com/xml/ns/javaee">
        
    <xsl:output method="xml" encoding="ISO-8859-1"/>

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

    <xsl:template match="servlet">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <init-param>
                <param-name>readonly</param-name>
                <param-value>true</param-value>
            </init-param>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

XML 中的相关部分(整个文档太大而无法发布)。它只是与Tomcat 8捆绑在一起的默认web.xml,但这里是:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1">

    <servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>listings</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>jsp</servlet-name>
        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
        <init-param>
            <param-name>fork</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>xpoweredBy</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>3</load-on-startup>
    </servlet>

<!--
    <servlet>
        <servlet-name>ssi</servlet-name>
        <servlet-class>
          org.apache.catalina.ssi.SSIServlet
        </servlet-class>
        <init-param>
          <param-name>buffered</param-name>
          <param-value>1</param-value>
        </init-param>
        <init-param>
          <param-name>debug</param-name>
          <param-value>0</param-value>
        </init-param>
        <init-param>
          <param-name>expires</param-name>
          <param-value>666</param-value>
        </init-param>
        <init-param>
          <param-name>isVirtualWebappRelative</param-name>
          <param-value>false</param-value>
        </init-param>
        <load-on-startup>4</load-on-startup>
    </servlet>
-->

<!--
    <servlet>
        <servlet-name>cgi</servlet-name>
        <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
        <init-param>
          <param-name>cgiPathPrefix</param-name>
          <param-value>WEB-INF/cgi</param-value>
        </init-param>
        <load-on-startup>5</load-on-startup>
    </servlet>
-->

    <!-- The mapping for the default servlet -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- The mappings for the JSP servlet -->
    <servlet-mapping>
        <servlet-name>jsp</servlet-name>
        <url-pattern>*.jsp</url-pattern>
        <url-pattern>*.jspx</url-pattern>
    </servlet-mapping>

    <!-- The mapping for the SSI servlet -->
<!--
    <servlet-mapping>
        <servlet-name>ssi</servlet-name>
        <url-pattern>*.shtml</url-pattern>
    </servlet-mapping>
-->

    <!-- The mapping for the CGI Gateway servlet -->

<!--
    <servlet-mapping>
        <servlet-name>cgi</servlet-name>
        <url-pattern>/cgi-bin/*</url-pattern>
    </servlet-mapping>
-->

</web-app>
XML XSLT XSLT-1.0

评论


答:

1赞 M. Rizzo 10/13/2023 #1

看起来您可能一直在 stackoverflow 或 Web 上查看其他一些示例。命名空间声明与尝试转换的 web.xml 不匹配。“http://java.sun.com/xml/ns/javaee”与“http://xmlns.jcp.org/xml/ns/javaee”不匹配

请尝试以下修改后的 XSLT:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:jee="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        exclude-result-prefixes="jee">
        
  <xsl:output method="xml" encoding="ISO-8859-1"/>

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

    <xsl:template match="jee:servlet">
        <xsl:copy>
            <!-- copy its children -->
            <xsl:apply-templates select="@* | node()"/>
            <!-- Add a new elements -->
              <init-param>
                  <param-name>readonly</param-name>
                  <param-value>true</param-value>
              </init-param>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

评论

0赞 robhyx 10/14/2023
啊,我明白了。是的,我举了几个不同的例子,其中一个来自另一个用户也试图操纵 web.xml。这有效,所以非常感谢。