十进制格式的孩子

时间:2014-03-25 10:12:54

标签: xml xslt

我的XSL文件出现以下错误:

cvc-complex-type.2.1: Element 'xsl:decimal-format' must have no character or element information item [children], because the type's content type is empty.

这是因为我有以下代码:

<xsl:decimal-format name="symbols" NaN="0">
    <xsl:attribute name="decimal-separator"><xsl:value-of select="$DecimalSeparator" /></xsl:attribute>
    <xsl:attribute name="grouping-separator"><xsl:value-of select="$GroupingSeparator" /></xsl:attribute>
    <xsl:attribute name="infinity"><xsl:value-of select="$Infinity" /></xsl:attribute>
    <xsl:attribute name="minus-sign"><xsl:value-of select="$MinusSign" /></xsl:attribute>
    <xsl:attribute name="NaN"><xsl:value-of select="$NaN" /></xsl:attribute>
    <xsl:attribute name="percent"><xsl:value-of select="$Percent" /></xsl:attribute>
    <xsl:attribute name="per-mille"><xsl:value-of select="$PerMill" /></xsl:attribute>
    <xsl:attribute name="zero-digit"><xsl:value-of select="$ZeroDigit" /></xsl:attribute>
    <xsl:attribute name="digit"><xsl:value-of select="$Digit" /></xsl:attribute>
    <xsl:attribute name="pattern-separator"><xsl:value-of select="$PatternSeparator" /></xsl:attribute>
</xsl:decimal-format>

xsl:decimal-format标记中有子属性,这是不允许的。我的问题是如何重写这个以便我不再收到此警告?

使用Locale从DecimalFormatSymbols在Java中设置每个属性,这可能就是为什么用这种方式编写的。

这是正确的解决方案吗?

<xsl:decimal-format name="symbols" NaN="0" 
    decimal-separator="{$DecimalSeparator}"
    infinity="{$Infinity}"
>

1 个答案:

答案 0 :(得分:0)

我假设 $ DecimalSeparator $ GroupingSeparator 等是您打算传递到XSLT样式表的参数?您可以采取另一种方法,但它确实涉及在另一次转换的形式中进行一些预处理。

XSLT文档也恰好是格式良好的XML文档。这意味着您可以在XSLT文档上应用XSLT转换。因此,您可以创建一个新的XSLT文档,该文档可以应用于您只需复制它的当前XSLT,但也会将属性添加到 xsl:decimal-format

该过程如下:

  1. 加载您当前的XSLT
  2. 加载&#39;本地化&#39; XSLT
  3. 应用&#39;本地化&#39; XSLT到您当前的XSLT创建一个新的本地化的&#39; XSLT(在内存中)
  4. 应用本地化的&#39; XSLT到您当前的XML
  5. 例如,考虑您有当前的XSLT

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
        <xsl:output method="xml" indent="yes"/>
    
        <xsl:decimal-format name="local" decimal-separator="." grouping-separator=","/>
    
        <xsl:template match="/">
            <xsl:value-of select="format-number(26825.8, '#.###,00', 'local')"/>
        </xsl:template>
    </xsl:stylesheet>
    

    本地化&#39;然后XSLT看起来像这样

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
        <xsl:output method="xml" indent="yes"/>
    
        <xsl:param name="DecimalSeparator" select="','" />
        <xsl:param name="GroupingSeparator" select="'.'" />
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="xsl:decimal-format">
            <xsl:copy>
                <xsl:copy-of select="@name" />
                <xsl:attribute name="decimal-separator"><xsl:value-of select="$DecimalSeparator" /></xsl:attribute>
                <xsl:attribute name="grouping-separator"><xsl:value-of select="$GroupingSeparator" /></xsl:attribute>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
    

    如果你应用了本地化&#39; XSLT到原来的XSLT(根据需要传入参数),输出结果如下:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
        <xsl:output method="xml" indent="yes"/>
    
        <xsl:decimal-format name="local" decimal-separator="," grouping-separator="."/>
    
        <xsl:template match="/">
            <xsl:value-of select="format-number(26825.8, '#.###,00', 'local')"/>
        </xsl:template>
    </xsl:stylesheet>
    

    然后,您可以将这个新的XSLT应用于您的XML,希望能够为您提供所需的结果。

相关问题