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 December 21st, 2009, 11:17 AM
Authorized User
 
Join Date: Feb 2009
Posts: 31
Thanks: 6
Thanked 1 Time in 1 Post
Send a message via MSN to anil_yadav26@hotmail.com
Default XSLT with XPATH

Hi,

I have an xml file which contain TABLES. As per the width of table I have to decide whether the table will transform to html or convert as an Image.

I'm going with logic where I'm taking the longest word of column as the length of that particular column. In the last I will add the length of all the columns (longest words). If it is more than 16 characters then the table will not transform.

And if it is less than 16 characters then it will transform.


I have write some code for the same but I'm stuck at XPath where I have to check the longest word in that column and then add all the columns to get the column width.

Below is XML file and XSLT code. Can you please look into this and do the needful. It is bit urgent.

Thanks,
Anil Yadav

XML File
Code:
<?xml version="1.0" encoding="UTF-8"?>
<body>
<TABLE border="0" width="100%" align="center" cellpadding="0" cellspacing="0" style="font-size: 10pt; font-family: 'Times New Roman', Times; color: #000000; background: #FFFFFF">
<TR>
<TD>Cell 1</TD>
<TD>Some text</TD>
<TD>Some text</TD>
<TD>Some text</TD>
</TR>
<TR>
<TD>Cell 2</TD>
<TD>Some text</TD>
<TD>Some more text</TD>
<TD></TD>
</TR>
<TR>
<TD>Cell 3</TD>
<TD>Less</TD>
<TD>Very very less</TD>
<TD>Sample</TD>
</TR>
</TABLE>
<TABLE>
<TR>
<TD>Cell 1</TD>
<TD>So</TD>
<TD>So</TD>
<TD>So</TD>
</TR>
<TR>
<TD>Cell 2</TD>
<TD>So</TD>
<TD>So</TD>
<TD></TD>
</TR>
<TR>
<TD>Cell 3</TD>
<TD></TD>
<TD>Very</TD>
<TD>Sam</TD>
</TR>
</TABLE>
</body>
XSLT CODE

Code:
<xsl:template match="TABLE">
  <xsl:variable name="colCount" select="TR[1]//TD"/>
  <xsl:for-each select="TR">
   <xsl:for-each select="TD">
    <xsl:for-each-group group-by="." select="for $x in tokenize(string(.), ' ') return string-length(($x))">
     <!--xsl:if test="string-length(current-grouping-key())"-->
     <word word="{current-grouping-key()}"/>
     <!--/xsl:if-->
    </xsl:for-each-group>
   </xsl:for-each>
  </xsl:for-each>

 </xsl:template>
 
Old December 21st, 2009, 11:46 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The sum over all columns of the longest word in the column is (with TABLE as the context node)

Code:
<xsl:function name="f:column" as="element(TD)*">
  <xsl:param name="table" as="element(TABLE)"/>
  <xsl:param name="col" as="xs:integer"/>
  <xsl:sequence select="$table/TR/TD[$col]"/>
</xsl:function>

<xsl:function name="f:longest-word-in-column" as="xs:integer">
  <xsl:param name="column" as="element(TD)*"/>
  <xsl:sequence select="max(for $e in $column return 
                                       for $t in tokenize($e, '\s')
                                        return string-length($t))"/>
</xsl:function>

<xsl:variable name="table" select="."/>
<xsl:value-of select="sum(for $i in 1 to count($table/TR[1]/TD)
                                   return f:longest-word-in-column(f:column($table, $i)))"/>
Not tested.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 22nd, 2009, 02:31 AM
Authorized User
 
Join Date: Feb 2009
Posts: 31
Thanks: 6
Thanked 1 Time in 1 Post
Send a message via MSN to anil_yadav26@hotmail.com
Default

Hi Michael,

Thanks for your reply.

I get the following error message while running the above code:

An empty sequence is not allowed as the result of function f:longest-word-in-column()


XSLT

Code:
 <xsl:template match="TABLE">
  <xsl:variable name="table" select="."/>
  <xsl:value-of select="sum(for $i in 1 to count($table/TR[1]/TD)
   return f:longest-word-in-column(f:column($table, $i)))"/>
 </xsl:template>


<!--Functions-->
 <xsl:function name="f:column" as="element(TD)*">
  <xsl:param name="table" as="element(TABLE)"/>
  <xsl:param name="col" as="xs:integer"/>
  <xsl:sequence select="$table/TR/TD[$col]"/>
 </xsl:function>
 
 <xsl:function name="f:longest-word-in-column" as="xs:integer">
  <xsl:param name="column" as="element(TD)*"/>
  <xsl:sequence select="max(for $e in $column return 
   for $t in tokenize($e, '\s')
   return string-length($t))"/>
 </xsl:function>
 
Old December 22nd, 2009, 04:13 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I warned you it wasn't tested.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 22nd, 2009, 04:48 AM
Authorized User
 
Join Date: Feb 2009
Posts: 31
Thanks: 6
Thanked 1 Time in 1 Post
Send a message via MSN to anil_yadav26@hotmail.com
Default

Michael,

The code is perfectly working on the tables where the column(s) are not blank. Can you please suggest what should I do for that. If you wish I can send you the table in which I'm facing the problem.

Thanks for all your support and help.

Thanks,
Anil Yadav

Last edited by [email protected]; December 22nd, 2009 at 04:50 AM..
 
Old December 22nd, 2009, 05:01 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try replacing max(EXPR) by max((0, EXPR)). Then if EXPR is empty, max() will return zero. Or you might want to return say 3 so the column always has some width.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
[email protected] (December 22nd, 2009)
 
Old December 22nd, 2009, 09:40 AM
Authorized User
 
Join Date: Feb 2009
Posts: 31
Thanks: 6
Thanked 1 Time in 1 Post
Send a message via MSN to anil_yadav26@hotmail.com
Default

Thanks a lot, Michael!!!





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT aggregate function for xpath 1.0 garinapavan XSLT 1 November 23rd, 2009 04:30 PM
JAXP and XSLT 2.0 / XPATH 2.0 ojasrege XSLT 2 November 27th, 2007 10:40 AM
xslt xpath boris17 XSLT 2 October 15th, 2007 05:31 PM
XSLT -XPATH Error xslspy XSLT 1 October 27th, 2005 03:24 AM
XSLT/XPATH Help KombatKarl XSLT 3 September 2nd, 2004 07:35 AM





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