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 March 7th, 2008, 02:12 PM
Friend of Wrox
 
Join Date: Jul 2006
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


 
Old March 7th, 2008, 02:22 PM
Friend of Wrox
 
Join Date: Nov 2007
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

 
Old March 7th, 2008, 02:30 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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
 
Old March 7th, 2008, 02:39 PM
Friend of Wrox
 
Join Date: Nov 2007
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_')]"/>
 
Old March 7th, 2008, 02:44 PM
Friend of Wrox
 
Join Date: Jul 2006
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.



 
Old March 7th, 2008, 03:03 PM
Friend of Wrox
 
Join Date: Jul 2006
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.
 
Old March 7th, 2008, 03:20 PM
Friend of Wrox
 
Join Date: Jul 2006
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....


 
Old March 7th, 2008, 03:38 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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
 
Old March 7th, 2008, 08:51 PM
Friend of Wrox
 
Join Date: Jul 2006
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.



 
Old March 10th, 2008, 07:59 AM
Friend of Wrox
 
Join Date: Jul 2006
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.







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





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