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 November 7th, 2003, 05:01 AM
Authorized User
 
Join Date: Nov 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default finding max string length if more than 2 elements

suppose i have an XML file like the following,

<?xml version="1.0" encoding="UTF-8"?>
<registers>
     <register>
          <name>ramesh</name>
          <Bits><start>15</start></Bits>
     </register>
     <register>
          <name>xxxxxxxx</name>
          <Bits><start>12</start></Bits>
     </register>
     <register>
          <name>srinivas</name>
          <Bits><start>20</start><end>16</end></Bits>
     </register>
     <register>
          <name>rajagopi</name>
          <Bits><start>6</start><end>0</end></Bits>
     </register>
     <register>
          <name>rama</name>
          <Bits><start>10</start><end>7</end></Bits>
     </register>
</registers>

here i have to find maximum string length. if i use the xsl file like the following i am getting the result as srinivas.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="Length">
   <xsl:for-each select="/employees/emp/name">
      <xsl:sort select="string-length(.)" order="descending" data-type="number"/>
      <xsl:if test="position() = 1">
         <xsl:value-of select="string-length(.)"/>
      </xsl:if>
   </xsl:for-each>
</xsl:variable>
Max string is: <xsl:value-of select="$Length"/>
</xsl:template>
</xsl:stylesheet>

but here we have 3 registers( xxxxxxxx,srinivas,rajagopi) with max string length of 8.

here if i find the range of bits, like, start-end, for xxxxxxxx it is 1, because there is no end, and for srinivas it is 4(20-16) and for rajagopi it is 6(6-0). so in these kind of situations i have to give preference to xxxxxxxx, i.e max string length with range 1.

so if i get 2 or more strings with same string length and with various ranges,
then first
1. i have to get the string with max string length and range is 1.
otherwise
if max string length, strings with range 1 are not there, then
2. i should get the string with max string length and range is max.

for the second condition suppose i have srinivas and rajagopi with ranges 4 and 6, so i have to get rajagopi.

is it possible to get it? how i have to change my xsl file so that i will get this result?

please help me . i am trying for it since 3 days.
thanks,
srini.



srini
__________________
srini
 
Old November 7th, 2003, 12:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Here is the stylesheet (I've added my code to your template):

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
        <xsl:template match="/">
            <xsl:variable name="Length">
               <xsl:for-each select="/registers/register/name">
                  <xsl:sort select="string-length(.)" order="descending" data-type="number"/>
                  <xsl:if test="position() = 1">
                     <xsl:value-of select="string-length(.)"/>
                  </xsl:if>
               </xsl:for-each>
            </xsl:variable>
            Max string length is: <xsl:value-of select="$Length"/><br/>
            <xsl:variable name="max-strings" select="/registers/register/name[string-length() = $Length]"/>
            The string selected is: <xsl:choose>
                <xsl:when test="$max-strings[not(../Bits/end)]">
                     <xsl:value-of select="$max-strings[not(../Bits/end)][1]"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:for-each select="$max-strings">
                        <xsl:sort select="../Bits/start - ../Bits/end" order="descending" data-type="number"/>
                        <xsl:if test="position() = 1">
                            <xsl:value-of select="."/>
                        </xsl:if>                    
                    </xsl:for-each>
                </xsl:otherwise>
            </xsl:choose>  
        </xsl:template>
</xsl:stylesheet>
Regards,
Armen
 
Old April 4th, 2006, 10:00 AM
Registered User
 
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have a similar problem but it is more complex than the original post.

I am trying to specify the column widths in GMR XML (gmr:ColInfo). I GMR, the unit is equal to the maximum number of characters that could be visible in that column.

I want to set the width of the column to the largest length of the elements that fall under that column. Here is my XML
 ...
<cdmReportTableHeaderXML>
    <header type="text">
      <label>Order Location</label>
    </header>
    <header type="text">
      <label>Order Location Name</label>
    </header>
    <header type="text">
      <label>State</label>
    </header>
    <header type="integer">
      <label>ECar Orders</label>
    </header>
    <header type="integer">
      <label>Model Orders</label>
    </header>
    <header type="integer">
      <label>Furnished Model</label>
    </header>
    <header type="double">
      <label>%Furnished Model</label>
    </header>
  </cdmReportTableHeaderXML>
  <cdmReportTableXML>
    <tableRow>
      <column>00003</column>
      <column>BUFFALO </column>
      <column>NY</column>
      <column>122</column>
      <column>83</column>
      <column>78</column>
      <column>93.97590361445783</column>
    </tableRow>
    <tableRow>
      <column>00184</column>
      <column>CLEVELAND </column>
      <column>OH</column>
      <column>48</column>
      <column>38</column>
      <column>35</column>
      <column>92.10526315789474</column>
    </tableRow>
  ....
  <cdmReportTableXML>

The desired output is

 <gmr:Cols>
        <gmr:ColInfo No="0" Unit="lengthOfLargestOrderLocationString"/>
        <gmr:ColInfo No="1" Unit="lengthOfLargestOrderLocationName"/>
        <gmr:ColInfo No="2" Unit="..."/>
        <gmr:ColInfo No="3" Unit=".../>
        <gmr:ColInfo No="4" Unit=".../>
        <gmr:ColInfo No="5" Unit=".../>
        <gmr:ColInfo No="6" Unit=".../>
</gmr:Cols>

Ofcourse the number of column elements under tableRow would be same as the number of header elements. Neither the number of headers nor the number of rows are constant.

In the reply to the original post, the element name was predefined. Basically given an index 'i', I want an XPATH that could give me the column elements under all tableRows such that each column element is the 'i'th child of a tableRow.

Any suggestions?

 
Old April 4th, 2006, 10:23 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>Basically given an index 'i', I want an XPATH that could give me the column elements under all tableRows such that each column element is the 'i'th child of a tableRow.

That's

tableRow/column[$i]

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 4th, 2006, 01:08 PM
Registered User
 
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Turns out so simple.... Thanks a lot Micheal

It really worked so beautifully. Here is the template.. it might be useful for others

<xsl:template name="outputColumnSizes">
<gmr:Cols>
  <xsl:for-each select="key('headerXML', 'header')">
   <xsl:variable name="index" select="position()" />
   <xsl:variable name="headerTitleSize" select = "string-length(.)" />
   <xsl:variable name="colSize">
     <xsl:for-each elect="key('tableDataXML',
                          'tableRow')/column[$index]">
        <xsl:sort select="string-length(.)"
                           order="descending" data-type="number"/>
    <xsl:if test="position() = 1">
          <xsl:value-of select="string-length(.)"/>
          </xsl:if>
      </xsl:for-each>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test = "$colSize &gt; headerTitleSize" >
    <gmr:ColInfo No="{$index - 1}" Unit="{$colSize * 6}" />
      </xsl:when>
      <xsl:otherwise>
       <gmr:ColInfo No="{$index - 1}" Unit="{$headerTitleSize * 6}"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each>
</gmr:Cols>
</xsl:template>

And your book is wonderful insight for programmers like me.










Similar Threads
Thread Thread Starter Forum Replies Last Post
max length property of multiline text box MunishBhatia ASP.NET 2.0 Professional 5 June 14th, 2007 01:55 AM
Max length venkata MySQL 2 May 3rd, 2006 12:39 AM
max length of textbox? JelfMaria VB How-To 3 May 10th, 2005 04:02 PM
Query String Too Long....Max Length Error Message phungleon Classic ASP Databases 14 May 28th, 2004 12:25 PM
Finding Field Type & string length From dataset? Jackie VB.NET 2002/2003 Basics 4 July 9th, 2003 04:53 PM





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