Search code examples
xsltxslt-groupingxslt-3.0

XSLT 3.0 extract transformed nodes as multiple documents


I have this XML structure

<doc>
  <Bundle>
    <entry>
      <Observation>
          <id value="o1-3" />
          <subject>
            <reference value="Subject/1" />
          </subject>
          <valueQuantity>
            <value value="400" />
            <unit value="U" />
          </valueQuantity>
          <referenceRange>
            <low>
              <value value="0" />
              <unit value="U" />
            </low>
            <high>
              <value value="45" />
              <unit value="U" />
            </high>
          </referenceRange>
        </Observation>
    </entry>
    <entry>
        <Observation>
          <id value="o8-3" />
          <subject>
            <reference value="Subject/1" />
          </subject>
          <valueQuantity>
            <value value="0.39" />
            <unit value="L" />
          </valueQuantity>
          <referenceRange>
            <low>
              <value value="0.14" />
              <unit value="L" />
            </low>
            <high>
              <value value="0.35" />
              <unit value="L" />
            </high>
          </referenceRange>
        </Observation>
    </entry>
  </Bundle>
  <Bundle>
    <entry>
        <Observation>
          <id value="o3-4" />
          <subject>
            <reference value="Subject/2" />
          </subject>
          <valueQuantity>
            <value value="10" />
            <unit value="U" />
          </valueQuantity>
          <referenceRange>
            <low>
              <value value="3" />
              <unit value="U" />
            </low>
            <high>
              <value value="30" />
              <unit value="U" />
            </high>
          </referenceRange>
        </Observation>
    </entry>
    <entry>
       <Observation>
          <id value="o15-4" />
          <subject>
            <reference value="Subject/2" />
          </subject>
          <valueQuantity>
            <value value="7.1" />
            <unit value="m" />
          </valueQuantity>
          <referenceRange>
            <low>
              <value value="3.5" />
              <unit value="m" />
            </low>
            <high>
              <value value="5.0" />
              <unit value="m" />
            </high>
          </referenceRange>
        </Observation>
    </entry>
  </Bundle>
</doc>

I am developing below mechanism:

  1. Interpret if the valueQuantity is deviated from the referenceRange, if yes, transform the entry
  2. Extract the Observation node grouped by Observation/subject as separate document.

A correctly interpreted Observation and extracted document is below:

<?xml version="1.0" encoding="UTF-8"?>
<Interpretation xmlns="http://intelli.org/interpretation">
  <Subject>Subject/1</Subject>
  <Observations>
    <id value="o1-3"/>
    <subject>
      <reference value="Subject/1"/>
    </subject>
    <valueQuantity>
      <value value="400"/>
      <unit value="U"/>
    </valueQuantity>
    <referenceRange>
      <low>
        <value value="0"/>
        <unit value="U"/>
      </low>
      <high>
        <value value="45"/>
        <unit value="U"/>
      </high>
  </referenceRange></Observations></Interpretation>

My XSLT:

<!-- Interpretation Starts -->
<xsl:template match="valueQuantity">
  <xsl:param name="value" as="xs:double*" select="value/@value" />
  <xsl:param name="low" as="xs:double*" select="following::referenceRange[1]/low/value/@value" />
  <xsl:param name="high" as="xs:double*" select="following::referenceRange[1]/high/value/@value" />
  <xsl:if test="$value lt $low or $value gt $high">
    <xsl:element name="Interpretation">
    </xsl:element>
  </xsl:if>
  <!-- Interpretation Ends -->
  <!-- Identity Transform -->
  <xsl:copy-of select="." />
  <!-- Extraction Starts: Locality? -->
  <xsl:for-each select="parent::Observation">
     <xsl:result-document include-content-type="no" href="/interpret&amp;extract/deviation/{concat('interpretation/', id/@value, '.xml')}">
      <xsl:copy-of select="." />
    </xsl:result-document>
  </xsl:for-each>
</xsl:template>

Solution

  • I guess (because you haven't explained it clearly) that you're trying to write all the entry/valueQuantity elements that have the same value for entry/subject/reference to the same output file. The spec doesn't allow that (for a number of reasons: the results would depend on order of execution, parallel execution would become very difficult, and the resulting XML document would have no outer wrapper element).

    Instead, do a separate pass over the input to generate this output file, using something like

    <xsl:for-each-group select="entry" group-by="subject/reference/@value">
       <xsl:result-document href="{...}">
          <wrapper>
             <xsl:copy-of select="current-group()"/>
          </wrapper>
       </xsl:result-document>
    </xsl:for-each-group>