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

Discussion in 'Java' started by John Nilsen, Jan 24, 2004.

  1. John Nilsen

    John Nilsen Guest

    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>



    --
    John Nilsen
     
    John Nilsen, Jan 24, 2004
    #1
    1. Advertising

  2. John Nilsen wrote:

    > <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
    --
    <http://www.schierge.de/tobias/>
     
    Tobias Schierge, Jan 25, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    4,383
    Joe Kesselman
    Aug 2, 2006
  2. Replies:
    8
    Views:
    532
  3. =?UTF-8?b?5byg6Z+h5q2m?=
    Replies:
    3
    Views:
    735
    DeepthiShri
    Oct 23, 2006
  4. Bob
    Replies:
    1
    Views:
    1,303
    Mark Fitzpatrick
    Feb 8, 2008
  5. Erik Wasser
    Replies:
    5
    Views:
    500
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page