Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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
Reply With Quote
  #2 (permalink)  
Old March 15th, 2011, 09:16 AM
Friend of Wrox
Points: 6,673, Level: 34
Points: 6,673, Level: 34 Points: 6,673, Level: 34 Points: 6,673, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,242
Thanks: 0
Thanked 244 Times in 243 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
Reply With Quote
  #3 (permalink)  
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>
Reply With Quote
  #4 (permalink)  
Old March 15th, 2011, 10:56 AM
Friend of Wrox
Points: 6,673, Level: 34
Points: 6,673, Level: 34 Points: 6,673, Level: 34 Points: 6,673, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,242
Thanks: 0
Thanked 244 Times in 243 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
Reply With Quote
  #5 (permalink)  
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
Reply With Quote
  #6 (permalink)  
Old March 15th, 2011, 11:11 AM
Friend of Wrox
Points: 6,673, Level: 34
Points: 6,673, Level: 34 Points: 6,673, Level: 34 Points: 6,673, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,242
Thanks: 0
Thanked 244 Times in 243 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
Reply With Quote
  #7 (permalink)  
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:14 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.