XPath Not Working

A seemingly simple XPath expression to modify some nodes in web.xml during maven build process using XMLTask from oops consultancy didn’t work. Article Little Back Corners nailed the issue — namespace declarations in xml file. I have used local-name() function to workaround the issue. Below is an example:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
  <servlet>
   <display-name>Log4j configuration Servlet</display-name>
   <servlet-name>log4j</servlet-name>
   <servlet-class>it.openutils.log4j.Log4jConfigurationServlet</servlet-class>
 </servlet>

  <servlet-mapping>
  	<servlet-name>log4j</servlet-name>
  	<url-pattern>/chlog4j/*</url-pattern>
  </servlet-mapping>
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

XSLT to output servlet-class node value of servlet log4j:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">

<xsl:value-of select="/*[local-name()='web-app']/*[local-name()='servlet']/*[local-name()='servlet-name' and text()='log4j']/../*[local-name()='servlet-class']"></xsl:value-of>

</xsl:template>

</xsl:stylesheet>
Advertisements