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 June 2nd, 2009, 11:40 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default POSITION()

Hi,

I am using xslt 1 and msxml

I need to produce the following xml. Please note that the onclick event calls a function
SortTable(0) and the parameters must be numeric values starting from 0 to as many <th> I have.

<tr>
<th class="locked" colspan="2" style="border:0px;background-color: #859EB5"> </th>
<th onclick="SortTable(0)">Total Pages</th>
<th onclick="SortTable(1)">Cost Gross</th>
<th onclick="SortTable(2)">Total Pages</th>
<th onclick="SortTable(3)">Cost Gross</th>
<th onclick="SortTable(4)">Total Pages</th>
<th onclick="SortTable(5)">Cost Gross</th>
</tr>

I thought that would be easier using the position() function but was in for a surprise!!!

I have tried to use the context position and also the position of when I call the template but got wrong result as follows:

PASSING THE POSITION FROM CALLING TEMPLATE
The parameters are repeated 001122 instead of 012345

<tr>
<th class="locked" colspan="2" style="border:0px;background-color: #859EB5"> </th>
<th onclick="SortTable(0)">Total Pages</th>
<th onclick="SortTable(0)">Cost Gross</th>
<th onclick="SortTable(1)">Total Pages</th>
<th onclick="SortTable(1)">Cost Gross</th>
<th onclick="SortTable(2)">Total Pages</th>
<th onclick="SortTable(2)">Cost Gross</th>
</tr>

USING THE POSITION WITHING THE CURRENT TEMPLATE
<tr>
<th class="locked" colspan="2" style="border:0px;background-color: #859EB5"> </th>
<th onclick="SortTable(1)">Total Pages</th>
<th onclick="SortTable(2)">Cost Gross</th>
<th onclick="SortTable(1)">Total Pages</th>
<th onclick="SortTable(2)">Cost Gross</th>
<th onclick="SortTable(1)">Total Pages</th>
<th onclick="SortTable(2)">Cost Gross</th>
</tr>

This is the xslt I have tried but to no avail. I have also attempted to use a named template to return a sequence of numbers starting from 0 but it did not work.

<!-- TEMPLATE MEASURE -->
<xsl:template match="Measure">
<xsl:param name="pos"/>
<!--<xsl:variable name ="p" select ="-1"/>-->
<xsl:variable name ="po" select ="$pos - 1"/>
<!--
<xsl:variable name ="p">
<xsl:call-template name ="getPosition">
<xsl:with-param name ="posInc" select ="-1"/>
</xsl:call-template>
</xsl:variable>
-->
<th onclick="SortTable({position()})">
<xsl:value-of select="@heading"/>
</th>
</xsl:template>

<xsl:template name ="getPosition">
<xsl:param name ="posInc"/>
<xsl:value-of select ="$posInc + 1"/>
</xsl:template>

Is there a way to solve this problem by getting xslt to produce the sequence of numbers starting from 0 but not based on any context?

Cheers

C

Last edited by pallone; June 2nd, 2009 at 11:43 AM..
Reply With Quote
  #2 (permalink)  
Old June 2nd, 2009, 11:46 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Firstly position() is, as you say, context sensitive. It depends on the current position in your source document.

Unfortunately you don't show us what your source XML document looks like, so there is no way we can help you get position() to work.

However, you might want to look at the <xsl:number/> instruction, which can do exactly what you want I believe.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?

Last edited by samjudson; June 2nd, 2009 at 11:49 AM..
Reply With Quote
The Following User Says Thank You to samjudson For This Useful Post:
pallone (June 2nd, 2009)
  #3 (permalink)  
Old June 2nd, 2009, 12:33 PM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Sam,

Thanks for your reply.

I am sorry I forgot to include the xml. Here it is:

<Report name="audit" title="Audit" date="05-28-2009">
<Measures>
<Measure idx="1" heading="Total Pages" class="num1"/>
<Measure idx="2" heading="Cost Gross" class="cur1"/>
</Measures>
<Columns>
<ColGrp heading="Month">
<ColGrp heading="2008">
<ColGrp heading="Q3">
<Col heading="JUL"/>
<Col heading="SEP"/>
</ColGrp>
</ColGrp>
</ColGrp>
<ColGrp>
<ColGrp>
<ColGrp heading="">
<Col heading="Total"/>
</ColGrp>
</ColGrp>
</ColGrp>
</Columns>
<Rows>
<RowGrp heading="Media Full Name">
<RowGrp heading="LABORATORY">
<Row heading="CAP TODAY">
<Cell>
<Msr idx="1" val=""/>
<Msr idx="2" val=""/>
</Cell>
<Cell>
<Msr idx="1" val="0.33" class="num1"/>
<Msr idx="2" val="2505" class="cur1"/>
</Cell>
<Cell>
<Msr idx="1" val="0.33" class="num1"/>
<Msr idx="2" val="2505" class="cur1"/>
</Cell>
</Row>
</RowGrp>
<RowGrp heading="MULTISPECIALTY">
<Row heading="AMERICAN FAMILY PHYSICIAN">
<Cell>
<Msr idx="1" val="0.33" class="num1"/>
<Msr idx="2" val="7475" class="cur1"/>
</Cell>
<Cell>
<Msr idx="1" val=""/>
<Msr idx="2" val=""/>
</Cell>
<Cell>
<Msr idx="1" val="0.33" class="num1"/>
<Msr idx="2" val="7475" class="cur1"/>
</Cell>
</Row>
</RowGrp>
</RowGrp>
<RowGrp>
<RowGrp>
<Row heading="Total">
<Cell>
<Msr idx="1" val="0.33" class="num1"/>
<Msr idx="2" val="7475" class="cur1"/>
</Cell>
<Cell>
<Msr idx="1" val="0.33" class="num1"/>
<Msr idx="2" val="2505" class="cur1"/>
</Cell>
<Cell>
<Msr idx="1" val="0.66" class="num1"/>
<Msr idx="2" val="9980" class="cur1"/>
</Cell>
</Row>
</RowGrp>
</RowGrp>
</Rows>
</Report>

It would be great if I could achieve that with xslt instead of using Javascript.

I will see if I can find any tutorials about using the <xsl:number/> instruction. I have never used this before..

Cheers

C
Reply With Quote
  #4 (permalink)  
Old June 2nd, 2009, 12:36 PM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Sorry,

I just forgot to say that I thought we could achieve that with a named template but maybe I am wrong.

C
Reply With Quote
  #5 (permalink)  
Old June 2nd, 2009, 12:52 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Now that we know how your XML looks we need more information nevertheless. When you want to create
Code:
<th onclick="SortTable(0)">Total Pages</th>
<th onclick="SortTable(1)">Cost Gross</th>
<th onclick="SortTable(2)">Total Pages</th>
<th onclick="SortTable(3)">Cost Gross</th>
<th onclick="SortTable(4)">Total Pages</th>
<th onclick="SortTable(5)">Cost Gross</th>
with your stylesheet, do these six 'th' elements relate to any elements or attributes in your XML input? To which exactly? <xsl:template match="Measure"> suggest they are related to those 'Measure' elements in your XML input but I only see two of them, not six, so it is currently not clear how exactly the stylesheet should map input XML nodes to output HTML elements.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #6 (permalink)  
Old June 2nd, 2009, 02:29 PM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Martin,

For every <col> element in my XML I have 2 <Measure> elements.

So since there are 3 <col> elements:

<Col heading="JUL"/>
<Col heading="SEP"/>
<Col heading="Total"/>

I get 6 <th> elements.


<tr>
<th class="locked" colspan="2" style="border:0px;background-color: #859EB5"> </th>
<th colspan="2">JUL</th>
<th colspan="2">SEP</th>
<th colspan="2">Total</th>
</tr>
<tr>
<th class="locked" colspan="2" style="border:0px;background-color: #859EB5"> </th>
<th onclick="SortTable(1)">Total Pages</th>
<th onclick="SortTable(2)">Cost Gross</th>
<th onclick="SortTable(1)">Total Pages</th>
<th onclick="SortTable(2)">Cost Gross</th>
<th onclick="SortTable(1)">Total Pages</th>
<th onclick="SortTable(2)">Cost Gross</th>
</tr>

This's how I call the Measure template. As you can see I was passing the position() but it does not help.

<xsl:for-each select="$set">
<!-- template Measure -->
<xsl:apply-templates select="/Report/Measures/*">
<xsl:with-param name="pos" select="position()"/>
</xsl:apply-templates>
</xsl:for-each>

I would appreciate if you could shed some light otherwise I will have to unfortunately use javascript to create the onclick event.

Cheers

C
Reply With Quote
  #7 (permalink)  
Old June 3rd, 2009, 08:20 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is a sample:
Code:
  <xsl:template match="Report">
    <table>
      <tr>
        <xsl:for-each select="/Report/Columns/descendant::Col">
          <xsl:variable name="pos1" select="position()"/>
          <xsl:for-each select="/Report/Measures/Measure">
            <th onclick="SortTable({($pos1 - 1) * 2 + position() - 1})">
              <xsl:value-of select="@heading"/>
            </th>
          </xsl:for-each>
        </xsl:for-each>
      </tr>
    </table>
  </xsl:template>
That snipped, when applied to the XML you posted earlier, outputs
Code:
<table>
   <tr>
      <th onclick="SortTable(0)">Total Pages</th>
      <th onclick="SortTable(1)">Cost Gross</th>
      <th onclick="SortTable(2)">Total Pages</th>
      <th onclick="SortTable(3)">Cost Gross</th>
      <th onclick="SortTable(4)">Total Pages</th>
      <th onclick="SortTable(5)">Cost Gross</th>
   </tr>
</table>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #8 (permalink)  
Old June 3rd, 2009, 09:15 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Martin,

Many thanks for your help.

I will try that straight away.

Cheers

P
Reply With Quote
  #9 (permalink)  
Old June 3rd, 2009, 10:04 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Martin,

Your code worked very well. Thanks for that.

I assume that the number 2 in the expression below represents the number of Measure elements right?

So, I had to create a count for the Measure elements because they can vary and use that instead:

<xsl:variable name="count-Measure" elect="count(/Report/Measures/Measure)"/>

<th onclick="SortTable({($pos - 1) * $count-Measure + position() - 1})">

++++++++++++++++++++++++++++++++++++++++++++++++

Still on the same xml, I have created a template that computes the col span for my td/th

It is working fine but I would appreciate your input regarding whether the code I wrote is the best way to do it and the fastest because this xml can get very big. I wanted to avoid using "//" for speed purposes and decide to passed a set and work with it. I am not sure though this is the best technique. Here is the code:

<xsl:variablename="row-span">
<
xsl:call-templatename="count-row-span">
<
xsl:with-paramname="set"select="/Report/Columns/ColGrp"/>
<
xsl:with-paramname="rowcount"select="0"/>
</
xsl:call-template>
</
xsl:variable>

<xsl:templatename="count-row-span">
<
xsl:paramname="set"/>
<
xsl:paramname="rowcount"/>
<
xsl:choose>
<
xsl:whentest="$set/ColGrp">
<
xsl:call-templatename="count-row-span">
<
xsl:with-paramname="set"select="$set/*[1]"/>
<
xsl:with-paramname="rowcount"select="$rowcount + 1"/>
</
xsl:call-template>
</
xsl:when>
<
xsl:whentest="$set/Col">
<
xsl:value-ofselect ="count($set[1]/Col) + $rowcount + 2"/>
</
xsl:when>
</
xsl:choose>

Cheers

P

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
How to get the position of an element nazran Javascript How-To 3 February 2nd, 2010 06:56 AM
Cursor.Position iremtoksoz C# 3 January 2nd, 2008 11:41 AM
Position barski XSLT 5 July 11th, 2007 01:54 PM
Position: relative; czambran BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 2 April 7th, 2005 11:22 AM
position:absolute nerssi CSS Cascading Style Sheets 3 February 21st, 2005 10:17 AM



All times are GMT -4. The time now is 03:27 PM.


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