all groups > dotnet xml > march 2006 >
You're in the

dotnet xml

group:

templates and xpath expr with predicate and first and last functions


templates and xpath expr with predicate and first and last functions Martin
3/7/2006 2:57:10 PM
dotnet xml:
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

Re: templates and xpath expr with predicate and first and last functions Martin
3/7/2006 3:23:02 PM
Oops sorry, the data should read (1 x root, 2 y children)
[quoted text, click to view]

The rest of the problem description remains.
Thanks
Martin

[quoted text, click to view]

Re: templates and xpath expr with predicate and first and last functions Martin
3/7/2006 3:39:16 PM
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

[quoted text, click to view]

Re: templates and xpath expr with predicate and first and last functions Martin Honnen
3/7/2006 4:07:56 PM


[quoted text, click to view]

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.

[quoted text, click to view]

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
Re: templates and xpath expr with predicate and first and last functions Martin Honnen
3/7/2006 4:47:20 PM


[quoted text, click to view]

Well I suggested a change in my earlier post


[quoted text, click to view]



--

Martin Honnen --- MVP XML
Re: templates and xpath expr with predicate and first and last functions Martin
3/7/2006 5:04:10 PM
Thanks Martin.

Very tired today.

Martin
[quoted text, click to view]

AddThis Social Bookmark Button