Subject: Introducing parent node to specific siblings
Posted By: Frimann Post Date: 4/25/2008 10:26:27 AM
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 By: mhkay Reply Date: 4/25/2008 10:56:11 AM
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 By: Alain COUTHURES Reply Date: 4/25/2008 11:07:11 AM
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
<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 By: Frimann Reply Date: 4/25/2008 11:49:30 AM
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 By: Alain COUTHURES Reply Date: 4/25/2008 11:56:42 AM
So, you have to use <xsl:apply-templates select="......."/> instead of <xsl:copy-of select="......."/> and define all the templates you need !

Reply By: Frimann Reply Date: 4/25/2008 12:15:21 PM
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 By: Frimann Reply Date: 7/2/2008 7:57:02 AM
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:


<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 By: Volder Reply Date: 7/2/2008 8:04:34 AM
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 By: Frimann Reply Date: 7/2/2008 8:31:58 AM
Alright, here we go:

Input:

<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:

<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 By: Martin Honnen Reply Date: 7/2/2008 8:56:06 AM
Here is an XSLT 1.0 stylesheet that should do what you want:

<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 By: Volder Reply Date: 7/2/2008 9:37:47 AM
quote:
Alright, here we go:

the only thing you needed to amend in the stylesheet provided for you by Alain is to add a check whether this is a first pagebreak processed, and if yes - add there one more <div> with all the preceding elements, e.g. using <xsl:if>:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="div">
        <div class="article-content-container">
            <xsl:for-each select="pagebreak">
                <xsl:if test="position()=1">
                    <div class="page-content-container">
                        <xsl:copy-of select="preceding-sibling::*"/>
                    </div>
                </xsl:if>
                <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>
        </div>
    </xsl:template>
</xsl:stylesheet>


Reply By: Frimann Reply Date: 7/3/2008 3:20:20 PM
Thanks for the solutions Martin and Volder. I ended up using Volder's as it iplemented in my existing XSLT without fuss. I tried Martins too, but I couldn't get it to work in my design =/

XSLT is really giving me a hard time! I can't seem to grasp the logic.


Go to topic 1897

Return to index page 1