我的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标签中有不允许的子属性。我的问题是我如何重写这个,使我不再得到这个警告?
每个属性都是在Java中使用区域设置从DecimalFormatSymbols中设置的,这可能就是为什么这样写的原因。
这是正确的解决方案吗?
<xsl:decimal-format name="symbols" NaN="0"
decimal-separator="{$DecimalSeparator}"
infinity="{$Infinity}"
>
等。
我假设$DecimalSeparator和$GroupingSeparator等是您打算传递到XSLT样式表中的参数。您还可以采用另一种方法,尽管它确实涉及到在另一个转换上进行一些预处理。
XSLT文档碰巧也是格式良好的XML文档。这意味着您可以在XSLT文档上应用XSLT转换。因此,您可以创建一个新的XSLT文档,该文档可以应用于当前的XSLT,只需复制它,但还将属性添加到xsl:decimal-format。
过程如下:
- 加载当前XSLT
- 加载'本地化' XSLT
- 将'本地化' XSLT应用到当前XSLT以创建一个新的'本地化' XSLT(在内存中)
- 将此"本地化"XSLT应用于当前XML
例如,假设您有当前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看起来像这样
<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>
如果将'本地化' 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中,希望能得到所需的结果。