[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Validating structured XML attributes containing JSON with Schematron

  • From: Rick Jelliffe <rjelliffe@allette.com.au>
  • To: xml-dev <xml-dev@lists.xml.org>
  • Date: Fri, 5 Apr 2024 11:32:49 +1100

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>
````
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



[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.