Home > Software engineering >  how to translate multiples string text in a XML using XSLT 1.0
how to translate multiples string text in a XML using XSLT 1.0

Time:07-21

I want to translate different lines of text in an XML using XSLT, I have the following XML

<COMPANY>   
    <SCHEDULES>
        <OFFICE name="BCO">
            <DEPARTAMENT name="BCO1">
                <days>monday:friday</days>
                <code>True</code>
                <indice>check</indice>
            </DEPARTAMENT>
            <TEAM name="BCO2">
                <days>monday:friday</days>
                <code>True</code>
                <indice>check</indice>
            </TEAM>
        </OFFICE>
        <OFFICE name="RCO">
            <DEPARTAMENT name="RCO1">
                <days>monday:tuesday</days>
                <code>False</code>
                <indice>check</indice>
            </DEPARTAMENT>
            <TEAM name="RCO2">
                <days>monday:wednesday</days>
                <code>True</code>
                <indice>check</indice>
            </TEAM>
        </OFFICE>
        <OFFICE name="LCO">
            <DEPARTAMENT name="LCO1">
                <days>wednesday:saturday</days>
                <code>True</code>
                <indice>check</indice>
            </DEPARTAMENT>
            <TEAM name="LCO2">
                <days>wednesday:saturday</days>
                <code>True</code>
                <indice>check</indice>
            </TEAM>
        </OFFICE>
        <OFFICE name="ACO">
            <DEPARTAMENT name="ACO1">
                <days>monday</days>
                <code>False</code>
                <indice>check</indice>
            </DEPARTAMENT>
            <TEAM name="ACO2">
                <days>tuesday</days>
                <code>False</code>
                <indice>check</indice>
            </TEAM>
        </OFFICE>
    </SCHEDULES>
</COMPANY>

I want to change all the text True for TRUE, and the check for CHECK, in that way I would have an output like:

<COMPANY>   
    <SCHEDULES>
        <OFFICE name="BCO">
            <DEPARTAMENT name="BCO1">
                <days>monday:friday</days>
                <code>TRUE</code>
                <indice>CHECK</indice>
            </DEPARTAMENT>
            <TEAM name="BCO2">
                <days>monday:friday</days>
                <code>TRUE</code>
                <indice>CHECK</indice>
            </TEAM>
        </OFFICE>
        <OFFICE name="RCO">
            <DEPARTAMENT name="RCO1">
                <days>monday:tuesday</days>
                <code>False</code>
                <indice>CHECK</indice>
            </DEPARTAMENT>
            <TEAM name="RCO2">
                <days>monday:wednesday</days>
                <code>TRUE</code>
                <indice>CHECK</indice>
            </TEAM>
        </OFFICE>
        <OFFICE name="LCO">
            <DEPARTAMENT name="LCO1">
                <days>wednesday:saturday</days>
                <code>TRUE</code>
                <indice>CHECK</indice>
            </DEPARTAMENT>
            <TEAM name="LCO2">
                <days>wednesday:saturday</days>
                <code>TRUE</code>
                <indice>CHECK</indice>
            </TEAM>
        </OFFICE>
        <OFFICE name="ACO">
            <DEPARTAMENT name="ACO1">
                <days>monday</days>
                <code>False</code>
                <indice>CHECK</indice>
            </DEPARTAMENT>
            <TEAM name="ACO2">
                <days>tuesday</days>
                <code>False</code>
                <indice>CHECK</indice>
            </TEAM>
        </OFFICE>
    </SCHEDULES>
</COMPANY>

Im trying this XSLT, but its not working, it does not translate the word, I tried to adapt the code I got from another answer but my adaptation doesn't work.

<xsl:stylesheet version="1.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:template match="text()">
    <xsl:value-of select="translate(.,'True','TRUE')"/>
<xsl:value-of select="translate(.,'check','CHECK')"/>
</xsl:template>

</xsl:stylesheet>

CodePudding user response:

As a general rule, the translate() function transforms each individual character into another character. Therefore it cannot be used to replace a word with another word.

In your specific example, you could try:

<xsl:template match="code | indice">
    <xsl:copy>
        <xsl:value-of select="translate(.,'ruechk','RUECHK')"/>
    </xsl:copy>
</xsl:template>

but then "False" would be translated to "FalsE". If you want to keep "False" as it is, then you will need to use a different method, e.g.

<xsl:template match="code[.='True']">
    <xsl:copy>TRUE</xsl:copy>
</xsl:template>

and likewise for "check".

  • Related