2022-04-23

Display empty XML data node as text message

I am fairly new to xml and I'm having a weird problem with templates. For empty data nodes, instead of displaying a blank space where the data should be, I want to display a message like 'None Found' or 'No Data' on an html page. I have done both an xsl:choose statement and 2 xsl:if statements with no success. I already know this particular node is empty, I just want to show a message instead of a blank space. The Otherwise or False statements just don't seem to run at all. Please help!! What am I doing wrong here?

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>

<xsl:template match="/">

  <HTML>
    <HEAD>

      <TITLE></TITLE>
    </HEAD>
 <BODY style="font-family:Arial;font-size:12pt">
..
...

<div class="float-child" style="margin-bottom: 25px">
   <xsl:apply-templates select="root/dobs"/>
</div>


..

.
 </BODY>
  </HTML>
</xsl:template>

<xsl:template match="dobs">
<div><span style="font-weight:bold">DOB(s):</span>
<xsl:variable name="dobHasData" select="boolean(normalize-space(dob))" />

<xsl:value-of select="$dobHasData"/> <!-- displays as 'false' -->
<xsl:if test="$dobHasData = 'true'">
<xsl:value-of select="dob"/>
</xsl:if>

<xsl:if test="$dobHasData = 'false'">
<span style="font-weight:normal"> <xsl:value-of select="$dobHasData"/> </span> <!-- this line will not display -->
</xsl:if>
</div>
</xsl:template>

As requested, here is an example of an XML file I'm trying to transform

<?xml version="1.0" encoding="utf-8"?>
<Root>
    <Node1>
        <Node1>name</Node1>
        <Node1>phone</fields>
        <Node1>address</fields>
        <Node1>email</fields>
    </Node1>
    <dobs/> <--- This is the empty node I'm trying to manage
    <id>22</id>
    <Node4>
        <firstName>Homer</firstName>
        <lastName>Simpson</lastName>
        <address>1122 Smithers Lane</address>
        <city>Springfield</city>
        <zip>88773</zip>
        <state>MA</state>
    </Node4>
</Root>


No comments:

Post a Comment