Project

Profile

Help

Shouldn't Saxon ensure input fed to xml-to-json is valid against the schema?

Added by Martin Honnen over 2 years ago

Saxon-JS 2 and BaseX reject XML input that doesn't conform to the schema defined at https://www.w3.org/TR/xpath-functions/schema-for-json.xsd. An example is the following with some stray text content (here inserted intentionally in the sample but in a real case produced by some wrong XSLT trying to create the xml-to-json input):

<map xmlns="http://www.w3.org/2005/xpath-functions">
    <string key='desc'>Distances between several cities, in kilometers.</string>
    <string key='updated'>2014-02-04T18:50:45</string>
    <boolean key="uptodate">true</boolean>
    <null key="author"/>
    <map key='cities'>
      <array key="Brussels">
        test
        <map>
            <string key="to">London</string>
            <number key="distance">322</number>
        </map> 
        <map>
            <string key="to">Paris</string>
            <number key="distance">265</number>
        </map> 
        <map>
            <string key="to">Amsterdam</string>
            <number key="distance">173</number>
        </map> 
      </array>
    </map>  
</map>

Saxon 10.5 (both HE and EE) do process the file with e.g. XSLT and

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all"
  expand-text="yes">

  <xsl:output method="text"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="/" name="xsl:initial-template">
    <xsl:value-of select="xml-to-json(., map { 'method' : true() })"/>
  </xsl:template>

</xsl:stylesheet>

and output JSON.

The spec at https://www.w3.org/TR/xpath-functions/#func-xml-to-json says:

Otherwise, the processor may attempt to validate the element against the schema, in which case it is treated as valid if and only if the outcome of validation is valid.

Otherwise (if the processor does not attempt validation using the schema), the processor must ensure that the content of the element, after stripping all attributes (at any depth) in namespaces other than http://www.w3.org/2005/xpath-functions, is such that validation against the schema would have an outcome of valid.

https://saxonica.plan.io/issues/2722 seems related but obviously doesn't seem to cover the sample given above.


Replies (2)

RE: Shouldn't Saxon ensure input fed to xml-to-json is valid against the schema? - Added by Michael Kay over 2 years ago

Yes, this should be an error.

Saxon does have a check for this particular error but the code is incorrect.

    (1-2/2)

    Please register to reply