[xsl] Re: processing-instruction()

Subject: [xsl] Re: processing-instruction()
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Wed, 31 Jul 2002 08:04:41 -0700 (PDT)
--- "Jiang, Peiyun" <Peiyun dot Jiang at nrc dot ca> wrote:
 
> What's wrong?
> 
> I'm trying to match processing instructions. I want to get rid of the
> <?Eqn
> TeX input="\hskip -32pt\hbox to32pt{[6]}"?> part in the source file. 
> 
> Source file: <equation> and <inlineequation> mixed with text and
> markup.
> XSLT: I match every <equation> and <inlineequation> and copy
> recursively any
> attributes and elements. I match processing instructions and remove
> them all
> (but I want to keep some of them later).
> output: no processing instructions removed.
> 
> Am I doing the right thing to match the processing instructions? See
> stylesheet below.
> 
> Thanks.
> 
> Peiyun
> 
> 
> -----------------------
> source: equations mixed with text.
> <doc>Text before equations.
> <inlineequation id="il1"><?Pub Eqn?>
> <f><rm><a><ac>CV</ac><ac>&d4;</ac></a><fen lp="par"><rm><g>q</g></rm>
> <rp post="par"/></fen></rm></f>
> </inlineequation>
> More text and other <tags></tags> more text.
> <equation id="eqn006"><?Pub Eqn?>
> <fd><fl><rm><?Eqn TeX input="\hskip -32pt\hbox to32pt{[6]}"?></rm>
> <rm>Prob<fen
> lp="par"><mit>m<inf>ij</inf>&vbm0;R<inf>j</inf><rm>,</rm>
> <g>p</g><inf>ij</inf></mit><rp post="par"/></fen>=<fen lp="par"><stk>
>
<lyr><mit>Rj</mit></lyr><lyr><it><mit>m<inf>ij</inf></mit></it></lyr></stk>
> <rp
> post="par"/></fen><g>p</g><sup><mit>m<inf>ij</inf></mit></sup><inf>
> <mit>ij</mit></inf></rm><fen lp="par">1-<g>p</g><inf>ij</inf><rp
> post="par"/></fen>
> <sup>R<inf>j</inf>-m<inf>ij</inf></sup></fl></fd>
> </equation>
> <moretext></moretext> more text.
> </doc>
> -----------------------
> output: equations without text.
> 
> <equations>
> <inlineequation id="il1"><?Pub Eqn?>
> <f><rm><a><ac>CV</ac><ac>&d4;</ac></a><fen lp="par"><rm><g>q</g></rm>
> <rp post="par"/></fen></rm></f>
> </inlineequation>
> <equation id="eqn006"><?Pub Eqn?>
> <fd><fl><rm><?Eqn TeX input="\hskip -32pt\hbox to32pt{[6]}"?></rm>
> <rm>Prob<fen
> lp="par"><mit>m<inf>ij</inf>&vbm0;R<inf>j</inf><rm>,</rm>
> <g>p</g><inf>ij</inf></mit><rp post="par"/></fen>=<fen lp="par"><stk>
>
<lyr><mit>Rj</mit></lyr><lyr><it><mit>m<inf>ij</inf></mit></it></lyr></stk>
> <rp
> post="par"/></fen><g>p</g><sup><mit>m<inf>ij</inf></mit></sup><inf>
> <mit>ij</mit></inf></rm><fen lp="par">1-<g>p</g><inf>ij</inf><rp
> post="par"/></fen>
> <sup>R<inf>j</inf>-m<inf>ij</inf></sup></fl></fd>
> </equation>
> </equations>
> -----------------------
> stylesheet:
>   <xsl:template match="/">     
>      <equations>
>         <xsl:for-each select="//equation|//inlineequation" >
>           <xsl:copy>
>              <xsl:copy-of select="@*" />
>              <xsl:apply-templates select="node()" />
>              <xsl:apply-templates
> select="processing-instruction('Eqn')" />
>              <xsl:apply-templates
> select="processing-instruction('Pub')" />
>           </xsl:copy>
>         </xsl:for-each>
>      </equations>       
>   </xsl:template>
>   
>   <xsl:template match="node()">
>        <xsl:copy>
>             <xsl:copy-of select="@*" />
>             <xsl:apply-templates select="node()" />
>              <xsl:apply-templates
> select="processing-instruction('Eqn')" />
>              <xsl:apply-templates
> select="processing-instruction('Pub')" />
>        </xsl:copy>
>   </xsl:template>
>   
>   <xsl:template match="processing-instruction('Eqn')">
>       <!-- do nothing now -->
>   </xsl:template>
>   
>   <xsl:template match="processing-instruction('Pub')">
>       <!-- do nothing now, but want to keep -->
>   </xsl:template>
>  
> -----------------------
> desired output: (inline)equations only and without <?Eqn ...?> 
> <equations>
> <inlineequation id="il1"><?Pub Eqn?>
> <f><rm><a><ac>CV</ac><ac>&d4;</ac></a><fen lp="par"><rm><g>q</g></rm>
> <rp post="par"/></fen></rm></f>
> </inlineequation>
> <equation id="eqn006"><?Pub Eqn?>
> <fd><fl><rm></rm>
> <rm>Prob<fen
> lp="par"><mit>m<inf>ij</inf>&vbm0;R<inf>j</inf><rm>,</rm>
> <g>p</g><inf>ij</inf></mit><rp post="par"/></fen>=<fen lp="par"><stk>
>
<lyr><mit>Rj</mit></lyr><lyr><it><mit>m<inf>ij</inf></mit></it></lyr></stk>
> <rp
> post="par"/></fen><g>p</g><sup><mit>m<inf>ij</inf></mit></sup><inf>
> <mit>ij</mit></inf></rm><fen lp="par">1-<g>p</g><inf>ij</inf><rp
> post="par"/></fen>
> <sup>R<inf>j</inf>-m<inf>ij</inf></sup></fl></fd>
> </equation>
> </equations>
> -----------------------


This transformation can be simple, if implemented as customisation of
the identity rule:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:template match="/">
    <equations>
      <xsl:apply-templates select="/*/node()"/>
    </equations>
  </xsl:template>
  
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="processing-instruction('Eqn')"/>
  <xsl:template match="text()[not(ancestor::equation 
                            or ancestor::inlineequation)]"/>
  <xsl:template match="*[not(ancestor::*[2]) and
                         not(self::equation 
                            or self::inlineequation
                            )
                         ]"/>
  
</xsl:stylesheet>



=====
Cheers,

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

__________________________________________________
Do You Yahoo!?
Yahoo! Health - Feel better, live better
http://health.yahoo.com

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


Current Thread