Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 通过输入舍入所有数值进行XSL解析_Xml_Xslt_Xslt 1.0 - Fatal编程技术网

Xml 通过输入舍入所有数值进行XSL解析

Xml 通过输入舍入所有数值进行XSL解析,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我需要解析一个文档,并删除每个标记的所有0结尾的小数位。结果应该与输入相同,但将所有“10.00”转换为“10”,例如 以下是一些可能出现的结构的虚构示例: <Order> <OrderId>1234567890</OrderId> <TotalOrder>506.40</TotalOrder> <TotalTax>17.00</TotalTax> <Currency>

我需要解析一个文档,并删除每个标记的所有0结尾的小数位。结果应该与输入相同,但将所有“10.00”转换为“10”,例如

以下是一些可能出现的结构的虚构示例:

<Order>
    <OrderId>1234567890</OrderId>
    <TotalOrder>506.40</TotalOrder>
    <TotalTax>17.00</TotalTax>
    <Currency>XYZ</Currency>
</Order>

1234567890
506.40
17
XYZ
应导致:

<Order>
    <OrderId>1234567890</OrderId>
    <TotalOrder>506.4</TotalOrder>
    <TotalTax>17</TotalTax>
    <Currency>XYZ</Currency>
</Order>

1234567890
506.4
17
XYZ
需要注意的一点是,Order的孩子可能会有所不同。因此,我的目的不是为每一种可能性制作一个模板匹配

到目前为止,我所做的似乎是正确的,但结果如下:

<OrderId>1.23456789E9</OrderId>
<TotalOrder>506.4</TotalOrder>
<TotalTax>17</TotalTax>
1.23456789E9
506.4
17
迄今为止构建的Xsl:

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

<xsl:template match="node()">
    <xsl:for-each select="node()">
        <xsl:if test="string(number(node())) != 'NaN'">
            <xsl:copy>
                <xsl:value-of select="number(.)"/>
            </xsl:copy>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

它必须符合1.0标准

有什么想法吗


最终XSL(欢迎提出更简洁的建议):


尝试使用此方法,而不是当前的xsl:copy

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

只要在“0”前面加上你所需要的数量

和你一起做这个我得到的例子

<OrderId>1234567890</OrderId>
<TotalOrder>506.4</TotalOrder>
<TotalTax>17</TotalTax>
1234567890
506.4
17

我建议您从一个开始,然后使用模板覆盖您要更改的特定内容,例如

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- identity transform templates -->
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="*">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@* | comment() | processing-instruction()">
        <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="text()">
        <xsl:copy-of select="."/>
    </xsl:template>

    <!-- match the text nodes where the text is a number -->
    <xsl:template match="text()[number(.) = number(.)]">
        <xsl:value-of select="format-number(., '#########0.##')"/>
    </xsl:template>
</xsl:stylesheet>


谢谢@RT72的建议!我对最后的表达式做了一些修改,将字符串标记“Currency”包含到输出中。这个答案看起来更简洁,谢谢!我认识到的另一件事是,我的解决方案不适用于嵌套结构,而您的解决方案可以完美地处理它们。我一定会更深入地研究这个概念。
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- identity transform templates -->
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="*">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@* | comment() | processing-instruction()">
        <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="text()">
        <xsl:copy-of select="."/>
    </xsl:template>

    <!-- match the text nodes where the text is a number -->
    <xsl:template match="text()[number(.) = number(.)]">
        <xsl:value-of select="format-number(., '#########0.##')"/>
    </xsl:template>
</xsl:stylesheet>