简体   繁体   English

如何使用 XSLT 1.0 翻译 XML 中的多个字符串文本

[英]how to translate multiples string text in a XML using XSLT 1.0

I want to translate different lines of text in an XML using XSLT, I have the following XML我想使用 XSLT 翻译 XML 中的不同文本行,我有以下 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:我想将所有文本 True 更改为 TRUE,并检查 CHECK,这样我会得到如下输出:

<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.我正在尝试这个 XSLT,但它不起作用,它不能翻译这个词,我试图调整从另一个答案中得到的代码,但我的调整不起作用。

<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>

As a general rule, the translate() function transforms each individual character into another character.作为一般规则, translate()函数将每个单独的字符转换为另一个字符。 Therefore it cannot be used to replace a word with another word.因此,它不能用于将一个词替换为另一个词。

In your specific example, you could use:在您的具体示例中,您可以使用:

<xsl:template match="text()">
    <xsl:value-of select="translate(.,'ruechek','RUECHEK')"/>
</xsl:template>

but that's only because you are in fact only changing the case of each character.但这只是因为您实际上只是在更改每个字符的大小写。


See another example here: https://stackoverflow.com/a/53232710/3016153在此处查看另一个示例: https ://stackoverflow.com/a/53232710/3016153

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM