xml如果名字 space 在 xml 中被提及,starlet 将无法工作

xmlstarlet is not working if name space is mentioned on the xml

我是运行这个命令,而且有效

xmlstarlet sel -t -v '/web-app/filter/filter-name/text()' ./sampleWeb.xml

回复

HeaderSecurityFilter
httpHeaderSecurity
httpHeaderSecurity
ContentSecurityPolicyFilter
ReferrerPolicyFilter

sampleWeb.xml

的内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
    <filter>
        <filter-name>HeaderSecurityFilter</filter-name>
        <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HeaderSecurityFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>httpHeaderSecurity</filter-name>
        <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
        <init-param>
            <param-name>antiClickJackingOption</param-name>
            <param-value>SAMEORIGIN</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>httpHeaderSecurity</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

    <filter>
        <filter-name>httpHeaderSecurity</filter-name>
        <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>hstsEnabled</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>hstsMaxAgeSeconds</param-name>
            <param-value>31556927</param-value>
        </init-param>
        <init-param>
            <param-name>hstsIncludeSubDomains</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter>
        <filter-name>ContentSecurityPolicyFilter</filter-name>
        <filter-class>YourPackagePath.ContentSecurityPolicyFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>ContentSecurityPolicyFilter</filter-name>
        <url-pattern>/*</url-pattern>
     </filter-mapping>
     <filter>
        <filter-name>ReferrerPolicyFilter</filter-name>
        <filter-class>my.package.ReferrerPolicyFilter</filter-class>
     </filter>
     <filter-mapping>
       <filter-name>ReferrerPolicyFilter</filter-name>
       <url-pattern>/*</url-pattern>
     </filter-mapping>

</web-app>

但是如果将 sampleWeb.xml 更改为这个(包括 xmlns、xsi 和版本)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
                      https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
  version="6.0">
    <filter>
        <filter-name>HeaderSecurityFilter</filter-name>
        <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
    </filter>

同样的命令不起作用

xmlstarlet sel -t -v '/web-app/filter/filter-name/text()' ./sampleWeb.xml

编辑

我阅读了 How to declare XPath namespaces in xmlstarlet? 问题,但这并没有解决 tomcat web.xml 的问题。这个问题也没有提到 -t 和 -v 选项。因此,这不是一个重复的问题。在您决定删除一个完全正确且独特的问题之前,请再检查一遍。与其他解析相比,xmlstartet 使用起来有点复杂。最终 -N 和 -u 对我的问题不起作用。

您必须考虑命名空间,因为 xmlns= 在此元素及其所有子元素上定义了默认命名空间。然后传给xmlstarlet,像这样:

xmlstarlet sel -N jak="https://jakarta.ee/xml/ns/jakartaee" -t -v '/jak:web-app/jak:filter/jak:filter-name/text()' sampleWeb.xml

现在输出应该是一样的。