Search code examples
xpathsaxonxslt-3.0

xml-to-json number-formatter entry has no effect


When using Saxon 12.1, I tried to use the function xml-to-json new options entry number-formatter. It appears Saxon recognizes it, but I get no change to my output.

Input 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>

Input 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>

I realize nowhere in the Saxon documentation does it say this is implemented, but it doesn't say it isn't either.


Solution

  • I did a quick test using this query from the command line:

    let $x := <fn:number xmlns:fn='http://www.w3.org/2005/xpath-functions'>25</fn:number> 
    return xml-to-json($x, map{'number-formatter':function($n){'++++'||$n}})
    

    and it produced the output ++++25.

    So the feature is working in principle.

    There's an obvious error in your code which is that you have set the value of the property to hr:really-good rather than hr:really-good#1, so it's interpreted as an element name rather than a function reference. But if I try to replicate that I get a clear error message telling me the value must be a function.

    (delete): ~~That may be related to another error in your code which is that the XML supplied to the xml-to-json function is in the wrong namespace. But again, I would expect that to result in an error message.~~