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 April 25th, 2008, 10:26 AM
Registered User
 
Join Date: Apr 2008
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 =)

 
Old April 25th, 2008, 10:56 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
 
Old April 25th, 2008, 11:07 AM
Authorized User
 
Join Date: Apr 2008
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>
 
Old April 25th, 2008, 11:49 AM
Registered User
 
Join Date: Apr 2008
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 =)



 
Old April 25th, 2008, 11:56 AM
Authorized User
 
Join Date: Apr 2008
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 !

 
Old April 25th, 2008, 12:15 PM
Registered User
 
Join Date: Apr 2008
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 =)

 
Old July 2nd, 2008, 07:57 AM
Registered User
 
Join Date: Apr 2008
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 =)

 
Old July 2nd, 2008, 08:04 AM
Authorized User
 
Join Date: May 2008
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.

 
Old July 2nd, 2008, 08:31 AM
Registered User
 
Join Date: Apr 2008
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>
 
Old July 2nd, 2008, 08:56 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Not outputting a specific node zach_1988 XSLT 3 December 2nd, 2008 10: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 05:10 AM
Saving the parent node Doyle Bradely XML 2 December 23rd, 2003 07:05 PM





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