Re: Replacement for "first-of-type", etc.

Subject: Re: Replacement for "first-of-type", etc.
From: James Clark <jjc@xxxxxxxxxx>
Date: Mon, 26 Apr 1999 08:42:21 +0700
You would have to do either:

<xsl:template match="bold[last()=1]|emph[last()=1]|...">
...
</xsl:template>

or

<xsl:template match="*">
  <xsl:variable name="cur" expr="."/>
  <xsl:choose>
    <xsl:when test="count(../*[namespace(.)=namespace($cur)
                       and local-part(.)=local-part($cur)])=1">
      ...
    </xsl:when>
    <xsl:otherwise>
      ...
    </xsl:otherwise>
 </xsl:choose
</xsl:template>

I am having a hard time thinking of a real-life problem where I would
need to do this. Do you have one?

Michel Goossens wrote:
> 
> I would like to select all child element types that occur only once.
> In the previous draft I wrote something like
> <xsl-template match="*[first-of-type() and last-of-type()]">
> ...
> 
> But I do not see an evident replacement in the current (April 22) draft.
> 
> Here is an example:
> 
> Consider the following XML file:
> 
> <article>
>   <title>This is the article's title</title>
>   <author>Author One</author>
>   <author>Author Two</author>
>   <abstract>A <emph>short</emph> description of the contents</abstract>
>   <section>
>     <stitle>First section title</stitle>
>     <par>The first paragraph for this section.</par>
>     <par>A paragraph with <emph>emphasised</emph> text.</par>
>     <par>This is the end of the section.</par>
>   </section>
>   <section>
>     <stitle>Second section title</stitle>
>     <par>This is <emph>emph</emph> and <emph>more emph</emph> text.</par>
>     <par>Paragraph with <bold>bold</bold> and
>          <emph>emphasised</emph> text.</par>
>   </section>
> </article>
> 
> with the following XSL stylesheet:
> 
> <?xml version='1.0'?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0";>
> <xsl:template match="/">
>   <xsl:text>(*root*)</xsl:text>
>   <xsl:apply-templates/>
>   <xsl:text>(/*root*)</xsl:text>
> </xsl:template>
> <xsl:template match="*" priority="-1">
>   <xsl:text>(*)</xsl:text>
>   <xsl:apply-templates/>
>   <xsl:text>(/*)</xsl:text>
> </xsl:template>
> <xsl:template match="*[position()=1 and position()=last()]" priority="1">
>   <xsl:text>($)</xsl:text>
>   <xsl:apply-templates/>
>   <xsl:text>(/$)</xsl:text>
> </xsl:template>
> </xsl:stylesheet>
> 
> Then I obtain with xt the following output (which is correct):
> 
> (*root*)($)
>   (*)This is the article's title(/*)
>   (*)Author One(/*)
>   (*)Author Two(/*)
>   (*)A ($)short(/$) description of the contents(/*)
>   (*)
>     (*)First section title(/*)
>     (*)The first paragraph for this section.(/*)
>     (*)A paragraph with ($)emphasised(/$) text.(/*)
>     (*)This is the end of the section.(/*)
>   (/*)
>   (*)
>     (*)Second section title(/*)
>     (*)This is (*)emph(/*) and (*)more emph(/*) text.(/*)
>     (*)Paragraph with (*)bold(/*) and
>          (*)emphasised(/*) text.(/*)
>   (/*)
> (/$)
> 
> But I would like to select the <title>, <abstract>, and <stitle>
> elements, plus the <bold> and <emph> elements in the last <par> element
> since they are unique child elements of their respective types for the
> parent node. I tried to understand how to work with the preceding and
> following-sibling axes, but the syntax I should use escapes me.
> Can somebody explain how I could express my match pattern? Thanks. m
> 
> ----------------------------------------------------
> Dr. Michel Goossens         Phone: (+41 22) 767-5028
> IT Division                 Fax:   (+41 22) 767-8630
> CERN                        Email:  goossens@xxxxxxx
> CH-1211 Geneva 23    F-01631 CERN Cedex
> Switzerland          France
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread