 |
| 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
|
|
|
|

November 8th, 2006, 04:15 AM
|
|
Authorized User
|
|
Join Date: Nov 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

November 8th, 2006, 04:41 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

November 8th, 2006, 04:47 AM
|
|
Authorized User
|
|
Join Date: Nov 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry that was just a typo. I have used same case in both files xsl and xml.
|
|

November 8th, 2006, 04:48 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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)
|
|

November 8th, 2006, 05:21 AM
|
|
Authorized User
|
|
Join Date: Nov 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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,
|
|

November 8th, 2006, 05:40 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

November 8th, 2006, 05:43 AM
|
|
Authorized User
|
|
Join Date: Nov 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>
|
|

November 8th, 2006, 05:53 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

November 8th, 2006, 06:27 AM
|
|
Authorized User
|
|
Join Date: Nov 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>
|
|

November 8th, 2006, 06:40 AM
|
|
Authorized User
|
|
Join Date: Nov 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|
|
 |