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, 2009, 05:39 PM
Authorized User
 
Join Date: Feb 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to select attributes in previous nodes

What I need to do is take the text-align attributes from <colspec> and put them into the <entry> elements, while maintaining their correct column position. So I start with something like this:

Code:
<table>
  <tgroup cols="3">
   <colspec text-align="center"/>
   <colspec text-align="right"/>
   <colspec text-align="left"/>
   <tbody>
    <row>
     <entry>T1, R1, C1</entry>
     <entry>T1, R1, C2</entry>
     <entry>T1, R1, C3</entry>
    </row>
    <row>
     <entry>T1, R2, C1</entry>
     <entry>T1, R2, C2</entry>
     <entry>T1, R2, C3</entry>
    </row>
   </tbody>
  </tgroup>
 </table>
And want to end with this:

Code:
<table>
  <tgroup cols="3">
   <colspec/>
   <colspec/>
   <colspec/>
   <tbody>
    <row>
     <entry text-align="center">T1, R1, C1</entry>
     <entry text-align="right">T1, R1, C2</entry>
     <entry text-align="left">T1, R1, C3</entry>
    </row>
    <row>
     <entry text-align="center">T1, R2, C1</entry>
     <entry text-align="right">T1, R2, C2</entry>
     <entry text-align="left">T1, R2, C3</entry>
    </row>
   </tbody>
  </tgroup>
 </table>
The input tables will all be different, with an arbitrary number of columns and rows. I do have the number of columns available in the @cols attribute of the <colspec> element, but I'm not sure how to make use of it. The basic problem I'm having is how to make sure I'm putting the proper @text-align attribute into <entry> as I'm looping through the rows. It would be easy if I could simply increment a number I think, using something like colspec[$x]/@text-align, but apparently it's difficult to simply increment a number in xslt. Any idea how I might grab these text-align values and keep them with the proper column?

Thanks,
Josh
Reply With Quote
  #2 (permalink)  
Old March 8th, 2009, 03:54 AM
Friend of Wrox
Points: 533, Level: 8
Points: 533, Level: 8 Points: 533, Level: 8 Points: 533, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default

Try the following

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xs"
      version="2.0">
    <xsl:template match="table">
        <xsl:copy>
            <xsl:apply-templates></xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="tgroup">
        <xsl:copy>
            <xsl:attribute name="cols" select="@cols"></xsl:attribute>
            <xsl:apply-templates></xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="colspec">
        <xsl:copy></xsl:copy>
    </xsl:template>
    <xsl:template match="tbody">
        <xsl:copy>
            <xsl:for-each select="row">
                <xsl:copy>
                    <xsl:for-each select="entry">
                        <xsl:variable name="position" select="position()"></xsl:variable>
                        <xsl:copy>
                            <xsl:attribute name="text-align" select="ancestor::tgroup/colspec[position() = $position]/attribute::text-align"></xsl:attribute>
                            <xsl:value-of select="."></xsl:value-of>
                        </xsl:copy>
                    </xsl:for-each>
                </xsl:copy>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>
Cheers, John Bampton
Reply With Quote
  #3 (permalink)  
Old March 8th, 2009, 05:40 PM
Authorized User
 
Join Date: Feb 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks so much Mr. Bampton. You have no idea how much more simple this is than what I was trying. I was fighting with nasty recursive templates, etc. This is so much more simple it's brilliant.

I didn't use everything you posted, but the key lines were:
<xsl:variable name="position" select="position()"></xsl:variable>
and
<xsl:attribute name="text-align" select="ancestor::tgroup/colspec[position() = $position]/attribute::text-align"></xsl:attribute>

Really all I had to do was add those to my existing xslt.

Thanks again. You just saved me hours (additional hours) of pounding my head against the wall.

Thanks,
Josh
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
Conditional select previous records' value sjanderson XSLT 5 May 4th, 2007 03:28 AM
selecting nodes that lack certain attributes rmers XSLT 3 October 4th, 2006 03:44 PM
How to select these nodes? hustsay23 XSLT 1 September 7th, 2006 04:15 AM
XML DOM :modifying the existing nodes attributes i sharmasourabh54 XML 1 February 14th, 2005 05:04 AM
Need to copy all nodes, attributes, and namespaces juaniux XSLT 4 October 22nd, 2004 04:39 PM



All times are GMT -4. The time now is 04:15 AM.


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