XML.orgXML.org
FOCUS AREAS |XML-DEV |XML.org DAILY NEWSLINK |REGISTRY |RESOURCES |ABOUT
OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]
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]


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 1993-2007 XML.org. This site is hosted by OASIS