Search code examples
xmlxslthl7

XSLT replace string with an int


I'm working on a project where a received HL7 message is validated by XSLT. The issue I'm having is where the HL7 message field for 'Gender' (PID.8) comes in as a string, but the database has the gender field as an int.

I'm trying to make it so that when the HL7 field for gender is being read, it converts to an int value. The result I'm looking for, when the PID.8 field shows 'M', it converts it to 1. However the result I'm getting currently is that when the PID.8 field shows 'M', it pushes 'M1'.

<xsl:variable name="value" select="//PID/PID.8" />
<xsl:value-of select="$value" />
<xsl:choose>
    <xsl:when test="contains($value, 'NA')">
        <xsl:value-of select="0" />
    </xsl:when>
    <xsl:when test="contains($value, 'M')">
        <xsl:value-of select="1" />
    </xsl:when>
    <xsl:when test="contains($value, 'F')">
        <xsl:value-of select="2" />
    </xsl:when>
    <xsl:when test="contains($value, 'UN')">
        <xsl:value-of select="3" />
    </xsl:when>
</xsl:choose>

Solution

  • It appears that you've left in <xsl:value-of select="$value" /> as the second line which is what is passing the "M" portion of "M1" in your example. If you comment that line out or remove it your code should generate the result you are seeking.

    I don't know enough about HL7, but I'd be tempted to check if the specifications for the message you are receiving lends itself to the use of an <xsl:otherwise> condition to add to the end of the <xsl:choose>.