tags:

views:

312

answers:

3

I am trying to use the below code to check if my variable contains the word NOT_INTERFACED. So I am using the ends-with function.

But if it does I want to print the remaining word minus NOT_INTERFACED.

But I am getting an error, my java program is calling this template however I get an exception ends-with not found.

Am I doing something wrong syntax wise ?

<xsl:choose>
  <xsl:when test="ends-with($ID_FO, 'NOT_INTERFACED')">
    <xsl:value-of select="substring-before($ID_FO, 'NOT_INTERFACED')"/>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="$ID_FO"/>
  </xsl:otherwise>
</xsl:choose>

and the error is

COULD NOT FIND FUNCTION ENDS WITH. LOCATION: unknown
java.lang.NullPointerException

+1  A: 

EDIT: See my comment below,

I checked the XPATH 1.0 documentation and it does not include an ends-with function. You'll have to do the substring method I detailed below--that is if you are using XSLT 1.0 (which uses XPATH 1.0)

EDIT 2: Just so my comment has some nice syntax highlighting:

<xsl:when test="substring($ID_FO, string-length($ID_FO)-14) = '_NOT_INTERFACED'">
quadelirus
Ok, what version of XSLT are you using? I think ends-with might not be in 1.0 in which case you need to do a little more fancy footwork. Like substring($ID_FO, string-length($ID_FO)-15) = '_NOT_INTERFACED'... (check my indices because XSLT counts from 1 not 0 so I tend to make mistakes.
quadelirus
I'm pretty sure it is actually -14 not -15. Since the string length is the number of characters which would be 15 if the string was simply _NOT_INTERFACED and the indices start at 1 so you want to subtract 14. I have no idea why they thought it was good to change stuff up on us programmers and start with 1 instead of 0.
quadelirus
Did this fix it? Have you tried it?
quadelirus
+1  A: 

XSLT parsers are case sensitive - try using this:

<xsl:choose>
    <xsl:when test="ends-with($ID_FO, 'NOT_INTERFACED')">
      <xsl:value-of select="substring-before($ID_FO, 'NOT_INTERFACED')"/>
     </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$ID_FO"/>
    </xsl:otherwise>
</xsl:choose>

Also you had forgotten to close the xsl:when element after the test attribute.

Andrew Hare
guys... no type error from my end... What I may have typed here was just to explain the logic m using. I am not making any syntax error... the error I get is funtions ends-with not found :(....\Please help
Please post the actual code that is causing the error and the error you are seeing.
Andrew Hare
<xsl:choose> <xsl:when test="ends-with($ID_FO, 'NOT_INTERFACED')"><xsl:value-of select="substring-before($ID_FO, 'NOT_INTERFACED')"/></xsl:when><xsl:otherwise><xsl:text>garbage</xsl:text></xsl:otherwise></xsl:choose>and the error is COULD NOT FIND FUNCTION ENDS WITH.LOCATION: unknown java.lang.NullPointerException
Dire: you misunderstood the request. Please post the Java code you are using to try and run the xsl template against the xml document.
GaZ
java code is humongous to post here :(
@Dire - have you noticed that you can actually *edit* your own post? Please do not post question clarifications in comments. And please use *real* code right from the start. No-one can help you with syntactically incorrect mock-ups your made up "to show the logic".
Tomalak
A: 

I've already posted it as a comment in my answer over at your other question. Replace your entire <xsl:choose> construct with this one-liner:

<xsl:value-of select="
  substring-before(concat($ID_FO, 'NOT_INTERFACED'), 'NOT_INTERFACED')
" />

You are still not clear whether your string contains or ends with 'NOT_INTERFACED'.

If it ends with the substring you want to get rid of, use the above. If it contains it, you have your answer in the other question. I suggest having a second look there.

Tomalak