json-patch-002

Example test from RFC 6092.

Test is expected to pass.

The pipeline

<p:declare-step xmlns:cx="http://xmlcalabash.com/ns/extensions"
                xmlns:p="http://www.w3.org/ns/xproc"
                xmlns:t="http://xproc.org/ns/testsuite/3.0" name="main" version="3.0"
                expand-text="false">
   <p:import href="https://xmlcalabash.com/ext/library/json-patch.xpl"/>
   <p:output port="result"/>
   <cx:json-patch cx:expand-text="false">
      <p:with-input>
         <p:inline content-type="application/json">{ "foo": ["bar", "baz"]}</p:inline>
      </p:with-input>
      <p:with-input port="patch">
         <p:inline content-type="application/json">
        [
          { "op": "add", "path": "/foo/1", "value": "qux" }
        ]
      </p:inline>
      </p:with-input>
   </cx:json-patch>
   <p:cast-content-type content-type="application/xml"/>
</p:declare-step>

Result

<map xmlns="http://www.w3.org/2005/xpath-functions"
     xmlns:t="http://xproc.org/ns/testsuite/3.0">
   <array key="foo">
      <string>bar</string>
      <string>qux</string>
      <string>baz</string>
   </array>
</map>

Schematron checks

<s:schema xmlns:s="http://purl.oclc.org/dsdl/schematron"
          xmlns:t="http://xproc.org/ns/testsuite/3.0" queryBinding="xslt2">
   <s:ns prefix="fn"
         uri="http://www.w3.org/2005/xpath-functions"/>
   <s:pattern>
      <s:rule context="/">
         <s:assert test="fn:map">The root is not map.</s:assert>
         <s:assert test="count(fn:map/*) = 1">Wrong number of keys.</s:assert>
         <s:assert test="exists(fn:map/fn:array[@key='foo'])">foo isn’t an array</s:assert>
         <s:assert test="fn:map/fn:array[@key='foo']/fn:string[1] = 'bar'">foo[1] != bar</s:assert>
         <s:assert test="fn:map/fn:array[@key='foo']/fn:string[2] = 'qux'">foo[2] != bar</s:assert>
         <s:assert test="fn:map/fn:array[@key='foo']/fn:string[3] = 'baz'">foo[3] != bar</s:assert>
      </s:rule>
   </s:pattern>
</s:schema>

Revision history

09 May 2025, Norm Tovey-Walsh
Created test.