2017-07-27 2 views
2

を経由してXMLファイルを変換することによって、ローマ数字に数値を変換するは、私は次のXML入力を持っているXSLT

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

XSLTを書き込むこと。今まで私はこのXSLTを書いていましたが、正しい出力を出力するために何が必要ですか?

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

答えて

4

試行:

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

数字は数字の範囲内にあることを検証するには1と3999

間であるべきです1〜 3999は、あなたができる:

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

注サクソンは、少なくとも9999までローマ数字サポートしていること: http://xsltransform.net/bEzjRKe

+0

をあなたはおそらく ' ..'ラウンドの出力を含めることが、なぜXSLT 1または2を主張する?この解決策は、どちらかでも動作します。 – Flynn1179

+1

@ Flynn1179キャッチのおかげで - バージョンに関しては、私は答えを探す場所を知るように頼んだ。それが起こると、答えはXSLT 1.0でも機能しますが、必ずしもそうではありません。 –

+0

バリデーションを追加するにはどうしたらいいですか?数字0を入力するとエラーになり、またXSLTは3xxxまでの数字を処理できます。したがって、ローマ数字の中で最も必要な手紙は「M」になります。例えば、3999以上の数字は無効にする必要があります。したがって、短い数字は1〜3999の間である必要があります。 – habed

関連する問題