JSTL does not work with XML namespace yet! 

Joined:
08/13/2009
Posts:
110

July 22, 2010 16:52:16    Last update: July 22, 2010 16:53:26
JSTL XML tags does not work with XML namespace. Take this JSP:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
<html>
<head>
<title>JSTL XML Namespace</title>
</head>

<body>
<!-- without namespace -->
<x:parse var="newsWithoutNamespace">
    <News>
	<item id="1">
	    <header>I made this up</header>
	</item>
	<item id="2">
	    <header>This is a true story</header>
	</item>
    </News>
</x:parse>
<h2>Today's news without XML Namespace</h2>
<ul>
    <x:forEach var="story" select="$newsWithoutNamespace/News/item">
	<li><x:out select="header"/></li>
    </x:forEach>
</ul>

<!-- Same XML with namespace -->
<x:parse var="newsWithNamespace">
    <News xmlns="http://the.big.rumor.mill/feed">
	<item id="1">
	    <header>I made this up</header>
	</item>
	<item id="2">
	    <header>This is a true story</header>
	</item>
    </News>
</x:parse>
<h2>Today's news with XML Namespace</h2>
<ul>
    <x:forEach var="story" select="$newsWithNamespace/News/item">
	<li><x:out select="header"/></li>
    </x:forEach>
</ul>

</body>
</html>


The second list will be empty due to the namespace declaration.

In fact, the JSR change log clearly states:
JSTL 1.0 does not currently provide adequate support for XML namespaces in XPath expressions. Collaboration between the JSP, JAXP, and JSTL specs is necessary to come up with the proper solution. This will therefore be addressed in the next major revision of JSTL.

As a workaround, you can use XPath functions as described here:
http://pro-programmers.blogspot.com/2008/04/jstl-xparse-not-working-for-elements.html

More Info:
JSR #52 Summary
Share |
| Comment  | Tags