In .xsl, take a range value like "130-210", and determine if "86" or "458" is within that numeric range

1.8k Views Asked by At

I'm parsing an .xml file like:

<xml>
  <normalRange>100-200</normalRange>
  <value>83</value>
</xml>

In an .xls stylesheet I need to display a value indicating whether the value is within the normalRange, below it, or above it.

This is a very common problem when displaying Human Readable results from the CCR (Continuity of Care Record in Healthcare HL7 messaging) xml document.

1

There are 1 best solutions below

1
Grzegorz Szpetkowski On BEST ANSWER
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
    <xsl:variable name="value" select="/xml/value"/>
    <xsl:variable name="low" select="substring-before(/xml/normalRange, '-')"/>
    <xsl:variable name="high" select="substring-after(/xml/normalRange, '-')"/>

    <xsl:choose>
        <xsl:when test="$value &lt; $low">
            <output>below</output>
        </xsl:when>
        <xsl:when test="$value &gt; $high">
            <output>above</output>
        </xsl:when>
        <xsl:otherwise>
            <output>within</output>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

Note that element name "xml" is reserved by XML 1.0 standard, so it's probably good idea to avoid it.