← Prev in month
← Prev in thread
Next in thread →
Next in month →
Validating structured XML attributes containing JSON with Schematron
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>
<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>
<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.
<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>
</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
← Prev in month
← Prev in thread
Next in thread →
Next in month →