Finding out what is the parent element position ?

Here should go questions about transforming XML with XSLT and FOP.
H2O
Posts: 9
Joined: Tue Dec 13, 2005 7:42 pm
Location: France

Finding out what is the parent element position ?

Post by H2O » Fri Dec 23, 2005 7:57 pm

Hi,

I am generating CSSed html pages where I need to express a varying class for a menu bar list.
Each "chapter" element from the XML is transformed in a <li> element for the menu bar.
the <li> class of each chapter for the bar menu is varying accordingly to the position of the page being processed (4 cases).
My problem is that I have not been able- yet - to pass the current page (i.e being processed) chapter position for the math.

Basically I generate the content with a :
<for-each //masterPage |//page />

and then call a <for-each chapter> ... for generating the menu bar.

Although this may be trivial , I can't figure out how to do it.

Do you have any idea ?

Thank your for reading !



<?xml version="1.0" encoding="UTF-8"?>
<document>
<chapter>
<chapterDirectory/>
<masterPage>
<masterPageContent/>
</masterPage>
<page>
<pageName>
<pagecontent>
</page>
...
<page/>
...
<chapter></chapter>
</document>
loving being a human being

george
Site Admin
Posts: 2101
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Fri Dec 23, 2005 11:04 pm

Hi,

I do not understand exactly what you want... anyway here there are a couple of suggestions.
One way to get the position is by using the position() funtion. Another way is to count on the preceding-sibling axis, thus you get the possition of the node inside its parent.

Hope that helps!
Happy holidays,
George

H2O
Posts: 9
Joined: Tue Dec 13, 2005 7:42 pm
Location: France

Post by H2O » Sat Dec 24, 2005 6:53 pm

Thank you for your fast answer George,

I already tried the position() way but found out that it only applies to the current node.
If I missed the point please tell me.

To be more specific, here comes snippets of my XSLT stylesheet (the XML code snippet was exposed in my first post) :

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

...
<xsl:template match="/">
<xsl:for-each select=" //masterPage | //page ">
...

<xsl:result-document href="{$vDirectory}/{.}{$extension}">
<!-- creating each resulting page of the web site -->
<!-- The parent "chapterDirectory is easy to grab, but the problem is that I can't pass this parameter to do the math for my menu bar template : "chapter template"-->

<html>...
<!-- here goes coy of masterPageContent or copy of pageContent
that's the main content of my page -->

<!-- but each page also contains a menu bar which is the list of each chapter.
to generate the menu bar, I go thru : -->
<ul id=menubar>
<xsl:for-each select="/document/chapter">
<xsl:call-template name="chapter">
</xsl:for-each>
</ul>
</xsl result-document>
</xsl:for-each>
<xsl:template name="chapter">
<!-- without class markup for css, I would simply state : -->
<li><xsl:value-of select="chapterDirectory"/></li>
<!-- but I have a class attribute to add to the "li" element. And the class value
depends on the position of each chapter node being processed compared with
the current page's chapterDirectory : more than one before, one before, same position or after ? -->
<!-- When I processed each page one by one (without the result-document option) I passed an external paramter for each page giving the position of the parent chapter node for the page to be processed and then every thing was easy. -->

<xsl:if test="page">>
<ul class="deroulant">
<xsl:for-each select="page">
<li>
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat"stuf)"/><xsl:text>.htm</xsl:text>
</xsl:attribute>
<xsl:value-of select="pageName"/>
</a>
</body>
</html>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Should recursion be used to solve this problem ?
I would be delighted if someone could advise me some starting directions.

Thank you for reading, and happy holidays !
loving being a human being

george
Site Admin
Posts: 2101
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Tue Dec 27, 2005 10:31 am

Hi,

I'm sorry but I cannot understand what you have problems with, both the source and the stylesheet are not even wellformed so it is hard to try to guess their real content. If you can post a small working xml/xsl pair I and describe your problem with that I can look into it and see if I can come up with a solution.

Best Regards,
George

H2O
Posts: 9
Joined: Tue Dec 13, 2005 7:42 pm
Location: France

Post by H2O » Mon Jan 02, 2006 12:43 pm

Thank you for your answer george, the simple fact that you answered my post helped me going further.

The problem was using parameters for performing arithmetics in relative positionning of nodes. Recursion solved it :)

I wish all of you a happy new year full of joy and accomplishments
loving being a human being

Post Reply