Re: [xsl] Weired Result of XSLT

Subject: Re: [xsl] Weired Result of XSLT
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Tue, 22 May 2007 09:43:54 +0100
On 5/22/07, J. S. Rawat <jrawat@xxxxxxxxxxxxxx> wrote:
Lot of thanks for your nice clue !!! I have written <xsl:strip-space
elements="*"/> and it is working fine.

That's fine if you XML is data-centric, but if you have mixed content nodes (document-centric) then you may be stripping significant whitespace.

An alternative approach is to only select the nodes you want to
process, eg replace:

<xsl:template match="contrib-group">
 <contributors>
   <xsl:apply-templates/>
  </contributors>
</xsl:template>


<xsl:template match="contrib-group"> <contributors> <xsl:apply-templates select="contrib"/> </contributors> </xsl:template>

Then position() will work as expected.

Another alternatively is to replace position() with
count(preceding-sibling::contrib) which will only count <contrib>
elements, ignoring the whitespace presentational nodes between each
element.

Another alternative is to control the flow differently -
apply-templates to the first <contrib> element and output markup
specific to the first, then apply-templates to the rest.  This is
achieved easily with a mode, eg:

<xsl:template match="contrib-group">
 <contributors>
   <xsl:apply-templates select="contrib[1]" mode="first"/>
  </contributors>
</xsl:template>

<xsl:template match="contrib" mode="first">
 <person_name sequence="first" contributor_role="author">
   <xsl:apply-templates select="name"/>
 </person_name>

 <xsl:apply-templates select="following-sibling::contrib"/>
</xsl:template>

<xsl:template match="contrib">
 <person_name sequence="additional" contributor_role="author">
   <xsl:apply-templates select="name"/>
 </person_name>
</xsl:template>

However as both variations are identical other than the @sequence then
I'd just use an AVT, eg.

sequence="{if (not(preceding-sibling::contrib)) then 'first' else 'additional'}"

cheers
andrew

Current Thread