Hello ,
um using xpath into jsp .
According to what I know that the following :

<xpath:set var="channel_name" select="channelName" />

returns a string .
Let's assume that the value of this string is "foo"
I need to compare this string with other string !!
like
if (var1=="foo") then
do smth

I have already used this :

<xpath:comapre(channel_name,"foo") />

but it doesn't work .

Please advice .

Dani AI

Generated

This thread is about comparing a string value obtained via an XPath-based JSP tag. extracted the value and tried a tag-based comparison; pointed toward using an XPath boolean test and noted a likely typo. The following adds practical, reliable ways to compare that value and quick checks to find why a comparison might fail.

If you can compare in Java (servlet or a small scriptlet), do a null-safe content comparison rather than reference equality. A common pattern is to put the literal on the left so nulls on the variable do not throw a NPE, and use the case-insensitive variant when appropriate. See the Java String API for details: String.equals.

If you prefer JSP-side logic, use JSTL/EL instead of raw scriptlets. The core tag library’s conditional tag with EL lets you compare string contents cleanly; check the JSTL docs for the exact tag usage on your container: JSTL core (Tomcat docs).

If you must compare inside an XPath expression, normalize the value first to avoid hidden whitespace and then use XPath string comparison and functions (for example, normalize-space). The W3C XPath spec documents string literals, equality, and string functions in detail: XPath 1.0 spec.

Quick debugging checklist

  • Render or log the raw extracted value and its length to reveal invisible whitespace or entities.
  • Trim or normalize whitespace before comparing.
  • Use a null-safe compare pattern in Java to avoid NPEs.
  • Confirm the taglib’s documentation for exact attribute/argument names and how it exposes values (EL variable, Java object, or XPath node).
  • If switching templating engines (Velocity, etc.), validate that the value is a plain string in that context before comparing.

These steps cover the usual causes (whitespace, encoding, nulls, wrong equality operator, or taglib syntax).

Recommended Answers

All 2 Replies

I've never seen the <xpath:set> or <xpath:compare> instructions before, so I have to assume those are custom functions you're using.

But a simple boolean xpath direct string comparison should work. If the value of your variable is 'foo' the xpath below results in true.

channel_name = 'foo'

Also in your code listed you have it spelled "comapre" instead of "compare" Could it be a typo?

Thanks iceandrews .
It seems that um going to switch to Velocity mean time .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.