XSLT: Perform an action once per element type?

S

Stuart A Yeates

I'm trying to write a template that matches "*" but which has an if
clause which if only executed once per type of element seen (where
all elements of a type have the same name and namespace). The test
in the example below doesn't work, but it seems that there must be
some relatively simple text i'm overlooking.

<!--
- process an element
-->
<xsl:template match="*">
<xsl:copy>
<xsl:if test="last()=position()">
<xsl:text>
i occur once for each type of element. this time it's for:
</xsl:text>
<xsl:value-of select="."/>
</xsl:if>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

If my input is:

<a>
<b> <d/><d/> </b>
<c> <d/><b/> </c>
</a>

I want instantiate the body of my if once for <a>, once for <b>,
once for <c> and once for <e>. It doesn't really bother me what
order they happen it.

I suspect that what I need is a recursive template that finds
determines whether the current element occupies some special
place (first or last), but i'm not sure how to write it.

stuart
 
D

Dimitre Novatchev

Use:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput omit-xml-declaration="yes"/>

<xsl:key name="kName" match="*" use="name()"/>

<xsl:template match="*[generate-id()
= generate-id(key('kName',
name()
)[1]
)
]">
Element name: <xsl:value-of select="name()"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="node()[not(self::*)]"/>
</xsl:stylesheet>

This transformation, when applied on your source.xml:

<a>
<b>
<d/>
<d/>
</b>
<c>
<d/>
<b/>
</c>
</a>

produces the desired result:


Element name: a
Element name: b
Element name: d
Element name: c


Read about the "Muenchian method for grouping" in order to understand what
actually happens here.



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top