Connecting Tech Pros Worldwide Help | Site Map

templates and xpath expr with predicate and first and last functions

  #1  
Old March 7th, 2006, 03:05 PM
Martin
Guest
 
Posts: n/a
HI,

I have some data like:
<x>
<y Position='Top'/>
<y/>
<x/>

and a template like:
<xsl:template match="/x/y[@Position='Top']">
<li>
<xsl:if test="position()=last()">
<xsl:attribute name="class">last</xsl:attribute>
</xsl:if>
</li>
</xsl:template>

I want to catch the last y with a position of Top
unfortunately position and last work on a list of y elements, not a list of
y[@Position='Top'] elements.

In fact it's even worse than that.
If my data was
<x>
<y Position='Top'/>
<y/>
<z/>
<x/>

last() would be 3, not 2

Does any one have a work around for this bug?

Thanks
Martin


  #2  
Old March 7th, 2006, 03:25 PM
Martin Honnen
Guest
 
Posts: n/a

re: templates and xpath expr with predicate and first and last functions




Martin wrote:
[color=blue]
> I have some data like:
> <x>
> <y Position='Top'/>
> <y/>
> <x/>[/color]

Where is the closing </x> then? It is not clear whether that first x
element has two y child elements as well as an x child element.
[color=blue]
> and a template like:
> <xsl:template match="/x/y[@Position='Top']">
> <li>
> <xsl:if test="position()=last()">
> <xsl:attribute name="class">last</xsl:attribute>
> </xsl:if>
> </li>
> </xsl:template>
>
> I want to catch the last y with a position of Top
> unfortunately position and last work on a list of y elements, not a list of
> y[@Position='Top'] elements.[/color]

The template does not change the type and size of the current node list.
You simply need to use xsl:apply-templates to filter out nodes first e.g.
<xsl:apply-templates select="y[@Position = 'Top']" />

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  #3  
Old March 7th, 2006, 03:35 PM
Martin
Guest
 
Posts: n/a

re: templates and xpath expr with predicate and first and last functions


Oops sorry, the data should read (1 x root, 2 y children)[color=blue][color=green]
>> <x>
>> <y Position='Top'/>
>> <y/>
>> </x>[/color][/color]

The rest of the problem description remains.
Thanks
Martin

"Martin Honnen" <mahotrash@yahoo.de> wrote in message
news:eGhsvjfQGHA.5400@TK2MSFTNGP09.phx.gbl...[color=blue]
>
>
> Martin wrote:
>[color=green]
>> I have some data like:
>> <x>
>> <y Position='Top'/>
>> <y/>
>> <x/>[/color]
>
> Where is the closing </x> then? It is not clear whether that first x
> element has two y child elements as well as an x child element.
>[color=green]
>> and a template like:
>> <xsl:template match="/x/y[@Position='Top']">
>> <li>
>> <xsl:if test="position()=last()">
>> <xsl:attribute name="class">last</xsl:attribute>
>> </xsl:if>
>> </li>
>> </xsl:template>
>>
>> I want to catch the last y with a position of Top
>> unfortunately position and last work on a list of y elements, not a list
>> of y[@Position='Top'] elements.[/color]
>
> The template does not change the type and size of the current node list.
> You simply need to use xsl:apply-templates to filter out nodes first e.g.
> <xsl:apply-templates select="y[@Position = 'Top']" />
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/[/color]


  #4  
Old March 7th, 2006, 03:55 PM
Martin
Guest
 
Posts: n/a

re: templates and xpath expr with predicate and first and last functions


Having read some more, I think it's correct xslt behaviour, but it's still
not what I want.

I need to control the context size to be what I require it to be, maybe with
an apply-templates select clause.

Here is my new xslt which seems to fit the bill:
<xsl:template match="/x/y[1]">
<xsl:apply-templates select="/x/y[@Position='Top']" mode="test"/>
</xsl:template>

<xsl:template match="y" mode="test">
p=<xsl:value-of select="position()"/>
l=<xsl:value-of select="last()"/>
x
</xsl:template>

Martin

"Martin" <x@y.z> wrote in message
news:OMluiqfQGHA.2436@TK2MSFTNGP11.phx.gbl...[color=blue]
> Oops sorry, the data should read (1 x root, 2 y children)[color=green][color=darkred]
>>> <x>
>>> <y Position='Top'/>
>>> <y/>
>>> </x>[/color][/color]
>
> The rest of the problem description remains.
> Thanks
> Martin
>
> "Martin Honnen" <mahotrash@yahoo.de> wrote in message
> news:eGhsvjfQGHA.5400@TK2MSFTNGP09.phx.gbl...[color=green]
>>
>>
>> Martin wrote:
>>[color=darkred]
>>> I have some data like:
>>> <x>
>>> <y Position='Top'/>
>>> <y/>
>>> <x/>[/color]
>>
>> Where is the closing </x> then? It is not clear whether that first x
>> element has two y child elements as well as an x child element.
>>[color=darkred]
>>> and a template like:
>>> <xsl:template match="/x/y[@Position='Top']">
>>> <li>
>>> <xsl:if test="position()=last()">
>>> <xsl:attribute name="class">last</xsl:attribute>
>>> </xsl:if>
>>> </li>
>>> </xsl:template>
>>>
>>> I want to catch the last y with a position of Top
>>> unfortunately position and last work on a list of y elements, not a list
>>> of y[@Position='Top'] elements.[/color]
>>
>> The template does not change the type and size of the current node list.
>> You simply need to use xsl:apply-templates to filter out nodes first e.g.
>> <xsl:apply-templates select="y[@Position = 'Top']" />
>>
>> --
>>
>> Martin Honnen --- MVP XML
>> http://JavaScript.FAQTs.com/[/color]
>
>[/color]


  #5  
Old March 7th, 2006, 04:05 PM
Martin Honnen
Guest
 
Posts: n/a

re: templates and xpath expr with predicate and first and last functions




Martin wrote:
[color=blue]
> Oops sorry, the data should read (1 x root, 2 y children)[/color]
[color=blue]
> The rest of the problem description remains.[/color]

Well I suggested a change in my earlier post

[color=blue][color=green]
>>
>>The template does not change the type and size of the current node list.
>>You simply need to use xsl:apply-templates to filter out nodes first e.g.
>> <xsl:apply-templates select="y[@Position = 'Top']" />[/color][/color]



--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  #6  
Old March 7th, 2006, 05:15 PM
Martin
Guest
 
Posts: n/a

re: templates and xpath expr with predicate and first and last functions


Thanks Martin.

Very tired today.

Martin
"Martin Honnen" <mahotrash@yahoo.de> wrote in message
news:ex01v5fQGHA.2704@TK2MSFTNGP15.phx.gbl...[color=blue]
>
>
> Martin wrote:
>[color=green]
>> Oops sorry, the data should read (1 x root, 2 y children)[/color]
>[color=green]
>> The rest of the problem description remains.[/color]
>
> Well I suggested a change in my earlier post
>
>[color=green][color=darkred]
>>>
>>>The template does not change the type and size of the current node list.
>>>You simply need to use xsl:apply-templates to filter out nodes first e.g.
>>> <xsl:apply-templates select="y[@Position = 'Top']" />[/color][/color]
>
>
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/[/color]


Closed Thread