[exslt] Abt Libxslt use in replace function
by Bhushan other posts by this author
Sep 15 2006 6:31AM messages near this date
Re: [exslt] date:date returns 2000-01-01Z. What is wrong?
|
Re: [exslt] Abt Libxslt use in replace function
& XSLT i have used str:replace in my xsl file
this my XSL file
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform" xmlns:idmef="http://iana.org/idmef"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<xsl:import href="replace.xsl" />
<xsl:variable name="color"
select="idmef:IDMEF-Message/idmef:Alert/idmef:Analyzer/idmef:Node/idmef:name"/>
<xsl:variable name="color1" select="'a'" />
<xsl:variable name="color2" select="'e'" />
<xsl:template match="/" >
<xsl:call-template name="str:replace">
<xsl:with-param name="string" select="color"/>
<xsl:with-param name="search" select="$color1" />
<xsl:with-param name="replace" select="$color2"/>
</xsl:call-template>
<xsl:value-of select="color" />
</xsl:template>
</xsl:stylesheet>
This is my replace.xsl
this file i hv taken from EXSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="str exsl">
<xsl:template name="str:replace">
<xsl:param name="string" select="''" />
<xsl:param name="search" select="/.." />
<xsl:param name="replace" select="/.." />
<xsl:choose>
<xsl:when test="not($string)" />
<xsl:when test="not($search)">
<xsl:value-of select="$string" />
</xsl:when>
<xsl:when test="function-available('exsl:node-set')">
<!-- this converts the search and replace arguments to node sets
if they are one of the other XPath types -->
<xsl:variable name="search-nodes-rtf">
<xsl:copy-of select="$search" />
</xsl:variable>
<xsl:variable name="replace-nodes-rtf">
<xsl:copy-of select="$replace" />
</xsl:variable>
<xsl:variable name="replacements-rtf">
<xsl:for-each select="exsl:node-set($search-nodes-rtf)/node()">
<xsl:variable name="pos" select="position()" />
<replace search="{.}">
<xsl:copy-of
select="exsl:node-set($replace-nodes-rtf)/node()[$pos]" />
</replace>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="sorted-replacements-rtf">
<xsl:for-each select="exsl:node-set($replacements-rtf)/replace">
<xsl:sort select="string-length(@search)" data-type="number"
order="descending" /> <xsl:copy-of select="." />
</xsl:for-each>
</xsl:variable>
<xsl:call-template name="str:_replace">
<xsl:with-param name="string" select="$string" />
<xsl:with-param name="replacements"
select="exsl:node-set($sorted-replacements-rtf)/replace" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:message terminate="yes">
ERROR: template implementation of str:replace relies on
exsl:node-set().
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="str:_replace">
<xsl:param name="string" select="''" />
<xsl:param name="replacements" select="/.." />
<xsl:choose>
<xsl:when test="not($string)" />
<xsl:when test="not($replacements)">
<xsl:value-of select="$string" />
</xsl:when>
<xsl:otherwise>
<xsl:variable name="replacement" select="$replacements[1]" />
<xsl:variable name="search" select="$replacement/@search" />
<xsl:choose>
<xsl:when test="not(string($search))">
<xsl:value-of select="substring($string, 1, 1)" />
<xsl:copy-of select="$replacement/node()" />
<xsl:call-template name="str:_replace">
<xsl:with-param name="string" select="substring($string, 2)" />
<xsl:with-param name="replacements" select="$replacements" />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($string, $search)">
<xsl:call-template name="str:_replace">
<xsl:with-param name="string" select="substring-before($string,
$search)" />
<xsl:with-param name="replacements"
select="$replacements[position() > 1]" />
</xsl:call-template>
<xsl:copy-of select="$replacement/node()" />
<xsl:call-template name="str:_replace">
<xsl:with-param name="string" select="substring-after($string,
$search)" />
<xsl:with-param name="replacements" select="$replacements" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="str:_replace">
<xsl:with-param name="string" select="$string" />
<xsl:with-param name="replacements"
select="$replacements[position() > 1]" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
This my XML File
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="cd-price.xsl"?>
<idmef:IDMEF-Message version="1.0" xmlns:idmef="http://iana.org/idmef">
<idmef:Alert messageid="abc123456789">
<idmef:Analyzer analyzerid="bcfs">
<idmef:Node category="dns">
<idmef:name> bhushan</idmef:name>
</idmef:Node>
</idmef:Analyzer>
</idmef:Alert>
</idmef:IDMEF-Message>
when i run conversion code i am not getting converted
string it shows on following
<?xml version="1.0"?>
Plz tell wat mistake that i have made in above XSL file ..
- bhushan. E . Sonawane
Thread:
Bhushan
James Fuller
James Fuller
|