RE: [xsl] Removing non-alphanumeric characters from attribute

Subject: RE: [xsl] Removing non-alphanumeric characters from attribute
From: vsubramanian@xxxxxxxxxx
Date: Fri, 25 Jun 2004 09:20:12 -0400
Sorry i just realized that piece of code wont work.

Thanks,
Vidya

-----Original Message-----
From: Mukul Gandhi [mailto:mukul_gandhi@xxxxxxxxx]
Sent: Friday, June 25, 2004 2:58 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Removing non-alphanumeric characters from attribute


Please try the XSL -

<?xml version="1.0"?> 
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
  
<xsl:variable name="str"
select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'" />
  
<xsl:template match="node()">
   <xsl:copy>       
      <xsl:for-each select="@*">
        <xsl:attribute name="{name()}">
          <xsl:variable name="att-val" select="." />
          <xsl:call-template name="stripchars">
             <xsl:with-param name="x"
select="substring($att-val, 1, 1)" /> 
             <xsl:with-param name="y"
select="substring(., 2, string-length($att-val))" /> 
          </xsl:call-template>
        </xsl:attribute>
      </xsl:for-each>
      <xsl:apply-templates />
   </xsl:copy>
</xsl:template>  
  
<xsl:template name="stripchars">
  <xsl:param name="x" />
  <xsl:param name="y" />
    
  <xsl:if test="contains($str, $x)">
    <xsl:value-of select="$x" />
  </xsl:if>
    
  <xsl:if test="string-length($y) > 0">
     <xsl:call-template name="stripchars">
       <xsl:with-param name="x" select="substring($y,
1, 1)" /> 
       <xsl:with-param name="y" select="substring($y,
2, string-length($y))" /> 
     </xsl:call-template> 
  </xsl:if>
</xsl:template>
 
</xsl:stylesheet>

for e.g. when it is applied to XML -

<?xml version="1.0"?>
<root>
  <a x="123ABC+-" />
  <b y="ABC12" />
  <c z="+-1" />
</root>

it produces output -

<?xml version="1.0"?>
<root>
  <a x="123ABC" />
  <b y="ABC12" />
  <c z="1" />
</root>

Regards,
Mukul

--- perry.ielati@xxxxxxxxxxxx wrote:
> Hi all,
> 
> I was wondering if there is any way possible of
> stripping any non-alphanumeric 
> characters from an attribute. ie keep anything that
> is A-Z/0-9 and strip all 
> other characters like ",*-+. etc etc?
> 
> Thanks
> -Pez



		
__________________________________
Do you Yahoo!?
Yahoo! Mail is new and improved - Check it out!
http://promotions.yahoo.com/new_mail

--+------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe@xxxxxxxxxxxxxxxxxxxxxx>
--+--

Current Thread