0

my XML is -

<app>
 <data>
    <lang>en</lang>
 </data>
</app>

I want to check, whether 'lang' tag/element is present or not. I am using below code to check it, but I think it is not working. -

<xsl:if test="app/data/lang">
<xsl:call-template name="xyz" /></xsl:if>

I am not able to understand what I am doing wrong here. I referred this page of stackoverflow. Please suggest any other way do solve this. Thanks in advance!

Answer -

After doing many changes I got one solution. I made a small change to solve this issue. I added '/' before 'app'.

<xsl:if test="/app/data/lang">
  <xsl:call-template name="xyz" />
</xsl:if>

Thanks everyone for sharing their solutions.

Nitesh
  • 1,477
  • 5
  • 23
  • 34
  • Nitesh, Can u tell after checking what you have to do. Actually there is nothing wrong in if condition, prb with call-template only?? – Lucifer May 08 '18 at 10:59
  • actually I am new to xslt. We have one old app. as per code, we are trying to call "xyz" template. – Nitesh May 08 '18 at 11:06
  • My guess would be that you're not thinking about context. The condition `test="app/data/lang"` will work provided that the context item is the document node (the parent of the app element). My guess is that it isn't. – Michael Kay May 08 '18 at 14:59

4 Answers4

0

You can use XSLT checking . Choose and When is good for doing it Below is the sample code

   <xsl:choose>
          <xsl:when test="app/data/lang">
                 *write what ever you want*
          </xsl:when>
   </xsl:choose>
  • @Nitesh can you provide expeted output .. ?and How are you applying XSLT over XML –  May 08 '18 at 11:12
  • I am calling one xsl template – Nitesh May 08 '18 at 11:22
  • I want to call that template only when I have "lang" tag. – Nitesh May 08 '18 at 11:23
  • If you are already doing a for loop or any other before applying this choose and when .. we must take into consideration . While writing the when test condition For ex: ` –  May 08 '18 at 11:27
  • @Nitesh I tried this loop works fine .. loops there something do with call-templeate may be with name which we specifi –  May 08 '18 at 11:30
  • Thank you for your valuable answer and your time. I have added my answer. – Nitesh May 09 '18 at 06:15
  • @Nitesh Looks you were facing same problem as , i explained in previous comment . Like in For-each and When class , like node we must take from where we left .. As per my example as Above –  May 09 '18 at 10:33
0

E.g. in a template matching app you can use <xsl:if test="data/lang">.

The tag mentioned must exist, although it may be empty.

A suspected reason that your script failed is that the current object may be such that app/data/lang path finds nothing.

For a working example see http://xsltransform.net/bEJaog9

To get the "Absent" case, change e.g. the name of source lang tag to any other.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
0

Based on my understanding giving this answer, consider this might be your XML

<Data>
    <Record>
     <AddInfo>
     </AddInfo>
    </Record>
</Data>

you need to check AddInfo tag based on you have to call-template, then this would be the XSL need to form

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>

  <xsl:template match="Data/Record">
      <xsl:choose>
        <xsl:when test="AddInfo">Present</xsl:when>
        <xsl:otherwise>Not Present</xsl:otherwise>
      </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

Based on present or not present, you provide your condition. More stuff related to call-template Link here

Lucifer
  • 784
  • 2
  • 7
  • 20
0

I made a small change to solve this. I added '/' before 'app'.

<xsl:if test="/app/data/lang">
  <xsl:call-template name="xyz" />
</xsl:if>
Nitesh
  • 1,477
  • 5
  • 23
  • 34