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 February 22nd, 2006, 02:47 PM
Registered User
 
Join Date: Dec 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Moving Tags


Hi
I would like to move tags beginning with a certain string (and their contents) out from one level to the upper

In the example below (that should help you in understand my request), I would like to move all tags beginning by FR_Sched_ from START/MAIN/Sched_* level to START/MAIN level
Please find an example of what I have and the result expected:
--------------------------------------------------------------------------------------------------------------
Original XML:
<START>
        <HEADER>
                <DATA1>1</DATA1>
                <DATA2>2</DATA2>
        </HEADER>
        <MAIN>
                <Bonds>
                        <SD>2006-01-18</SD>
                        <MaturityDate>2007-07-18</MaturityDate>
                        <Principal>110</Principal>
                </Bonds>
                <Sched_0>
                        <FR_Sched_0>
                               <FR_ShortName>ShortName1</FR_ShortName>
                        </FR_Sched_0>
                        <Outsanding>200</Outsanding>
                        <StartDate>2006-01-18</StartDate>
                        <EndDate>2006-07-18</EndDate>
                </Sched_0>
                <Sched_1>
                <Outsanding>400</Outsanding>
                        <StartDate>2006-07-18</StartDate>
                        <EndDate>2007-01-18</EndDate>
                </Sched_1>
                <Sched_2>
                        <FR_Sched_2>
                               <FR_ShortName>ShortName2</FR_ShortName>
                        </FR_Sched_2>
                        <Outsanding>1000</Outsanding>
                        <StartDate>2007-01-18</StartDate>
                        <EndDate>2007-07-18</EndDate>
                </Sched_2>
        </MAIN>
</START>
--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
Expected XML:
<START>
        <HEADER>
                <DATA1>1</DATA1>
                <DATA2>2</DATA2>
        </HEADER>
        <MAIN>
                <Bonds>
                        <SD>2006-01-18</SD>
                        <MaturityDate>2007-07-18</MaturityDate>
                        <Principal>110</Principal>
                </Bonds>
                <Sched_0>
                        <Outsanding>200</Outsanding>
                        <StartDate>2006-01-18</StartDate>
                        <EndDate>2006-07-18</EndDate>
                </Sched_0>
                <Sched_1>
                        <Outsanding>400</Outsanding>
                        <StartDate>2006-07-18</StartDate>
                        <EndDate>2007-01-18</EndDate>
                </Sched_1>
                <Sched_2>
                        <Outsanding>1000</Outsanding>
                        <StartDate>2007-01-18</StartDate>
                        <EndDate>2007-07-18</EndDate>
                </Sched_2>

                <FR_Sched_0>
                        <FR_ShortName>ShortName1</FR_ShortName>
                </FR_Sched_0>
                <FR_Sched_2>
                        <FR_ShortName>ShortName2</FR_ShortName>
                </FR_Sched_2>
        </MAIN>
</START>
--------------------------------------------------------------------------------------------------------------


Is it feasible?

Feel free to ask me if you need more info.

Best Regards.
Stefan.

 
Old February 22nd, 2006, 03:44 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Usual approach to this kind of problem is to define an identity template as your default template:

<xsl;template match="*">
 <xsl:copy><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy>
</xsl:template>

and the add template rules for elements that need changing.

You need to change the MAIN element

<xsl:template match="MAIN">
<xsl:copy>
  <xsl:apply-templates/>
  <xsl:copy-of select="*/*[starts-with(name(), 'FR_Sched')]"/>
</xsl:copy>
</xsl:template>

and the template for FR_SCHED elements:

<xsl:template match="*[starts-with(name(), 'FR_Sched')]"/>

Incidentally, <FR_SCHED no="n"> is much better XML design than <FR_SCHED_n>.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
moving around a page ozPATT Excel VBA 1 October 17th, 2005 02:42 AM
Moving through a Do Loop mmcdonal VBScript 1 April 8th, 2005 02:10 PM
moving a column p_gauri7 Crystal Reports 6 December 20th, 2004 03:27 PM
Moving through a Recordset KeithHungate Access VBA 2 April 12th, 2004 08:47 PM
Moving Data stu9820 ASP.NET 1.0 and 1.1 Basics 1 February 17th, 2004 01:58 AM





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