Pooja
Pooja

Reputation: 495

How to check the tag has value or not in xslt

I am using xslt. If my input string is like

<entry colsep="0" rowsep="0" />
<entry colsep="0" rowsep="0">Acid suppressant</entry>

I need to check of the entry tag contains any value. if not, i need to replace it like

<entry colsep="0" rowsep="0">...</entry>

How to check this in XSLT.

Thanks Pooja

Upvotes: 1

Views: 161

Answers (1)

Tim C
Tim C

Reputation: 70648

If you want to match an element with no text value, you can simply do this

<xsl:template match="entry[not(text())]" >

You can then add your code to copy the element, but add a default value at the same time.

Combining this with the identity transform, gives the following XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="entry[not(text())]">
      <entry>
         <xsl:copy-of select="@*"/>
         <xsl:text>Default Value</xsl:text>
      </entry>
   </xsl:template>

   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

When applied to the following XML:

<entries>
   <entry colsep="0" rowsep="0" /> 
   <entry colsep="0" rowsep="0">Acid suppressant</entry> 
</entries>

The following is output

<entries>
   <entry colsep="0" rowsep="0">Default Value</entry>
   <entry colsep="0" rowsep="0">Acid suppressant</entry>
</entries>

Upvotes: 2

Related Questions