I am trying to translate white space by "-".
Data come from TEI-XML
data:
<m type="base">
<m type="baseForm">A<c type="infix">B</c>CD</m>
</m>
and XSL
file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:for-each select="descendant::m[@type='base']/m[@type='baseForm']">
<xsl:choose>
<xsl:when test="current()/c"><xsl:value-of select="current()[not != c[@type['infix']]] |node()"/></xsl:when>
<xsl:otherwise><xsl:value-of select="current()"/></xsl:otherwise>
</xsl:choose>
<!-- my attempt -->
<xsl:value-of select="translate(., ' ','-')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Following answer of this post XSL replace space with caret, I have used translate
, but it doesn't work.
The result should be: "A-B-CD" but I have "A B CD."
Thanks for your kind help.
As I can predict, problem with spaces will be when XML is beatified as below:
<?xml version="1.0" encoding="UTF-8"?>
<m type="base">
<m type="baseForm">
A
<c type="infix">
B
</c>
CD
</m>
</m>
In that case your logic can be put inside variable
and then performed translate
function, see XSL below:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/">
<!--put your logic in variable-->
<xsl:variable name="str">
<xsl:for-each select="descendant::m[@type='base']/m[@type='baseForm']">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:variable>
<!--normalize space will prevent spaces from left and right of string, then all spaces inside will be replaced by '-' -->
<xsl:value-of select="translate(normalize-space($str), ' ', '-')"/>
</xsl:template>
</xsl:stylesheet>
Result will be as expected:
A-B-CD