Re: xpath 3.1 fn:json-to-xml question

On 10/08/2025 19:44, Mukul Gandhi wrote:
>
> I've a question about XPath 3.1 function fn:json-to-xml and its
> duplicates resolution option 'retain', where all the XDM map's keys
> with same name have to be available within fn:json-to-xml's XML result
> serialization.
>
> Please consider following XSLT 3.0 stylesheet example using XPath 3.1
> function fn:json-to-xml.
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>                           xmlns:xs="http://www.w3.org/2001/XMLSchema"
>                           exclude-result-prefixes="xs"
>                           version="3.0">
>       <xsl:output method="xml" indent="yes"/>
>
>       <xsl:variable name="jsonStr" as="xs:string">
>           { "a" : 1,
>             "b" : {"x" : 10, "y" : 11, "x" : 12},
>             "b" : 3,
>             "a" : 4
>           }
>       </xsl:variable>
>
>      <xsl:template match="/">
>          <xsl:sequence select="json-to-xml($jsonStr, map{'duplicates' :
> 'retain'})"/>
>      </xsl:template>
>
> </xsl:stylesheet>
>
> Saxon-HE 12.5 XSLT 3.0 processor emits following XSL transform output
> for this stylesheet:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <map xmlns="http://www.w3.org/2005/xpath-functions">
>     <number key="a">1</number>
>     <map key="b">
>        <number key="x">10</number>
>        <number key="y">11</number>
>        <number key="x">12</number>
>     </map>
>     <number key="b">3</number>
>     <number key="a">4</number>
> </map>
>
> Whereas, Apache Xalan-J's XSLT 3.0 development code, for this
> stylesheet produces following result:
>
> <?xml version="1.0" encoding="UTF-8"?><map
> xmlns="http://www.w3.org/2005/xpath-functions">
>    <number key="a">1</number>
>    <number key="a">4</number>
>    <map key="b">
>      <number key="x">10</number>
>      <number key="x">12</number>
>      <number key="y">11</number>
>    </map>
>    <number key="b">3</number>
> </map>
>
> I think, that both of above mentioned XSL transformation results for
> the two mentioned XSLT processors looks similar, but they're not
> exactly same.
>
> Saxon's XSL transformation result for this example, follows XDM map's
> key order sequence as present within JSON input document, whereas,
> Xalan-J's XSL transformation result has XDM map keys with same name
> adjacent to each other.
>
> My question is, whether Xalan-J's result for this XSL stylesheet
> example, compliant to XPath 3.1 spec for the function fn:json-to-xml?
>
> Many thanks in advance for any answers.
>
I would say the relevant section of the spec 
https://www.w3.org/TR/xpath-functions-31/#json-to-xml-mapping says:


A JSON object is represented by an element named map. The content is a 
sequence of child elements each of which represents one of the 
name/value pairs in the object. The representation of the name/value 
pair N:V is obtained by taking the element that represents the value V 
(by applying these rules recursively) and adding an attribute with name 
key (in no namespace), whose value is N as an instance of xs:string. The 
functions fn:json-to-xml and fn:xml-to-json both retain the order of 
entries, subject to rules about how duplicate keys are handled.


I would interpret that as retaining the order of the entries (name/value 
pairs) if all duplicates are retained, so based on that the result Xalan 
gives breaks the rule to retain the order of entries.


I will try to check the existing XPath 3.1 test suite for test cases 
that perhaps cover that case.

Received on Sunday, 10 August 2025 19:58:06 UTC