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 January 20th, 2004, 05:05 AM
Authorized User
 
Join Date: Nov 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Inserting one element into another

Hi All,
I have to write an XSLT.

I have a xml element like below

<MD_Body>
    <SHRow rowsep="1"></SHRow>

    <MD_Row1>
        <MsgSN>Type</MsgSN>
        <MsgNo>8</MsgNo>
    </MD_Row1>
    <MD_Row1>
        <MsgSN>Res</MsgSN>
        <MsgNo>8</MsgNo>
    </MD_Row1>

    <SHRow rowsep="1">

    </SHRow>
    <MD_Row1>
        <MsgSN Id="CJAJHIGH">Length</MsgSN>
        <MsgNo>1</MsgNo>
    </MD_Row1>

    <SHRow rowsep="1"></SHRow>

    <MD_Row1>
        <MsgSN>Res</MsgSN>
        <MsgNo>8</MsgNo>
    </MD_Row1>

    <MD_Row1>
        <MsgSN Id="CJAHABGI">ALPHA</MsgSN>
        <MsgNo>4</MsgNo>
    </MD_Row1>
    <MD_Row1>
        <MsgSN>BETA</MsgSN>
        <MsgNo>4</MsgNo>
    </MD_Row1>
</MD_Body>


This I want to convert it as below

<MD_Body>
    <SHRow rowsep="1">

        <MD_Row1>
            <MsgSN>Type</MsgSN>
            <MsgNo>8</MsgNo>
        </MD_Row1>
        <MD_Row1>
            <MsgSN>Res</MsgSN>
            <MsgNo>8</MsgNo>
        </MD_Row1>

    </SHRow>

    <SHRow rowsep="1">

        <MD_Row1>
            <MsgSN Id="CJAJHIGH">Length</MsgSN>
            <MsgNo>1</MsgNo>
        </MD_Row1>
    </SHRow>

    <SHRow rowsep="1">
        <MD_Row1>
            <MsgSN>Res</MsgSN>
            <MsgNo>8</MsgNo>
        </MD_Row1>

        <MD_Row1>
            <MsgSN Id="CJAHABGI">ALPHA</MsgSN>
            <MsgNo>4</MsgNo>
        </MD_Row1>
        <MD_Row1>
            <MsgSN>BETA</MsgSN>
            <MsgNo>4</MsgNo>
        </MD_Row1>
    </SHRow>
</MD_Body>

please help me.
srini.


srini
__________________
srini
 
Old January 20th, 2004, 01:37 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I had to get help on this one :)

Basically you need to select all the SHRow elements and then process all the MD_Row1 following sibling elements that have their immediately previous SHRow equal to the current SHRow:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="MD_Body">
    <xsl:copy>
      <xsl:apply-templates select="SHRow"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="SHRow">
    <xsl:copy>
      <xsl:apply-templates select="following-sibling::MD_Row1[generate-id(preceding-sibling::SHRow[1]) = generate-id(current())]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="MD_Row1">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>    
  </xsl:template>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>    
  </xsl:template>

</xsl:stylesheet>
You can also do it by treating it as a value based grouping problem. Examples of this are at:

http://www.jenitennison.com/xslt/grouping/index.html
Joe (MVP -xml)
 
Old January 21st, 2004, 01:08 AM
Authorized User
 
Join Date: Nov 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much.
srini.

srini
 
Old January 21st, 2004, 08:09 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

As this is a recursive process you may find with large files that it takes too long. If this is the case then a non-recursive process is discussed here: http://aspn.activestate.com/ASPN/Mai...l-list/1913969
It is obviously more complicated and will need tailoring to your structure.

Joe (MVP - xml)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem adding element to the previous element dani1 XSLT 5 September 10th, 2008 01:38 AM
Inserting/Creating element in order whelanm XSLT 3 August 14th, 2008 12:54 PM
Inserting a heading only in only the first element stekker XSLT 2 September 28th, 2006 08:06 AM
inserting element wihout template match gonzalocordero XSLT 2 July 3rd, 2006 12:46 PM
adding of element and assigning to one element sushovandatta XSLT 2 November 16th, 2004 07:04 PM





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