Friday, June 8, 2012

Matching Items in one list against another list in XSLT

This is an overdue explanation for how to do something in XSLT that I was supposed to write up a couple of weeks ago for the HL7 CCD Bluebutton project.

The challenge is that we have two lists:  One of observations (particular lab results) for a particular patient, and the other a set of lab results that mean something (e.g., blood type).  The challenge is to find the set of results from the first list that match any of the codes in the second list.

The key is setting up a test of an item against a node list.

If you have in one file:

<observation>
   ...
   <code code='interestingCode1' codeSystem='...'/> 
   ...
</observation>
...
<observation>

   ...
   <code code='interestingCode2' codeSystem='...'/> 
   ...
</observation>

And in another file you have a list of codes:

<RetrieveValueSetResponse ... >
  <ValueSet id="..." 
    displayName="An Interesting Code Set" version="20061023">
    <ConceptList xml:lang="en-US">
      <Concept code="interestingCode1" displayName="Code 1"
        codeSystem="..."/>
      <Concept code="interestingCode2" displayName="Code 2"
        codeSystem="..."/>
    </ConceptList>
  </ValueSet>
</RetrieveValueSet>



You can make the second file available through the document function:

<xsl:variable name='InterestingCodes' 
              select='document("interestingCodes.xml")'/>

And then select the observations using the following expression:

<xsl:for-each select='//cda:observation[cda:code/@code = $InterestingCodes//svs:Concept/@code]'>
  ...
</xsl:for-each>

Inside the for-each, you'd do something interesting with the matched coded.  If you just needed the first on, you could add a [1] to the end of the expression.

According to XSLT and XPath, this will work because:
If one object to be compared is a node-set and the other is a string, then the comparison will be true if and only if there is a node in the node-set such that the result of performing the comparison on the string-value of the node and the other string is true.
So, that's how you'd do it.

1 comment:

  1. Success!
    Thanks, that is one less item on the CCD to BlueButton list.

    ReplyDelete