p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   xsl:key doesnt work (http://p2p.wrox.com/showthread.php?t=42922)

cristtiah May 23rd, 2006 02:31 PM

xsl:key doesnt work
 
<xsl:key name="b" match="Field/@label" use="."/>
<xsl:key name="c" match="Field/@type" use="."/>


....

<xsl:template name="generate-top-elements">

<tr>
<xsl:for-each select="//@label[generate-id(.)=generate-id(key('b',.)[1])]">
  <td><xsl:value-of select="."/></td>

</xsl:for-each>
</tr>
....

<xsl:for-each select="//@type[generate-id(.)=generate-id(key('c',.)[1])]">
    <td><xsl:value-of select="."/>

....

</xsl:template>

and an xml

<Fields>
<Field type="Separator" title="Account Setup" start="yes" pos="top"/>
<Field type="TextBox" label="First Name"></Field>
<Field type="TextBox" label="Middle Name"></Field>
<Field type="TextBox" label="Last Name"></Field>
<Field type="End"/>
<Field type="Separator" title="Investments" pos="level"/>
<Field type="DropDownList" label="Primary Service Type"></Field>
...

and the output

<table>
<tr>
<td>Account Setup</td>
</tr>
</table>
<table>
<tr>
<td>First Name</td>
<td>Middle Name</td>
<td>Last Name</td>
<td>Primary Service Type</td>
<td>Traits</td>
</tr>
<tr>
<td>First Name</td>
<td>Middle Name</td>
<td>Last Name</td>
<td>Primary Service Type</td>
<td>Traits</td>
</tr>
<tr>
<td>First Name</td>
<td>Middle Name</td>
<td>Last Name</td>
<td>Primary Service Type</td>
<td>Traits</td>
</tr>
</table>

<table>
<tr>
<td>Investments</td>
</tr>
</table>
<table>
<tr>
<td>Primary Service Type</td>
<td></td>
<td></td>
</tr>
<tr>
<td>Traits</td>
<td></td>
<td></td>
</tr>
</table>


but should be

<table>
<tr>
<td>Account Setup</td>
</tr>
</table>
<table>
<tr>
<td>First Name</td>
<td>Middle Name</td>
<td>Last Name</td>
</tr>
<tr>
<td>TextBox</td>
<td>TextBox</td>
<td>TextBox</td>
</tr>
</table>

<table>
<tr>
<td>Investments</td>
</tr>
</table>
<table>
<tr>
<td>Primary Service Type</td>
<td></td>
<td></td>
</tr>
<tr>
<td>Traits</td>
<td></td>
<td></td>
</tr>
</table>


is there a way I could put a condition on the key? thanks




mhkay May 24th, 2006 04:25 AM

You haven't shown enough of your code to see where the problem is. And I can't see where "Traits" comes from either. Please construct a simplified example to illustrate the problem, and then show the complete code. (Sometimes this exercise helps you to find your own bug before you post it.)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

cristtiah May 24th, 2006 06:12 AM

i have this code

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:asp="remove">
    <xsl:output method="html" />
    <xsl:key name="b" match="Field/@label" use="." />
    <xsl:key name="c" match="Field/@value" use="." />

    <xsl:key name="x" match="Field/@label" use="generate-id((self::Field[@pos='top']|following-sibling::Field[@label='End'])[last()])"/>

    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>
    <xsl:template match="Form/Pages/Page">
        <table>
            <xsl:apply-templates select="Fields" />
        </table>
    </xsl:template>

    <xsl:template match="Fields">
        <tr>
            <xsl:for-each select="Field[@pos='top']">
                    <td>
                        <xsl:copy-of select="key('x',generate-id())"/>
                    </td>
            </xsl:for-each>

        </tr>
    </xsl:template>



and xml


<?xml version="1.0"?>
<Form>
    <Pages>
        <Page>
            <Fields>
                <Field type="Separator" label="none" title="Accounts Setup" pos="top"/>
                <Field type="TextBox" label="one" value="- 1" />
                <Field type="TextBox" label="two" value="- 2" />
                <Field type="TextBox" label="three" value="- 3" />
                <Field type="End" label="End" />
                <Field type="Separator" label="none" title="Investment" pos="top" columns="2"/>
                <Field type="TextBox" label="four" value="- 4"/>
                <Field type="TextBox" label="five" value="- 5"/>
                <Field type="End" label="End" />
            </Fields>
        </Page>
    </Pages>
</Form>


I need to get the nodes between the type=separator and type=End and put them in a table like this

<table>
<tr>
<td>one</td>
<td>two</td>
<td>three</td>
</tr>
<tr>
<td>- 1</td>
<td>- 2</td>
<td>- 3</td>
</tr>
</table>
<table>
<tr>
<td>four</td>
<td>five</td>
</tr>
<tr>
<td>- 4</td>
<td>- 5</td>
</tr>
</table>


thanks.....



mhkay May 24th, 2006 07:53 AM

I don't really understand what you're trying to do. But this key

    <xsl:key name="x" match="Field/@label" use="generate-id((self::Field[@pos='top']|following-sibling::Field[@label='End'])[last()])"/>


is nonsense. The match pattern matches attributes, which means that the use expression will never select anything, because the attribute can't match self::Field, and an attribute has no following siblings.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

cristtiah May 24th, 2006 08:24 AM

Hi I just need the nodes between the type=Separator and type=End


mhkay May 24th, 2006 08:38 AM

>Hi I just need the nodes between the type=Separator and type=End

XSLT 2.0 solution:

<xsl:variable name="s" select="Field[@type='Separator']"/>
<xsl:variable name="e" select="Field[@type='End']"/>


<xsl:for-each select="*[. >> $s and . << $e]">
  ...

Sorry, the XSLT 1.0 solutions to such problems are just too much like hard work and I can't summon up the enthusiasm to tackle them. But I think your problem is that the key is matching attributes rather than elements.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

cristtiah May 24th, 2006 08:40 AM

is there any similar examples for this solution in XSLT 1.0? Thanks again..


cristtiah May 24th, 2006 08:56 AM

ow and I tried

<xsl:for-each select="*[. >> $s and . << $e]">

in XMLSpy xslt 2.0 and it says its not well formed "& expected"




mhkay May 24th, 2006 09:21 AM

Sorry, in XSLT the << operator needs to be escaped as &_lt;&_lt; (ignore the underscores)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

cristtiah May 24th, 2006 09:49 AM

nope doesnt work
<xsl:for-each select="*[. &_lt;&_lt; $s and . &_lt;&_lt; $e]">

Unknown error


cristtiah May 24th, 2006 09:54 AM

any example on xslt 1.0? thanks really appreciate it..


cristtiah May 24th, 2006 09:57 AM

I tried this

<xsl:for-each select="*[. &gt;&gt; $s and . &lt;&lt; $e]">

Error...too many items... any examples for xslt 1.0?



All times are GMT -4. The time now is 09:15 PM.

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