QA Testing
QA Testing

Reputation: 57

xslt for validating an element attribute a node or not

I have a XML where I need to select specific elements from all the nodes. The structure of actual XML looks like this-

<host> 
<node> 
  <type>fruit1</type>
  <value>10</value>
</node>
<node>
  <type>fruit2</type>
  <value>20</value>
</node>
<node>
  <type>fruit3</type>
  <value xsi:type="valueList">
      <listValues>
        <value>
            <value>30</value>
            <code>abc</code>
        </value>
      </listValues>
  </value>
</node>
<node>
  <type>fruit4</type>
  <value>40</value>
</node>  
<node>
  <type>fruit5</type>
  <value>50</value>
</node>
</host>

I have to choose the elements <type> and <value> from all the nodes in a tabular format. The 3rd node has list values for the element <value> and for this element the value should be 'true'. The expected output XML is as follows -

<html>
<body>
<table border="1"> 
<tr>
  <td>fruit1</td>
  <td>10</td>
</tr>
<tr>
  <td>fruit2</td>
  <td>20</td>
</tr>
<tr>
  <td>fruit3</td>
  <td>true</td>
</tr>
<tr>
  <td>fruit4</td>
  <td>40</td>
</tr>
<tr>
  <td>fruit5</td>
  <td>50</td>
</tr>
</table>  
</body>  
</html>

The XSLT written is as follows-

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
  <xsl:for-each select="host/node">
    <tr>
      <td><xsl:value-of select="type" /></td>
      <xsl:choose>
        <xsl:when test = "//node/value/listValues/value/value != null">
            <td>true</td>
        </xsl:when>
        <xsl:otherwise>
            <td><xsl:value-of select="value" /></td>
        </xsl:otherwise>
      </xsl:choose>
    </tr>
  </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

But I am unable to get the desired XML output format. Any help would be a great plus.

Upvotes: 0

Views: 507

Answers (1)

Rupesh_Kr
Rupesh_Kr

Reputation: 3435

Change

<xsl:when test = "//node/value/listValues/value/value != null">

to

<xsl:when test = "value/listValues/value/value != ''">

Upvotes: 1

Related Questions