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 May 22nd, 2008, 06:41 AM
Registered User
 
Join Date: May 2008
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>


 
Old May 22nd, 2008, 06:50 AM
Registered User
 
Join Date: May 2008
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.

 
Old May 22nd, 2008, 06:54 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
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
 
Old May 22nd, 2008, 07:07 AM
Registered User
 
Join Date: May 2008
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>


 
Old May 26th, 2008, 05:39 AM
Registered User
 
Join Date: May 2008
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

 
Old May 27th, 2008, 02:22 PM
Authorized User
 
Join Date: May 2008
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.






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





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