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 | Calendar | 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 May 22nd, 2008, 06:41 AM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Add Missing columns

I need to add missing column based on column count
I have
 <table>
        <tgroup cols="2">
          <colspec colnum="1" colname="col1" colwidth="1.00*" />
          <colspec colnum="2" colname="col2" colwidth="4.47*" />
          <thead>
            <row>
              <entry>Request Number:</entry>
              <entry>Changes since last revision:</entry>
            </row>
          </thead>
          <tbody >
            <row>
              <entry>R07-1171</entry>

            </row>
            <row>
              <entry />
              <entry />
            </row>
            <row>
              <entry />
              <entry />
            </row>
            <row>
              <entry>New topic released on 2007-03-19, <b>Rev. 1.0</b></entry>
            </row>
          </tbody>
        </tgroup>
      </table>

As first row contains only one entry but cols defined are 2 in tgroup.

I am able to get attribute value of tbody but I need attribute value of col in tgroup i.e parent's parent. Its inserting <entry> tag but not checking the condition.

<xsl:template match="row">
<row>
<xsl:variable name="noofcols" select="parent::tbody/attribute::try"/>
<b><xsl:value-of select="parent::tbody/attribute::try"/></b>
   <xsl:apply-templates select="*"/>
  <xsl:if test="count(following-sibling::entry) &lt; $noofcols"><entry> </entry></xsl:if>

</row>


Reply With Quote
  #2 (permalink)  
Old May 22nd, 2008, 06:50 AM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I was able match the count but I need to get parent's parent attribute value.

Reply With Quote
  #3 (permalink)  
Old May 22nd, 2008, 06:54 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I don't see an attribute named try: I would expect

<xsl:variable name="noofcols" select="../../@cols"/>

In XSLT 2.0 you can then do:

<xsl:for-each select="$noofcols - count(entry)">
  <entry/>
</xsl:for-each>

In XSLT 1.0 there's a kludgy equivalent

<xsl:for-each select="(//node())[position() &lt;= ($noofcols - count(entry))]">
  <entry/>
</xsl:for-each>

Your use of following-sibling::entry isn't going to work, because a row doesn't have any following siblings that are entry's.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #4 (permalink)  
Old May 22nd, 2008, 07:07 AM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry there was mistake. I require total entry nodes to be equal to value of cols under a row. if <row><entry>df</entry></row>
It should be <row><entry>df</entry><entry><entry></row>


Reply With Quote
  #5 (permalink)  
Old May 26th, 2008, 05:39 AM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
In the above case in thead section if two cols are present then in tbody rows should have same number of cols with same sequence.
eg
<table>
        <tgroup cols="2">
          <colspec colnum="1" colname="col1" colwidth="1.00*" />
          <colspec colnum="2" colname="col2" colwidth="4.47*" />
          <thead>
            <row>
              <entry>Request Number:</entry>
              <entry>Changes since last revision:</entry>
            </row>
          </thead>
          <tbody >
            <row>
              <entry colname="col1" >R07-1171</entry>

            </row>
            <row>
              <entry colname="col2" > ABC-223</entry>
                         </row>
                     </tbody>
        </tgroup>
      </table>

The out put should be
<table>
        <tgroup cols="2">
          <colspec colnum="1" colname="col1" colwidth="1.00*" />
          <colspec colnum="2" colname="col2" colwidth="4.47*" />
          <thead>
            <row>
              <entry>Request Number:</entry>
              <entry>Changes since last revision:</entry>
            </row>
          </thead>
          <tbody >
            <row>
              <entry colname="col1" >R07-1171</entry>
              <entry colname="col2" ></entry>

            </row>
            <row>
<entry colname="col1" ></entry>
              <entry colname="col2" > ABC-223</entry>
                         </row>
                     </tbody>
        </tgroup>
      </table>

I don't know how to check this as columns for difrernt table are going to be diffrerent . Kindly help me on this. Thanks

Reply With Quote
  #6 (permalink)  
Old May 27th, 2008, 02:22 PM
Authorized User
 
Join Date: May 2008
Location: Moscow, , Russia.
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:I don't know how to check this as columns for difrernt table are going to be diffrerent . Kindly help me on this. Thanks
the following XSL will produce the result required by you:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:apply-templates select="*" mode="all"/>
    </xsl:template>
    <xsl:template match="*" mode="all">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@*"/>
            <xsl:value-of select="text()"/>
            <xsl:apply-templates select="*" mode="all"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>
    </xsl:template>
    <xsl:template match="tbody/row" mode="all">
        <xsl:variable name="pos" select="position()"/>
        <row>
            <xsl:for-each select="../../colspec">
                <entry colname="{@colname}">
                    <xsl:variable name="att" select="@colname"/>
                    <xsl:value-of select="../tbody/row[position()=$pos]/entry[@colname=$att]"/>
                </entry>
            </xsl:for-each>
        </row>
    </xsl:template>
</xsl:stylesheet>

PS
But I think this can be simplified, and I hope gurus will show a way for this.

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
Instructions to add new columns to products table. mcarol44 BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 1 October 13th, 2007 03:26 AM
How to add 2 columns in DataGrid.DataKeyField drasko ASP.NET 1.0 and 1.1 Professional 4 September 17th, 2004 09:05 AM
Missing Columns with WriteXml khautinh General .NET 0 August 20th, 2004 10:46 AM
How to add 2 columns in DataGrid1.DataKeyField drasko C# 0 December 6th, 2003 09:47 AM



All times are GMT -4. The time now is 07:34 AM.


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