2012-05-29 35 views
1

我收到 纬度坐标形式在XML文件中的数据:3876570 经度:-9013376XSL:添加一个额外的数字来的坐标数据

我使用XSL到经度/转换LAT有8位而不是7(如上所述),所以我需要在上述坐标末尾追加一个零。即我需要 纬度:38765700 经度:-90133760

我想使用format-number()函数,但不知道如果我正确使用它。我试图

<xsl:value-of select='format-number(longitude, "########")'/> 

<xsl:value-of select='format-number(longitude, "#######0")'/> 

我最终得到了7位数字本身。请帮忙!

回答

3

您致电format-number不能给你你想要的结果,因为它不能改变它所代表的数字的值。

您可以乘十的值(有没有需要一个format-number通话,只要你使用XSLT 1.0)

<xsl:value-of select="longitude * 10" /> 

或追加零

<xsl:value-of select="concat(longitude, '0')" /> 
-1

明显答案 - 乘以10或连接'0'已被提议。

这里是一个更通用的解决方案

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

<xsl:template match=" 
*[self::latitude or self::longitude 
and 
    not(string-length() >= 8) 
or 
    (starts-with(., '-') and not(string-length() >= 9)) 
    ]"> 

    <xsl:copy> 
    <xsl:value-of select= 
    "concat(., 
      substring('00000000', 
         1, 
         8 + starts-with(., '-') - string-length()) 
      ) 
    "/> 
    </xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 

这种转变在latitudelongitude结束为任何值与string-length()小于8将必要的零的确切数目当应用于此XML文档时

<coordinates> 
<latitude>3876570</latitude> 
<longitude>-9013376</longitude> 
</coordinates> 

有用,正确的结果产生:

<coordinates> 
    <latitude>38765700</latitude> 
    <longitude>-90133760</longitude> 
</coordinates> 

当此XML文档上施加:

<coordinates> 
<latitude>123</latitude> 
<longitude>-99</longitude> 
</coordinates> 

再次有用,正确的结果产生:

<coordinates> 
    <latitude>12300000</latitude> 
    <longitude>-99000000</longitude> 
</coordinates> 

请注意

在表达:

substring('00000000', 
      1, 
      8 + starts-with(., '-') - string-length()) 

我们使用的事实,每当一个布尔值是一个参数的算术运算符,它是使用规则转换为数字是:

number(true()) = 1 

number(false()) = 0 

所以,日如果当前节点的值为负值,则上面的表达式提取一个零,以计算减号并获得我们必须附加到该数字的零的确切数目。

相关问题