I am new to xml and xslt. I have some data which is in the form of some values.
Suppose This is an xml
<root>
<z>517-98-0045</z>
<z1>449-235-1235</z1>
<z2>4265-6857-1293-0098</z2>
<z3>3232-3-3232-3232-12</z3>
<z4>449-235-1235</z4>
<z5>332323-32-322</z5>
<z6>218-28-2332</z6>
<z7>517-98-0045</z7>
</root>
It should be changed to the following:
<z>517-xx-xxxx</z>
<z1>449-xxx-xxxx</z1>
<z2>4265-xxxx-xxxx-xxxx</z2>
<z3>3232-3-3232-3232-12</z3>
<z4>449-xxx-xxxx</z4>
<z5>332323-32-322</z5>
<z6>218-28-2332</z6>
<z7>517-xx-xxxx</z7>
So this means whenever xslt comes accross the three values
517-98-0045
449-235-1235
4265-6857-1293-0098
it has to transform it into this
517-XX-XXXX
449-XXX-XXXX
4265-XXXX-XXXX-XXXX
I know that regular expressions can accomplish this, but how can I do that?
According to the undergiven answers..Is this following correct---
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:template match="/">
<xsl:when test="matches(517-98-0045,'\d{3}-\d{2}-\d{4}')">
<xsl:value-of select="replace('517-98-0045','517-XX-XXXX');"/>
</xsl:when>
<xsl:when test="matches(449-235-1235,'\d{3}-\d{3}-\d{4}')">
<xsl:value-of select="replace('449-235-1235','449-XX-XXXX');"/>
</xsl:when>
<xsl:when test="matches(4265-6857-1293-0098,'\d{4}-\d{4}-\d{4}-\d{4}')">
<xsl:value-of select="replace('4265-6857-1293-0098','4265-XXxx-XXXX-xxxx');"/>
</xsl:when>
<xsl:template>
<xsl:stylesheet>
If you just want to replace those three particular values, then this should do it:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()[. = '517-98-0045' or
. = '449-235-1235' or
. = '4265-6857-1293-0098']">
<xsl:value-of select="concat(substring-before(., '-'),
'-',
translate(substring-after(., '-'),
'0123456789', 'XXXXXXXXXX'))"/>
</xsl:template>
</xsl:stylesheet>
following 3 regexes should do the trick:
replace('517-98-0045','(\d{3})-(\d{2})-(\d{4})','$1-XX-XXXX');
replace('449-235-1235','(\d{3})-(\d{3})-(\d{4})','$1-XXX-XXXX');
replace('4265-6857-1293-0098','(\d{4})-(\d{4})-(\d{4})-(\d{4})','$1-XXXX-XXXX-XXXX-XXXX');
the only thing needed now is to check which replace function apply. the function matches could do that:
<xsl:choose>
<xsl:when test="matches(.,'\d{3}-\d{2}-\d{4}')">
<xsl:value-of select="replace(.,'(\d{3})-(\d{2})-(\d{4})','$1-XX-XXXX');"/>
</xsl:when>
...
If it is always the first group of digits that should not be replaced with 'X'
, the solution below works.
I don't think you need regexes that are more complex than that.
Sample Input XML (since you did not show any XML input)
<?xml version="1.0" encoding="UTF-8"?>
<root>
<n>517-98-0045</n>
<n>449-235-1235</n>
<n>4265-6857-1293-0098</n>
</root>
Stylesheet
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/root">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="n">
<xsl:copy>
<xsl:value-of select="concat(substring-before(.,'-'),'-',replace(substring-after(.,'-'),'\d','X'))"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output
<?xml version="1.0" encoding="UTF-8"?>
<root>
<n>517-XX-XXXX</n>
<n>449-XXX-XXXX</n>
<n>4265-XXXX-XXXX-XXXX</n>
</root>
EDIT (you updated your question with actual XML input)
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/root|root/*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()[.='4265-6857-1293-0098' or .='449-235-1235' or .='517-98-0045']">
<xsl:value-of select="concat(substring-before(.,'-'),'-',replace(substring-after(.,'-'),'\d','X'))"/>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>