By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,954 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,954 IT Pros & Developers. It's quick & easy.

XSLT: Perform an action once per element type?

P: n/a
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
--
-- stuart yeates <s.******@cs.waikato.ac.nz> aka `loam'
Sufficiently advanced political correctness is indistinguishable
from irony.
X-no-archive:yes
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Use:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output 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

"Stuart A Yeates" <sa**@bedrock.cs.waikato.ac.nz> wrote in message
news:10***************@clint.its.waikato.ac.nz...
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
--
-- stuart yeates <s.******@cs.waikato.ac.nz> aka `loam'
Sufficiently advanced political correctness is indistinguishable
from irony.
X-no-archive:yes

Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.