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, 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
 
Old March 8th, 2009, 03:54 AM
Friend of Wrox
 
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
 
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





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





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