本文介绍了通过 XSLT 转换

问题描述

我有以下

<计算><阿拉伯>42</阿拉伯></计算><计算><阿拉伯>137</阿拉伯></计算></root>

我想输出以下内容:

<计算><罗马>XLII</罗马><阿拉伯>42</阿拉伯></计算><计算><罗马>CXXXVII</罗马><阿拉伯>137</阿拉伯></计算></root>

通过编写 XSLT.到目前为止,我编写了这个 XSLT,但还需要做什么才能输出正确的输出?

解决方案

尝试:

<xsl:copy><罗马><xsl:number value="arab" format="I"/></罗马><xsl:apply-templates/></xsl:copy></xsl:模板>

数字应该在 1 到 3999 之间.

要验证数字是否在 1 到 3999 的范围内,您可以执行以下操作:

<xsl:copy><xsl:when test="1 le number(arab) and number(arab) le 3999"><罗马><xsl:number value="arab" format="I"/></罗马></xsl:when><xsl:否则><xsl:message terminate="no">请输入 1 到 3999 之间的数字</xsl:message></xsl:否则></xsl:选择><xsl:apply-templates/></xsl:copy></xsl:模板>

请注意,Saxon 至少支持高达 9999 的罗马数字:http://xsltransform.net/bEzjRKe

I have the following

<root>
    <calc>
        <arab>42</arab>
    </calc>
    <calc>
        <arab>137</arab>
    </calc>
</root>

I want to output the following:

<root>
    <calc>
        <roman>XLII</roman>
        <arab>42</arab>
    </calc>
    <calc>
        <roman>CXXXVII</roman>
        <arab>137</arab>
    </calc>
</root>

By writing a XSLT. So far I wrote this XSLT but what else needs to be done to output the right output?

解决方案

Try:

<xsl:template match="calc">
    <xsl:copy>
        <roman>
            <xsl:number value="arab" format="I"/>
        </roman>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>


To validate that the numbers are in the range of 1 to 3999, you could do:

<xsl:template match="calc">
    <xsl:copy>
        <xsl:choose>
            <xsl:when test="1 le number(arab) and number(arab) le 3999">
                <roman>
                    <xsl:number value="arab" format="I"/>
                </roman>
            </xsl:when>
            <xsl:otherwise>
                <xsl:message terminate="no">Please enter a number between 1 and 3999</xsl:message>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>


Note that Saxon at least supports roman numerals up to 9999:http://xsltransform.net/bEzjRKe

这篇关于通过 XSLT 转换

07-16 19:02
查看更多