RE: [xsl] Entity Questions

Subject: RE: [xsl] Entity Questions
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 17 Jan 2005 21:02:30 -0000
The problem of handling input in which elements such as <b>, <i>, and <u>
can be arbitrarily nested is bread-and-butter for XSLT:

<xsl:template match="b">
 <bold><xsl:apply-templates/></bold>
</xsl:template> 

<xsl:template match="u">
 <underline><xsl:apply-templates/></underline>
</xsl:template> 

<xsl:template match="i">
 <italic><xsl:apply-templates/></italic>
</xsl:template>

It's not clear from your description, but I think you might be alluding to a
different problem, which is multi-phase or pipeline processing: that is,
producing a temporary tree and then doing another transformation on that.
This works cleanly in XSLT 2.0:

<xsl:variable name="temp">
  <xsl:apply-templates select="/" mode="phase1"/>
</xsl:variable>

<xsl:template match="/">
  <xsl:apply-templates match="$temp" mode="phase2"/>
</xsl:template>

I use two different modes here to keep the template rules for the two phases
of processing separate from each other. Each template rule will be
identified as mode="phaseN" to indicate when it applies.

There's a slight glitch in XSLT 1.0 which doesn't allow this syntax: to get
round the restriction you need to write 

<xsl:template match="/">
  <xsl:apply-templates match="xx:node-set($temp)" mode="phase2"/>
</xsl:template>

where xx:node-set() is an extension function provided by your XSLT vendor.
Every XSLT 1.0 processor provides a function of this kind, but the namespace
varies.



> 
> I was thinking about implementing a global find and replace 
> function that
> would take an input string, target and replacement as params. It would
> recurse until all the targets in the string have been 
> replaced. I would have
> to do this seperately for <p>, <i>, <b> and <font>.

I'm sorry, but I can't see what this code is trying to do at all.

Michael Kay
http://www.saxonica.com/


> 
> Is there an easier approach?
> 
> <xsl:template name="text_display_and_edit">
> <xsl:param name="text_number" />
> <xsl:param name="textname" select="concat('TEXT',$text_number)" />
> <xsl:if test="DATA/VERSION/ITEM[@NAME=$textname] !=''" >
> <xsl:value-of select="DATA/VERSION/ITEM[@NAME=$textname]" />
> </xsl:if>
> </xsl:template>
> 
> Thanks,
> 
> Luke
> 
> ----- Original Message ----- 
> From: "David Carlisle" <davidc@xxxxxxxxx>
> To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
> Sent: Monday, January 17, 2005 11:29 AM
> Subject: Re: [xsl] Entity Questions
> 
> 
> >
> >   But I would still like a way to replace a <p> tag with a 
> <fo:block>,
> etc.
> >   Can this be done at the XSL level?
> >
> >
> > XSLT has no access to the tags in the document, they like 
> entities, are
> > resolved by the XML parser before XSLT starts; but 
> replacing a p element
> > by an fo:block one is surely the most basic XSLT operation 
> (it is quite
> > literally) the main application for which XSLT is designed.
> >
> > <xsl:template match="p"><!-- or h:p if input is in a namespace -->
> >  <fo:block>
> >   <xsl:apply-templates/>
> >  </fo:block>
> > </xsl:template>
> >
> > David
> >
> > 
> ______________________________________________________________
> __________
> > This e-mail has been scanned for all viruses by Star. The
> > service is powered by MessageLabs. For more information on 
> a proactive
> > anti-virus service working around the clock, around the 
> globe, visit:
> > http://www.star.net.uk
> > 
> ______________________________________________________________
> __________

Current Thread