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 8th, 2006, 04:15 AM
Authorized User
 
Join Date: Nov 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSL html output problem

Hi,
I have below xml
  <view date="" time="">
       <name>TEST</name>
    <sort order="0" col="test" visible="-1">desc</sort>
    <filter>
    </filter>
    <Main>
        <market>Test1</market>
        <ci>TestCl</ci>
        <bs>B</bs>
        <qty>0</qty>
        <qtycleared>all</qtycleared>
        <cont>c1</cont>
        <del>del1</del>
    </Main>
         <Main>
        <market>Test1</market>
        <ci>TestC1</ci>
        <bs>B</bs>
        <qty>0</qty>
        <cont>c1</cont>
        <del>del1</del>
    </Main>

</view>

I am trying to do something simple as displaying the data based on key market, below is xsl
<xsl:key name='mkt' match='main' use='market' />
 <xsl:template match='main'>
 <table class="trans" width="684" cellspacing="0" cellpadding="0" border="1" frame="box">
  <xsl:for-each select="main[count(. | key('mkt', market)[1]) = 1]">
   <tr>
     <td align="middle"><xsl:value-of select='./market' /></td>
     <td align="middle"><xsl:value-of select='./ci' /></td>
     <td align="middle"><xsl:value-of select='./bs'/></td>
     <td align="middle"><xsl:value-of select='./qty' /></td>
     <td align="middle"><xsl:value-of select='./cont' /></td>
     <td align="middle"><xsl:value-of select='./del' /></td>
    </tr>
  </xsl:for-each>
 </table>
</xsl:template>


But I dont get any output to html. What is the error in above. Also, if I need to generate a key based on market and ci,then how should I add ci to the key.






 
Old November 8th, 2006, 04:41 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

XML is case-sensitive. If the element name is spelt "Main" in the source document, it must be spelt "Main" in the stylesheet.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 8th, 2006, 04:47 AM
Authorized User
 
Join Date: Nov 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry that was just a typo. I have used same case in both files xsl and xml.

 
Old November 8th, 2006, 04:48 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

XML is case sensitive, Main not main in both your template and your key declaration.
To have a key based on both ci and market you can concatenate them using a character not present in either field, e.g. ~.
Code:
<xsl:key name="mainFromMarketAndCi" match="Main" use="concat(market, '~', ci)"/>
then you can access Main elements via:
Code:
key('mainFromMarketAndCi', concat('Test1', '~', 'TestCl'))
Not sure that your approach is strictly necessary though, depends on what you are trying to output exactly.

--

Joe (Microsoft MVP - XML)
 
Old November 8th, 2006, 05:21 AM
Authorized User
 
Join Date: Nov 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have ensured that the case is same in all places. But have a doubt on below line
I interpreted below as the first occurance of key ie since I have used key as market and if this is its first occurance then display
   <xsl:for-each select="main[count(. | key('mkt', market)[1]) = 1]">
Am I right?

Based on above reply, if I concat then do I need to do below
     <xsl:for-each select="main[count(. | key('mkt', concat(market,'~',ci)[1]) = 1]">

Actually, I need to concat all the elements in order to get the key. But to start with learning keys, I am trying with just 2 first.
Would I then need to concat all fields?

Thanks,

 
Old November 8th, 2006, 05:40 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, I can't distinguish typos from bugs. If the code you posted is not the code that's failing, then it's pointless for me to look at it.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 8th, 2006, 05:43 AM
Authorized User
 
Join Date: Nov 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry about that. Resending code,
<view date="" time="">
       <name>TEST</name>
    <sort order="0" col="test" visible="-1">desc</sort>
    <filter>
    </filter>
    <main>
        <market>Test1</market>
        <ci>TestCl</ci>
        <bs>B</bs>
        <qty>0</qty>
        <qtycleared>all</qtycleared>
        <cont>c1</cont>
        <del>del1</del>
    </main>
      <main>
        <market>Test1</market>
        <ci>TestC1</ci>
        <bs>B</bs>
        <qty>0</qty>
        <cont>c1</cont>
        <del>del1</del>
    </main>

</view>
--XSL---
<xsl:key name='mkt' match='main' use='market' />
 <xsl:template match='main'>
 <table class="trans" width="684" cellspacing="0" cellpadding="0" border="1" frame="box">
  <xsl:for-each select="main[count(. | key('mkt', market)[1]) = 1]">
   <tr>
     <td align="middle"><xsl:value-of select='./market' /></td>
     <td align="middle"><xsl:value-of select='./ci' /></td>
     <td align="middle"><xsl:value-of select='./bs'/></td>
     <td align="middle"><xsl:value-of select='./qty' /></td>
     <td align="middle"><xsl:value-of select='./cont' /></td>
     <td align="middle"><xsl:value-of select='./del' /></td>
    </tr>
  </xsl:for-each>
 </table>
</xsl:template>




 
Old November 8th, 2006, 05:53 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your element called "main" does not have a child called "main", so in a template with match="main", a path expression select="main[....]" will select nothing. Remember that in a step in a path expression, "main" is short for "child::main".

As far as I can see, you don't actually want to generate one output table for every "main" element in the input, so this code doesn't belong in a match="main" template.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 8th, 2006, 06:27 AM
Authorized User
 
Join Date: Nov 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Based on my understanding of your mail, I tried to match template ='view'. As this would have 'main' as its children. But I still dont see the output. Yes, I am not creating a table for each main. So I have just added <tr> for each select and moved <table> creation

<xsl:key name='mkt' match='main' use='market' />
 <xsl:template match='view'>
  <xsl:for-each select="view[count(. | key('mkt', market)[1]) = 1]">
   <tr>
     <td align="middle"><xsl:value-of select='./market' /></td>
     <td align="middle"><xsl:value-of select='./ci' /></td>
     <td align="middle"><xsl:value-of select='./bs'/></td>
     <td align="middle"><xsl:value-of select='./qty' /></td>
     <td align="middle"><xsl:value-of select='./cont' /></td>
     <td align="middle"><xsl:value-of select='./del' /></td>
    </tr>
  </xsl:for-each>
</xsl:template>


 
Old November 8th, 2006, 06:40 AM
Authorized User
 
Join Date: Nov 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, I tried after modifying below line to use main[] instead of view[] and checking with only 1 node
  <xsl:for-each select="main[1]">
Now, I get one row.
However, my previous question on my interpretation of
   <xsl:for-each select="main[count(. | key('mkt', market)[1]) = 1]">
Am I right?
And,actually, I need to concat all the elements in order to get the key. But to start with learning keys, I am trying with just 2 first.
Would I then need to concat all fields?








Similar Threads
Thread Thread Starter Forum Replies Last Post
output xsl in predefined template newuser2525 XSLT 1 December 24th, 2007 08:30 AM
XSL:output method="text" Pankaj C XSLT 4 August 2nd, 2007 10:05 AM
<xsl:output method=”xhtml”> problem li72 XSLT 5 May 16th, 2007 10:45 AM
xsl:element not on its own line in output EstherMStrom XSLT 3 February 26th, 2005 06:08 AM
PDF output problem using XSL pulavarthi XSLT 1 November 6th, 2003 05:41 AM





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