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 Display Modes
  #1 (permalink)  
Old April 25th, 2008, 10:26 AM
Registered User
 
Join Date: Apr 2008
Location: , , Denmark.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Frimann
Default Introducing parent node to specific siblings

I have spent a multitude of hours on this one and am at my wits end.

The XML-input is as follows, mildly simplified:

<div>

  <pagebreak img="filename_001.gif"/>
  <head>The history of the magnificient bla</head>
  <text>bla, bla, bla...</text>
  <text>bla, bla, bla...</text>
  <text>bla, bla, bla...</text>
  <pagebreak img="filename_002.gif"/>
  <text>bla, bla, bla...</text>
  <text>bla, bla, bla...</text>
  <text>bla, bla, bla...</text>
  <pagebreak img="filename_003.gif"/>
  etc..

</div>

The output is supposed to be like this:

  <pagebreak img="filename_001.gif"/>
<div class="page-content-container">
  <head>The history of the magnificient bla</head>
  <text>bla, bla, bla...</text>
  <text>bla, bla, bla...</text>
  <text>bla, bla, bla...</text>
</div>
  <pagebreak img="filename_002.gif"/>
<div class="page-content-container">
  <text>bla, bla, bla...</text>
  <text>bla, bla, bla...</text>
  <text>bla, bla, bla...</text>
</div>
  <pagebreak img="filename_003.gif"/>

  etc..

All I need to do really, is to introduce the <div class="page-content-container"> as a parent node to all siblings except the <pagebreak>-element. I'm new at XSLT, and the experienced might laugh at this problem, but I have tried everything. Mostly variants of <xsl:for-each/> as my small brain can't seem to accept that XSLT doesn't support conditional loops.

I've had a look at this thread: http://p2p.wrox.com/topic.asp?TOPIC_ID=38896

It concerns a similar problem, but I can't seem to implement the solution in a way that works for me.

I hope you will waste some time on me =)

Reply With Quote
  #2 (permalink)  
Old April 25th, 2008, 10:56 AM
mhkay's Avatar
Wrox Author
Points: 17,770, Level: 58
Points: 17,770, Level: 58 Points: 17,770, Level: 58 Points: 17,770, Level: 58
Activity: 77%
Activity: 77% Activity: 77% Activity: 77%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,838
Thanks: 0
Thanked 267 Times in 262 Posts
Default

This is very easy in XSLT 2.0, just do

<xsl:template match="div">
  <xsl:for-each-group select="*" group-starting-with="pagebreak">
    <xsl:copy-of select="current-group()[self::pagebreak]"/>
    <div>
      <xsl:copy-of select="current-group()[not(self::pagebreak)]"/>
    </div>
  </xsl:for-each-group>
</xsl:template>

It's rather harder in XSLT 1.0: search for "XSLT positional grouping", or "XSLT sibling recursion".

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old April 25th, 2008, 11:07 AM
Authorized User
 
Join Date: Apr 2008
Location: Bordeaux, , France.
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is it true in your XML document that the first pagebreak is also the first sibling ?

If so, with XSLT 1.0, you could use something like
Code:
<xsl:for-each select="pagebreak">
 <xsl:copy-of select="."/>
 <xsl:variable name="nbpg" select="position()"/>
 <div class="page-content-container">
  <xsl:copy-of select="following-sibling::*[name()!='pagebreak' and count(preceding-sibling::pagebreak)=$nbpg]"/>
 </div>
</xsl:for-each>
Reply With Quote
  #4 (permalink)  
Old April 25th, 2008, 11:49 AM
Registered User
 
Join Date: Apr 2008
Location: , , Denmark.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Frimann
Default

First of all, thanks for the quick responses.

I can't use <xsl:copy> though, as the output siblings must have new names (variations of the <class>-element). I apologize as this isn't reflected in my example above.

And to Alain: Yes, the <pagebreak>-element is the first sibling =)



Reply With Quote
  #5 (permalink)  
Old April 25th, 2008, 11:56 AM
Authorized User
 
Join Date: Apr 2008
Location: Bordeaux, , France.
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So, you have to use <xsl:apply-templates select="......."/> instead of <xsl:copy-of select="......."/> and define all the templates you need !

Reply With Quote
  #6 (permalink)  
Old April 25th, 2008, 12:15 PM
Registered User
 
Join Date: Apr 2008
Location: , , Denmark.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Frimann
Default

I used the XSLT 2.0 implementation and it worked like a charm. You guys just saved my weekend.

Thanks for your time Alain and mhkay =)

Reply With Quote
  #7 (permalink)  
Old July 2nd, 2008, 07:57 AM
Registered User
 
Join Date: Apr 2008
Location: , , Denmark.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Frimann
Default

Hi again!

I know it's been a while, but I need to revive this thread.

I ended up reverting to Alain's solution, as I'm forced to use XSLT 1.0 (XSLT must work in Zope, and our Python programmer assures me that XSLT 2.0 is not an option in this regard).

Now I have a new problem though. Before the XML is transformed it is cut down to smaller XML-files <div> by <div> (journals and monographies are thus reduced to articles and chapters). This means that a lot of pages is cut in half too. This is a problem as Alain's solution is using the pagebreaks to identify the content in between:

Code:
<xsl:for-each select="pagebreak">
 <xsl:copy-of select="."/>
 <xsl:variable name="nbpg" select="position()"/>
 <div class="page-content-container">
  <xsl:copy-of select="following-sibling::*[name()!='pagebreak' and count(preceding-sibling::pagebreak)=$nbpg]"/>
 </div>
</xsl:for-each>
My question is: How do I put the content before the first PageBreak and after the last PageBreak inside <div class="page-content-container"> too, when my XML often don't start or end with a PageBreak?

Did this make any sense at all? I can post the real code if it helps =)

Reply With Quote
  #8 (permalink)  
Old July 2nd, 2008, 08:04 AM
Authorized User
 
Join Date: May 2008
Location: Moscow, , Russia.
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

it would be better if you post your renewed XML input and expected output.
Probably it needs just a minor amendment from what Alain suggested you a while ago.

Reply With Quote
  #9 (permalink)  
Old July 2nd, 2008, 08:31 AM
Registered User
 
Join Date: Apr 2008
Location: , , Denmark.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Frimann
Default

Alright, here we go:

Input:
Code:
<div>
  <head>The history of the magnificient bla</head>
  <text>bla, bla, bla...</text>
  <text>bla, bla, bla...</text>
  <pagebreak img="filename_001.gif"/>
  <text>bla, bla, bla...</text>
  <text>bla, bla, bla...</text>
  <text>bla, bla, bla...</text>
  <pagebreak img="filename_002.gif"/>
  <text>bla, bla, bla...</text>
  <text>bla, bla, bla...</text>
  <text>bla, bla, bla...</text>
  <pagebreak img="filename_003.gif"/>
  <text>bla, bla, bla...</text>
  <text>bla, bla, bla...</text>
</div>
Desired output:
Code:
<div class="article-content-container">
  <div class="page-content-container">
    <head>The history of the magnificient bla</head>
    <text>bla, bla, bla...</text>
    <text>bla, bla, bla...</text>
  </div>
    <pagebreak img="filename_001.gif"/>
  <div class="page-content-container">
    <text>bla, bla, bla...</text>
    <text>bla, bla, bla...</text>
    <text>bla, bla, bla...</text>
  </div>
    <pagebreak img="filename_002.gif"/>
  <div class="page-content-container">
    <text>bla, bla, bla...</text>
    <text>bla, bla, bla...</text>
    <text>bla, bla, bla...</text>
  </div>
    <pagebreak img="filename_002.gif"/>
  <div class="page-content-container">
    <text>bla, bla, bla...</text>
    <text>bla, bla, bla...</text>
  </div>
Reply With Quote
  #10 (permalink)  
Old July 2nd, 2008, 08:56 AM
Friend of Wrox
Points: 6,420, Level: 34
Points: 6,420, Level: 34 Points: 6,420, Level: 34 Points: 6,420, Level: 34
Activity: 22%
Activity: 22% Activity: 22% Activity: 22%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,194
Thanks: 0
Thanked 236 Times in 235 Posts
Default

Here is an XSLT 1.0 stylesheet that should do what you want:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="div">
    <div class="article-content-container">
      <xsl:apply-templates select="*[1]"/>
    </div>
  </xsl:template>

  <xsl:template match="div/*[not(preceding-sibling::*) or preceding-sibling::*[1][self::pagebreak]]">
    <div class="page-content-container">
      <xsl:copy-of select="."/>
      <xsl:apply-templates select="following-sibling::*[1][not(self::pagebreak)]"/>
    </div>
    <xsl:apply-templates select="following-sibling::pagebreak[1]"/>
  </xsl:template>

  <xsl:template match="div/*[not(not(preceding-sibling::*) or preceding-sibling::*[1][self::pagebreak])]">
    <xsl:copy-of select="."/>
    <xsl:apply-templates select="following-sibling::*[1][not(self::pagebreak)]"/>
  </xsl:template>

  <xsl:template match="div/pagebreak">
    <xsl:copy-of select="."/>
    <xsl:apply-templates select="following-sibling::*[1]"/>
  </xsl:template>

</xsl:stylesheet>
--
  Martin Honnen
  Microsoft MVP - XML
Reply With Quote
Reply


Thread Tools
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
Not outputting a specific node zach_1988 XSLT 3 December 2nd, 2008 09:49 AM
retrieving information from a parent node Tomi XSLT 2 September 6th, 2006 06:54 AM
Finding parent node Chamkaur XSLT 4 August 9th, 2006 06:26 AM
How to delete an element and parent node. crossedge XSLT 1 March 14th, 2006 04:10 AM
Saving the parent node Doyle Bradely XML 2 December 23rd, 2003 06:05 PM



All times are GMT -4. The time now is 01:50 PM.


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