Martin,
Thank you so much for your reply. Your solution is intriguing and more sophisticated then what I came up with. I've enclosed my (naive) solution using nested recursions in mode templates as an alternate solution for any one interested. My solution is written for the actual data I have instead of the simplified example data I supplied, hence the additional code.
I made the required modifications to your solution using keys and it worked wonderfully. In stepping through the code I see that your method runs more efficiently than mine. But on closer inspection I found that it required additional tweaking to keep it from copying over the original data after the structured output.
For any one interested, the problem is how to group sibling nodes into a parent-child relationship. The issue is how to predicate the match to only those desire siblings. In this case we have a flat list in which a desired parent is followed by sibling nodes we want as child nodes in an ordered structure.
Thanks,
Joseph Miglietta
Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="SEGMENT">
<xsl:choose>
<xsl:when test="@NAME='OBR'">
<xsl:apply-templates select="." mode="OBR" />
</xsl:when>
<!-- copy other segments directly -->
<xsl:when test="@NAME='MSH'">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:when>
<xsl:when test="@NAME='PID'">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:when>
<xsl:when test="@NAME='PV1'">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:when>
<xsl:when test="@NAME='ORC'">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="SEGMENT" mode="OBR">
<xsl:choose>
<!-- copy OBR segments -->
<xsl:when test="@NAME='OBR'">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<!-- copy NTE segments directly associated with our OBR segment-->
<xsl:apply-templates select="following-sibling::SEGMENT[1]" mode="OBR" />
</xsl:copy>
</xsl:when>
<!-- copy NTE segments directly associated with our OBR segment-->
<xsl:when test="@NAME='NTE'">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<!-- Recurs to the next NTE segment (Does nothing if not an NTE node) -->
<xsl:apply-templates select="following-sibling::*[1]" mode="OBR" />
</xsl:when>
<xsl:when test="@NAME='OBX'">
<!-- copy OBX segments directly associated with our OBR segment-->
<xsl:apply-templates select="current()" mode="OBX" />
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="SEGMENT" mode="OBX">
<xsl:choose>
<xsl:when test="@NAME='OBX'">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:apply-templates select="following-sibling::SEGMENT[1]" mode="NTE" />
</xsl:copy>
<!-- Recurs to the next OBX segment (Does nothing if not an OBX node) -->
<xsl:apply-templates select="following-sibling::*[1]" mode="OBX" />
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="MATCH_NTE" match="SEGMENT" mode="NTE">
<xsl:choose>
<xsl:when test="@NAME='NTE'">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<!-- Recurs to the next NTE segment (Does nothing if not an NTE node) -->
<xsl:apply-templates select="following-sibling::*[1]" mode="NTE" />
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>