Validating structured XML attributes containing JSON with Schematron

Rick Jelliffe <[email protected]> Fri, 5 Apr 2024 11:32:49 +1100
Newsgroups gmane.text.xml.devel
Message-ID <CADUdYQWnfwS=pMLyEsLjUh6BQT3rXyQMHFWQksFU3jdzHSpruA@mail.gmail.com>
--000000000000135b6306154e9962
Content-Type: text/plain; charset="UTF-8"

Probably this is obvious, but you can use Schematron to validate so-called
"structured attributes", which is where an XML attribute value contains
arbitrary structured data to any nesting depth.

(Structured attributes take the pressure off having to use element syntax
for complex metadata, so that e.g. the elements' data content only contains
"text" while the attributes only contains "metadata". )

Lets use JSON as the notation for the structured attributes. Here is a
sample document:

````
<doc>
    <metadata structured-attribute= '
        {
            "a": "dog",
            "b": 5
        }
        '/>
    <a>dog</a>
    <b>4</b>
</doc>
````
The XPath3.1 function json-to-xml() will read this @structured-attribute
into an XDM XML document:
````
          <map xmlns="http://www.w3.org/2005/xpath-functions">
            <string key='a'>dog</string>
            <number key='b'>4</string>
         </map>
````

And here is a Schematron schema with some simple validation involving both
the element values and the structured-attribute values.
````
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron"
queryBinding="xslt3" >
    <sch:ns prefix="json"  uri="http://www.w3.org/2005/xpath-functions" />

    <sch:pattern id="example">
        <sch:let name="metadata"
value="json:json-to-xml(/doc/metadata/@structured-attribute)"  />

        <sch:rule context="a">
            <sch:assert test=". eq
 $metadata/json:map/*[@key='a']/text()">The value of element a should match
the value in the corresponding structured attribute.
                Expecting(<sch:value-of select="."/>) found (<sch:value-of
select="$metadata/json:map/*[@key='a']/text()"/>).
            </sch:assert>
        </sch:rule>

        <sch:rule context="b">
            <sch:assert test="number(.)">Element b should be a
number.</sch:assert>
            <sch:assert test="number(.) =
 number($metadata/json:map/*[@key='b']/text())">The value of element b
should match the value in the corresponding structured attribute.
             Expecting(<sch:value-of select="."/>) found (<sch:value-of
select="$metadata/json:map/*[@key='b']/text()"/>).
           </sch:assert>
        </sch:rule>

    </sch:pattern>
</sch:schema>
````

And the result will be
````
The value of element b should match the value in the corresponding
structured attribute. Expecting(4) found (5).
````

Regards
Rick

--000000000000135b6306154e9962
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Probably this is obvious, but you can use Schematron =
to validate so-called &quot;structured attributes&quot;, which is where an =
XML attribute value contains arbitrary structured data to any nesting depth=
. <br></div><div><br></div><div>(Structured attributes take the pressure of=
f having to use element syntax for complex metadata, so that e.g. the eleme=
nts&#39; data content only contains &quot;text&quot; while the attributes o=
nly contains &quot;metadata&quot;. )</div><div><br></div><div>Lets use JSON=
 as the notation for the structured attributes. Here is a sample document:<=
/div><div><br></div><div>````</div><div>&lt;doc&gt;<br>=C2=A0 =C2=A0 &lt;me=
tadata structured-attribute=3D &#39;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 {<br>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &quot;a&quot;: &quot;dog&quot;,<b=
r>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &quot;b&quot;: 5<br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 }<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 &#39;/&gt;<br>=C2=A0 =C2=
=A0 &lt;a&gt;dog&lt;/a&gt;<br>=C2=A0 =C2=A0 &lt;b&gt;4&lt;/b&gt; <br>&lt;/d=
oc&gt;</div><div>````</div><div>The XPath3.1 function json-to-xml() will re=
ad this=C2=A0<a class=3D"gmail_plusreply" id=3D"plusReplyChip-0">@structure=
d-attribute</a> into an XDM XML document:</div><div>````<br>=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 &lt;map xmlns=3D&quot;<a href=3D"http://www.w3.org/200=
5/xpath-functions">http://www.w3.org/2005/xpath-functions</a>&quot;&gt;<br>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;string key=3D&#39;a&#39;&gt;d=
og&lt;/string&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;number k=
ey=3D&#39;b&#39;&gt;4&lt;/string&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0&=
lt;/map&gt; <br></div><div>````<br></div><div><br></div><div>And here is a =
Schematron schema with some simple validation involving both the element va=
lues and the structured-attribute values. <br></div><div>````</div><div>&lt=
;sch:schema xmlns:sch=3D&quot;<a href=3D"http://purl.oclc.org/dsdl/schematr=
on">http://purl.oclc.org/dsdl/schematron</a>&quot; queryBinding=3D&quot;xsl=
t3&quot; &gt;<br>=C2=A0 =C2=A0 &lt;sch:ns prefix=3D&quot;json&quot; =C2=A0u=
ri=3D&quot;<a href=3D"http://www.w3.org/2005/xpath-functions">http://www.w3=
.org/2005/xpath-functions</a>&quot; /&gt; <br>=C2=A0 =C2=A0 <br>=C2=A0 =C2=
=A0 &lt;sch:pattern id=3D&quot;example&quot;&gt; <br>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 &lt;sch:let name=3D&quot;metadata&quot; value=3D&quot;json:json-to-x=
ml(/doc/metadata/@structured-attribute)&quot; =C2=A0/&gt;<br>=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 <br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;sch:rule context=3D&quot;=
a&quot;&gt; =C2=A0 =C2=A0<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;=
sch:assert test=3D&quot;. eq =C2=A0$metadata/json:map/*[@key=3D&#39;a&#39;]=
/text()&quot;&gt;The value of element a should match the value in the corre=
sponding structured attribute.<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0 Expecting(&lt;sch:value-of select=3D&quot;.&quot;/&gt;) foun=
d (&lt;sch:value-of select=3D&quot;$metadata/json:map/*[@key=3D&#39;a&#39;]=
/text()&quot;/&gt;). <br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;/sch=
:assert&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;/sch:rule&gt; <br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 <br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;sch:rule context=3D&q=
uot;b&quot;&gt; =C2=A0<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;sch=
:assert test=3D&quot;number(.)&quot;&gt;Element b should be a number.&lt;/s=
ch:assert&gt; <br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;sch:assert =
test=3D&quot;number(.) =3D =C2=A0number($metadata/json:map/*[@key=3D&#39;b&=
#39;]/text())&quot;&gt;The value of element b should match the value in the=
 corresponding structured attribute. <br></div><div>=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Expecting(&lt;sch:value=
-of select=3D&quot;.&quot;/&gt;) found (&lt;sch:value-of select=3D&quot;$me=
tadata/json:map/*[@key=3D&#39;b&#39;]/text()&quot;/&gt;). =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 <br></div><div>=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;/sch=
:assert&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;/sch:rule&gt;<br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 <br>=C2=A0 =C2=A0 &lt;/sch:pattern&gt;<br>&lt;/sch:schema=
&gt;</div><div>````</div><div><br></div><div>And the result will be</div><d=
iv>````</div><div>The value of element b should match the value in the corr=
esponding structured attribute. Expecting(4) found (5).</div><div>````<br><=
/div><br><div>Regards</div><div>Rick<br></div><br></div>

--000000000000135b6306154e9962--