Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 13th, 2011, 02:13 PM
Registered User
 
Join Date: May 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sibling grouping by recursion

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>

Last edited by jmiglietta; May 13th, 2011 at 02:37 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Grouping following siblings bonekrusher XSLT 5 November 19th, 2009 02:30 PM
Problem accessing the following siblings. Tre XSLT 6 June 5th, 2007 11:13 AM
Comparing siblings Chamkaur XSLT 1 June 17th, 2006 09:56 PM
Turning Siblings into Children wolfie78uk XSLT 3 December 22nd, 2005 08:33 AM
Accessing Attributes of Siblings AForgue XSLT 2 November 4th, 2003 01:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.