You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Saxon 12.1中xml-to-json的number-formatter选项无效果问题咨询

Saxon 12.1中xml-to-json函数的number-formatter选项无效果

使用Saxon 12.1时,尝试在xml-to-json函数中使用新增的number-formatter选项,发现Saxon能识别该选项,但输出完全没有变化。Saxon官方文档中既未明确说明该功能已实现,也未提及它尚未实现。

输入XML

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="repro.xsd">
<OptionPosition>-27975240</OptionPosition>
</root>

输入XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns="http://www.w3.org/2005/xpath-functions" xmlns:hr="hr" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:math="http://www.w3.org/2005/xpath-functions/math" xmlns:array="http://www.w3.org/2005/xpath-functions/array" xmlns:map="http://www.w3.org/2005/xpath-functions/map" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:err="http://www.w3.org/2005/xqt-errors" exclude-result-prefixes="array fn map math xhtml xs err" version="3.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <!--<xsl:import-schema schema-location="repro.xsd"/>--> 
    <xsl:function name="hr:really-good" as="xs:string">
        <xsl:param name="input" as="xs:string"/>
        1</xsl:function>
    <xsl:template match="/" name="xsl:initial-template">
        <xsl:variable name="mine">
            <map>
                <array key="AdjudicatorName">                   
                        <number>
                            <xsl:value-of select="/root/OptionPosition"/>
                        </number>           
                </array>                
            </map>
        </xsl:variable>
        <xsl:value-of select="xml-to-json($mine, map{'number-formatter': hr:really-good })" />
    </xsl:template>     
</xsl:stylesheet>

问题分析与解决

  • 参数类型不匹配:XPath规范要求number-formatter对应的函数必须接收xs:double类型的输入,你定义的hr:really-good参数是xs:string,不符合要求,导致函数从未被正确调用。
  • 修正函数定义:把参数类型改为xs:double,并实现实际的格式化逻辑,示例如下:
    <xsl:function name="hr:really-good" as="xs:string">
        <xsl:param name="input" as="xs:double"/>
        <!-- 这里可以替换为你需要的格式化规则,比如带千分位 -->
        <xsl:value-of select="format-number($input, '#,##0')"/>
    </xsl:function>
    
  • Saxon的实现情况:Saxon 12.1实际支持number-formatter选项,但严格遵循规范,只要函数符合参数要求就能生效。文档未明确标注状态属于疏漏,但功能可用。

内容的提问来源于stack exchange,提问作者J. Nicholas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 15:40:04