if-test on a variable (attribute value) from xml-parsing

J

John Nilsen

Hello, hope you can help me with this little problem.

Below is my JSP-code.
It parses a XML-file. The parsing works just fine, but i'm have a
problem with using "if".
I want to check the variabel ${attrTypeTerminId} and find outh if the
value is 0. But i don't know how to represent it....

So again, the code works fine, except from the if-section. What is the
propper syntax?



##########################################

<%@ page language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>

<HTML>
<BODY>

<c:import var="xml" url="tekstvarsel_define.xml"/>
<x:parse varDom="dom" xml="${xml}"/>

<x:forEach var="type" select="$dom/tekstvarsel/type">
<x:set var="attrTypeName"
select="string($type/@name)"/>
<p>Varseltype: <c:eek:ut value="${attrTypeName}"/>
<x:forEach var="termin" select="termin">
<x:set var="attrTypeTerminId" select="string($termin/@id)"/>
<br>&nbsp;&nbsp;Termin: <c:eek:ut
value="${attrTypeTerminId}"/>

<% if ( ${attrTypeTerminId} == 0 ) { %>
<p>YEEES: It has 0 as a value</p>
<% } else { %>
<p>NOOOO: It has <c:eek:ut value="${attrTypeTerminId}"/> as a
value</p>
<% } %>

<x:forEach var="sted" select="sted">
<x:set var="dataTypeTerminSted"
select="string($sted/text())"/>
<br>&nbsp;&nbsp;&nbsp;&nbsp;Sted: <c:eek:ut
value="${dataTypeTerminSted}"/>
</x:forEach>
</x:forEach>
</p>
</x:forEach>
<c:remove var="xml"/>
<c:remove var="dom"/>

</BODY>
</HTML>
 
T

Tobias Schierge

John said:
<x:set var="attrTypeTerminId" select="string($termin/@id)"/>

<% if ( ${attrTypeTerminId} == 0 ) { %>
<p>YEEES: It has 0 as a value</p>
<% } else { %>
<p>NOOOO: It has <c:eek:ut value="${attrTypeTerminId}"/> as a
value</p>
<% } %>

This will not work, it is not valid code, you should do it like this:

<c:choose>
<c:when test="${attrTypeTerminId == "someValue"}">
..
</c:when>
<c:eek:therwise>
..
</c:eek:therwise>
</c:choose>

The other posibility which is not very:

<% if ( "0".equals(request.getAttribute("attrTypeTerminId")) { %>
..
<% } else { %>
..
<% } %>

Adjust the variable scope to fit your needs.

I would suggest not mixing jstl and scriptlet code, try to do it all in with
the Taglibs.

HTH, regards,

Tobias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top