Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | 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 7th, 2008, 02:12 PM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default Indexing elements by ancestor

Hi,

I am using XSL-FO (and FOP 0.94). I am looking for a way to index or count a <table> element based on its ancestor and display the number e.g. "Table 1".

Say I had an XML file like this:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
    <SECTION_1>
        <TABLE>....</TABLE>
        <TABLE>....</TABLE>
    </SECTION_1>
    <SECTION_2>
        <TABLE>....</TABLE>
        <TABLE>....</TABLE>
        <TABLE>....</TABLE>
        <TABLE>....</TABLE>
        <TABLE>....</TABLE>
    </SECTION_2>
    <SECTION_3>
        <TABLE>....</TABLE>
        <TABLE>....</TABLE>
    </SECTION_3>
</ROOT>
Desired output:

Section 1

Table 1:
Table 2:

Section 2

Table 1:
Table 2:
Table 3:
Table 4:
Table 5:
.......

Some of my <table> elements are deep within the XML. The example above is to simplified. Any thoughts on best approach? Should I use keys? If so, how?

Thanks,

Bones


Reply With Quote
  #2 (permalink)  
Old March 7th, 2008, 02:22 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well you could try to set up the xsl:apply-templates in way that position() suffices:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:template match="ROOT/*">
    <xsl:value-of select="name()"/>
    <xsl:text>#10;</xsl:text>
    <xsl:apply-templates select=".//TABLE"/>
  </xsl:template>

  <xsl:template match="TABLE">
    <xsl:value-of select="concat('Table ', position())"/>
    <xsl:text>#10;</xsl:text>
  </xsl:template>

</xsl:stylesheet>
If that is not possible then xsl:number should help: http://www.w3.org/TR/xslt#number

Reply With Quote
  #3 (permalink)  
Old March 7th, 2008, 02:30 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default


Why is the section element named SECTION_2? It seems a very odd choice. Why include the position number as part of the element name?

The normal solution to this would be

<xsl:number level="any" match="SECTION"/>

But because of your peculiar naming convention, it might need to be something more complex. Perhaps

<xsl:number level="any" match="*[starts-with(local-name(), 'SECTION_')]"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #4 (permalink)  
Old March 7th, 2008, 02:39 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I don't think there is a match attribute on the xsl:number element. from seems to work instead:
Code:
<xsl:template match="TABLE">
<xsl:number level="any" from="*[starts-with(local-name(), 'SECTION_')]"/>
Reply With Quote
  #5 (permalink)  
Old March 7th, 2008, 02:44 PM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Hi, Actually the naming convention is nothing like the example. I have about 90 top level elements that can have <table> as descendants. I need to count the <table> position ("Table: {Number}") from the top level element.

Consider these top level elements where "chapters" and I needed to count the tables in that chapter from a <table> template.

I am reworking a XSL-fo to use with FOP 0.94. My predecessor used a XSLT extension with Hash-tables. I know I can accomplish this with straight XSLT... I am just having a mental block right now.



Reply With Quote
  #6 (permalink)  
Old March 7th, 2008, 03:03 PM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Ok, I think I got it...If there is a better way, I would like to try it.

Code:
<xsl:choose>
    <xsl:when test="//SECTION_1 | //SECTION_2 | //SECTION_3">
       <xsl:text>Figure: </xsl:text>    
           <xsl:number level="any" match="node()::table"/>
    </xsl:when>
<xsl:otherwise/>
Thanks for helping.
Reply With Quote
  #7 (permalink)  
Old March 7th, 2008, 03:20 PM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Nope.. this returns the tables numbers in document order... not by section....


Reply With Quote
  #8 (permalink)  
Old March 7th, 2008, 03:38 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Well, we all make mistakes: when I said match="SECTION" I meant from="SECTION", as Martin pointed out.

But this is complete garbage:

<xsl:when test="//SECTION_1 | //SECTION_2 | //SECTION_3">

It's saying "if there is a SECTION_1 or a SECTION_2 or a SECTION_3 anywhere in my document, then number the table, otherwise don't bother".

And with match="node()::table" you've not only repeated my error of getting the attribute name wrong, but you've now invented an axis called node()!

If by "top-level element" you mean a child of the document element, then you can use

<xsl:number level="any" from="/*/*"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #9 (permalink)  
Old March 7th, 2008, 08:51 PM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Thanks Michael and Martin,

I believe I am over thinking the problem and not explaining my desired result.

Is it possible to use xsl:key to index each SECTION[child::table] by position and call that position up by its @id?

Thanks for the help.



Reply With Quote
  #10 (permalink)  
Old March 10th, 2008, 07:59 AM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

After putting it down for a couple of days, I picked it up this morning:

<xsl:number level="any" count="//table[@tablenum[ancestor::section] = 'yes']" from="section"/>

Thanks Michael. I will need to add a <xsl:choose> for each "section", which works for me.

thanks again.


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
Extracting ancestor by providing the attribute eruditionist XSLT 6 September 9th, 2008 08:12 AM
parent/ancestor nmnm XSLT 1 April 24th, 2007 05:25 AM
Select ancestor Node bonekrusher XSLT 2 October 16th, 2006 07:07 AM
store name of an ancestor steelrose XSLT 2 June 15th, 2006 11:24 AM
Indexing. kilika SQL Server 2000 3 May 24th, 2004 03:14 PM



All times are GMT -4. The time now is 09:53 PM.


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