Biztalk/XSLT - map belonging segments without any correlating

54 Views Asked by At

I have the following xml (HL7) message.

`<OBR>One</OBR>`
`<ZCT>Two</ZCT>`
`<OBR>Three</OBR>`
`<ZCT>Four</ZCT>`

I need to map these to another XML like this:

`<Number>`
`<One>One</One>`
`<Two>Two</Two>`
`</Number>`
`<Number>`
`<One>Three</One>`
`<Two>Four</Two>`
`</Number>`

Nothing in these fields correlates. I can rely on the structure/order of the fields but that´s it. So I need to map all the OBR fields, together with the following ZCT fields, before the next OBR occurs.

Any suggestions on how to solve this?

1

There are 1 best solutions below

0
On

If you could combine both xml-fragments into one like i.e. this:

<?xml version="1.0" encoding="UTF-8"?>
<proces>
  <hl7>
    <OBR>One</OBR>
    <ZCT>Two</ZCT>
    <OBR>Three</OBR>
    <ZCT>Four</ZCT>
  </hl7>
  <other>
    <Number>
      <One>One</One>
      <Two>Two</Two>
    </Number>
    <Number>
      <One>Three</One>
      <Two>Four</Two>
    </Number>
  </other>
</proces>

Then the following xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:template match="/proces">
    <result>
      <xsl:variable name="hl7Props" select="hl7/*"/>
      <xsl:for-each select="other/*/*">
        <newProp>
          <xsl:variable name="pos" select="position()"/>
          <xsl:copy-of select="."/>
          <xsl:copy-of select="$hl7Props[$pos]"/>
        </newProp>
      </xsl:for-each>
    </result>
  </xsl:template>
  
</xsl:stylesheet>

gives this result:

<?xml version="1.0" encoding="UTF-8"?>
<result>
  <newProp>
    <One>One</One>
    <OBR>One</OBR>
  </newProp>
  <newProp>
    <Two>Two</Two>
    <ZCT>Two</ZCT>
  </newProp>
  <newProp>
    <One>Three</One>
    <OBR>Three</OBR>
  </newProp>
  <newProp>
    <Two>Four</Two>
    <ZCT>Four</ZCT>
  </newProp>
</result>