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 March 15th, 2011, 09:07 AM
lkd lkd is offline
Registered User
 
Join Date: Mar 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to MOVE sub-element to preceding-sibling’s last sub-element

I need an XSL rule to move an XML sub-element to print before the preceding-sibling’s last sub-element (<tools> or it could be <parts>). My if statement is:

Quote:
IF (/work/prelim/tools OR /work/prelim/parts) AND /work/main/planning exist in the XML,
THEN
MOVE /work/main/planning to print BEFORE (/work/prelim/tools OR /work/prelim/parts)
OTHERWISE just <apply-templates />.
I've tried several things, but I just can't get it to print before <tools> (or in some files, it's <parts>).

XML is as follows:

Code:
    <work>
        <prelim>
            <code>ABC</code>
            <source>DEF</source>
            <tools>Includes codes for a table</tools>
        </prelim>
        <main>
            <planning>Text for Planning</planning>
            <p>blah blah blah</p>
        </main>
    </work>
I want my output to be as follows (if the above condition exists):

Code:
    <work>
        <prelim>
            <code>ABC</code>
            <source>DEF</source>
            <planning>Text for Planning</planning>
            <tools>Includes codes for a table</tools>
        </prelim>
        <main>
            <p>blah blah blah</p>
        </main>
    </work>
Can anyone help me on how to achieve this?

Thanks in advance!!

lkd
 
Old March 15th, 2011, 09:16 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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

<xsl:template match="work/prelim/tools | work/prelim/parts">
  <xsl:copy-of select="../following-sibling::main/planning"/>
  <xsl:call-template name="identity"/>
</xsl:template>

<xsl:template match="work[prelim/tools | prelim/parts]/main/planning"/>
might suffice but would copy "planning" twice if both "tools" and "parts" exist.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 15th, 2011, 10:47 AM
lkd lkd is offline
Registered User
 
Join Date: Mar 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to MOVE sub-element to preceding-sibling’s last sub-element

Thanks!! Your code copied <planning> to the correct location but did not remove it from <main> (sigh...why doesn't XSL just have a move element?!). I tried the following rule, but it still did not remove <planning> from <main>:

Code:
<xsl:template match="main">
    <xsl:copy>
        <xsl:apply-template select=@* | node() [not(planning)]"/>
    </xsl:copy>
</xsl:template>
 
Old March 15th, 2011, 10:56 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Did you use the code as posted? When I use the stylesheet
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
<xsl:template match="@* | node()" name="identity">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="work/prelim/tools | work/prelim/parts">
  <xsl:copy-of select="../following-sibling::main/planning"/>
  <xsl:call-template name="identity"/>
</xsl:template>

<xsl:template match="work[prelim/tools | prelim/parts]/main/planning"/>

</xsl:stylesheet>
against the input
Code:
    <work>
        <prelim>
            <code>ABC</code>
            <source>DEF</source>
            <tools>Includes codes for a table</tools>
        </prelim>
        <main>
            <planning>Text for Planning</planning>
            <p>blah blah blah</p>
        </main>
    </work>
with Saxon 6.5.5 the output is
Code:
<?xml version="1.0" encoding="utf-8"?><work>
        <prelim>
            <code>ABC</code>
            <source>DEF</source>
            <planning>Text for Planning</planning><tools>Includes codes for a table</tools>
        </prelim>
        <main>

            <p>blah blah blah</p>
        </main>
    </work>
So that way the element "planning" is removed from the "main" element.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 15th, 2011, 11:00 AM
lkd lkd is offline
Registered User
 
Join Date: Mar 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Never mind...I found the resolution. Duh...all I had to do was create an empty planning rule for <planning>.

Thanks so much for your help!!

lkd
 
Old March 15th, 2011, 11:11 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well both code samples I posted already had the empty template
Code:
<xsl:template match="work[prelim/tools | prelim/parts]/main/planning"/>
for the "planning" element.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 15th, 2011, 11:15 AM
lkd lkd is offline
Registered User
 
Join Date: Mar 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks again!! I like yours better and it did work like I wanted. Yours was a new trick I didn't know!

lkd





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get preceding node child element value smandeh XSLT 7 June 9th, 2010 09:50 AM
How to move XML element at end krish XSLT 3 December 15th, 2009 06:58 AM
Move element Nagaraj XSLT 2 May 12th, 2009 03:29 AM
Want to Pull multiple following-sibling data to preceding-sibling element sameer_kadam XSLT 4 May 9th, 2009 08:07 AM
counting the preceding cousins of an element muki XSLT 1 June 15th, 2006 02:29 AM





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