Haitham Salah
Haitham Salah

Reputation: 107

Using conditions in XSL

I am working with some XML files where in some instances the HName node is available in some files while missing from the others. Here are two examples:

Xml #1:

<HSegment>
<Code>ABC</Code> 
</HSegment>

Xml #2:

<HSegment>
<Code>ABC</Code> 
<HName>JW BEACH</HName>  
</HSegment>

I am trying to parse the xml files using two conditions:

  1. Process the value within the data field if it's available.
  2. Otherwise inserting ‘NULL’ if it's not available.

The XSL code that i am working with below adds ‘NULL’ to both cases:

<Des>
<xsl:choose>
<xsl:when test="//PNR/SList/HSegment/HName='HName'">
</xsl:when>
<xsl:otherwise>
<xsl:text>NULL</xsl:text> 
</xsl:otherwise>
</xsl:choose>
</Des>

Thanks in advance!

Upvotes: 1

Views: 57

Answers (1)

Wayne
Wayne

Reputation: 60414

Modify the presence check to look like this:

//PNR/SList/HSegment/HName

In context:

<xsl:choose>
    <xsl:when test="//PNR/SList/HSegment/HName">
        <!-- do whatever -->
    </xsl:when>
    <xsl:otherwise>
        <xsl:text>NULL</xsl:text> 
    </xsl:otherwise>
</xsl:choose>

Upvotes: 1

Related Questions