简体   繁体   中英

Can not execute Xpath with Intellij Idea

I try to execute xpath expression in web.xml in Intellij Idea

Xpath Expression

//param-value[email]

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app  version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>ServletName</servlet-name>
        <servlet-class>com.ServletDemo</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ServletName</servlet-name>
        <url-pattern>/Demo1</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>email</param-name>
        <param-value>admin@email.com</param-value>
    </context-param>
</web-app>

And I have no results. Moreover Idea highlight param-value as it does not exist and when I clik ctrl+space I see no tags! I try use this plugin in another project and another xml - it works, but in that case it is not.

Any ideas? May be I missed some configuration?

UPDATED

I remove attributes from web-app tag and expression start works. But Why?

Firstly, the param-value element is in a namespace, but your XPath expression is looking for it in no namespace. Typically you want an expression such as //j:param-value where the prefix j is bound to the namespace http://java.sun.com/xml/ns/j2ee - you'll have to check the IntelliJ documentation to see how to do that.

Secondly, your param-value elements do not have a child element called email . If you want the value of the associated param-name element to be "email", that would be

//j:context-param[j:param-name='email']/j:param-value

Try following:

  1. Open evaluate Xpath window (ctrl + shift + a: Evaluate Xpath)
  2. Click Advanced button
  3. Add uri with prefix (on the screenshot below I have added Maven POM namespace with "pom" prefix)
  4. Try to evaluate "/pom:project" xpath on any pom.xml (notice that now IntelliJ autocompletes pom elements)

Intellij评估表达式截图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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