[xsl] Using xsl:variable and replace() to remove specific characters.

Subject: [xsl] Using xsl:variable and replace() to remove specific characters.
From: "Richard Corfield" <richard.corfield@xxxxxxx>
Date: Mon, 28 Apr 2008 10:25:52 +0100
Hi

I've been using xslt on and off for a few years, but not for long enough to make me feel like I've moved on from 'novice' status yet. The problem I'm grappling with at the moment is really making feel like I don't understand even the fundamentals yet!

What I'm trying to do is process some xml of which this is a fragment:

<aff id="aj266199af2">
  <label>2</label>National Radio Astronomy Observatory, Chile<xref ref-type="fn" rid="aj266199afn4"><sup>9</sup></xref>; <ext-link ext-link-type="email" id="aj266199em4">email address here</ext-link>
</aff>

<!-- snip -->
<fn id="aj266199afn4">
  <label>9</label>
  <p>The National Radio Astronomy Observatory is a facility of the National Science Foundation operated under cooperative agreement by Associated Universities, Inc.
  </p>
</fn>


The problem is the trailing semi-colon between the <xref> & <ext-link> elements.

Previously to remove characters like this I have seen this sort of thing successfully employed elsewhere:

<xsl:variable name="searchStr" select="'[:;,\.\s]+$'"/>
<xsl:variable name="replaceStr" select="''"/>

<!-- snip -->

<xsl:variable name="affilStr">
        <xsl:apply-templates select="key('key_allElementsById' , .)"/>
</xsl:variable>
<affil id="{.}">
        <xsl:value-of select="replace($affilStr , $searchStr , $replaceStr)"
/>


However the above xml snippet is dealt with by this:

<xsl:for-each select="//aff">
        <affil id="{label}">
           <xsl:apply-templates />
        </affil>
  </xsl:for-each>

... and this is the output:

<affil id="2">National Radio Astronomy Observatory, Chile<footnote pos="affil" id="9">The National Radio Astronomy Observatory is a facility of the National Science Foundation operated under cooperative agreement by Associated Universities, Inc.</footnote>; </affil>

However if I try to employ the same method to remove the semi-colon:

<xsl:variable name="searchStr" select="';\s*$'"/>
<xsl:variable name="replaceStr" select="''"/>

   <xsl:variable name="nameStr">
     <xsl:apply-templates/>
   </xsl:variable>
   <affil id="{label}">
           <xsl:value-of select="$nameStr"/>
        </affil>

The result is as follows:

      <affil id="2">National Radio Astronomy Observatory, ChileThe National Radio Astronomy Observatory is a facility of the National Science Foundation operated under cooperative agreement by Associated Universities, Inc.</affil>

We've lost the semi-colon (excellent!) but also structure of the xml, the <footnote> tags. :-(

Would anyone be able to comment on my approach and point out what would be a more sensible approach?

I'm using saxon to process the xml with xslt v2.

Many thanks

Richard

************************************************************************
This email (and attachments) are confidential and intended for the addressee(s) only. If you are not the intended recipient please notify the sender, delete any copies and do not take action in reliance on it. Any views expressed are the author's and do not represent those of IOP, except where specifically stated. IOP takes reasonable precautions to protect against viruses but accepts no responsibility for loss or damage arising from virus infection. For the protection of IOP's systems and staff emails are scanned automatically.

IOP Publishing Limited Registered in England under Registration No 467514. Registered Office: Dirac House, Temple Back, Bristol BS1 6BE England
Vat No GB 461 6000 84.

Please consider the environment before printing this e-mail

Current Thread