通过 XSLT 转换 XML 文件将数字转换为罗马数字 [英] Transforming numbers to Roman numbers by transforming an XML file via XSLT

查看:57
本文介绍了通过 XSLT 转换 XML 文件将数字转换为罗马数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 xml 输入:

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

我想输出以下内容:

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

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

<块引用>

<xsl:output method="xml" version="1.0"encoding="UTF-8" indent="yes"/><!-- 身份变换--><xsl:template match="@*|node()"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:模板></xsl:transform>

解决方案

尝试:

<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 xml input:

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

<?xml version="1.0" encoding="UTF-8"?>
    <xsl:transform
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:num="http://whatever"
      version="2.0" exclude-result-prefixes="xs num">

      <xsl:output method="xml" version="1.0"
        encoding="UTF-8" indent="yes"/>


      <!-- identity transform -->
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>

    </xsl:transform>

解决方案

Try:

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


numbers should be between 1 and 3999.

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 转换 XML 文件将数字转换为罗马数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆