Reputation: 15
I've been replacing the following characters, ÁÂÀÄÖÓÔÒØÅÜÉŠŽáâàäöôòøåüéšβźý, and that's working just fine. However, after the xslt the result is missing all the attributes names.
Been looking on stackoverflow and found some great code but for some reason, I can't get it to work properly.
My current code: `
<xsl:template match="@*|node()">
<xsl:call-template name="ReplaceChars">
<xsl:with-param name="Input" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:variable name="OddChars">ÁÂÀÄÖÓÔÒØÅÜÉŠŽáâàäöôòøåüéšβźý</xsl:variable>
<xsl:variable name="RegChars">AAAAOOOOOAUESZaaaaooooauesszy</xsl:variable>
<xsl:template name="ReplaceChars">
<xsl:param name="Input"/>
<xsl:value-of select="translate(replace(replace($Input, 'ß','ss'), 'ẞ', 'SS'), $OddChars, $RegChars)"/>
</xsl:template>
`
Working example: http://xsltfiddle.liberty-development.net/ej9EGbZ
How do I fix this?
Upvotes: 0
Views: 76
Reputation: 117102
There are no attributes in your XML. What's missing is your elements. That's because you're not copying them. Try it this way instead:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="OddChars">ÁÂÀÄÖÓÔÒØÅÜÉŠŽáâàäöôòøåüéšβźý</xsl:variable>
<xsl:variable name="RegChars">AAAAOOOOOAUESZaaaaooooauesszy</xsl:variable>
<xsl:template match="text()">
<xsl:value-of select="translate(replace(replace(., 'ß','ss'), 'ẞ', 'SS'), $OddChars, $RegChars)"/>
</xsl:template>
</xsl:stylesheet>
Note that both your question and your stylesheet are tagged version=1.0"
- but you are using the replace()
function which is only available in XSLT 2.0
Added:
To make this more generic, so that actual attributes are processed too:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="OddChars">ÁÂÀÄÖÓÔÒØÅÜÉŠŽáâàäöôòøåüéšβźý</xsl:variable>
<xsl:variable name="RegChars">AAAAOOOOOAUESZaaaaooooauesszy</xsl:variable>
<xsl:template match="text()">
<xsl:value-of select="translate(replace(replace(., 'ß','ss'), 'ẞ', 'SS'), $OddChars, $RegChars)"/>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="translate(replace(replace(., 'ß','ss'), 'ẞ', 'SS'), $OddChars, $RegChars)"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="comment() | processing-instruction()">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Reputation: 52888
Since you're using XSLT 2.0, you can also use an xsl:character-map
.
It looks like a lot, but you can break the character map out into a separate stylesheet and xsl:include
it.
Example...
XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" use-character-maps="cm"/>
<xsl:strip-space elements="*"/>
<xsl:character-map name="cm">
<xsl:output-character character="Á" string="A"/>
<xsl:output-character character="Â" string="A"/>
<xsl:output-character character="À" string="A"/>
<xsl:output-character character="Ä" string="A"/>
<xsl:output-character character="Ö" string="O"/>
<xsl:output-character character="Ó" string="O"/>
<xsl:output-character character="Ô" string="O"/>
<xsl:output-character character="Ò" string="O"/>
<xsl:output-character character="Ø" string="O"/>
<xsl:output-character character="Å" string="A"/>
<xsl:output-character character="Ü" string="U"/>
<xsl:output-character character="É" string="E"/>
<xsl:output-character character="Š" string="S"/>
<xsl:output-character character="Ž" string="Z"/>
<xsl:output-character character="á" string="a"/>
<xsl:output-character character="â" string="a"/>
<xsl:output-character character="à" string="a"/>
<xsl:output-character character="ä" string="a"/>
<xsl:output-character character="ö" string="o"/>
<xsl:output-character character="ô" string="o"/>
<xsl:output-character character="ò" string="o"/>
<xsl:output-character character="ø" string="o"/>
<xsl:output-character character="å" string="a"/>
<xsl:output-character character="ü" string="u"/>
<xsl:output-character character="é" string="e"/>
<xsl:output-character character="š" string="s"/>
<xsl:output-character character="β" string="s"/>
<xsl:output-character character="ź" string="z"/>
<xsl:output-character character="ý" string="y"/>
<xsl:output-character character="ß" string="ss"/>
<xsl:output-character character="ẞ" string="SS"/>
</xsl:character-map>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Fiddle: http://xsltfiddle.liberty-development.net/ej9EGbZ/3
Upvotes: 0