Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


XML and JSON together

From: Robert Koberg <rob@------.--->
To: xml-dev <xml-dev@-----.---.--->
Date: 4/7/2008 10:58:00 PM
Hi,

Here is an odd idea I am currently playing around with. I know it is a
hack, but what do you think?

The idea is to use JSON to extend legacy XML instance documents while
maintaining original validity. For example, I have some hibernate
mapping documents that use the well defined DTD. I want to extend the
XML syntax to allow for other concerns in the mapping document which
will be used to generate out to multiple concerns. 

With the hibernate mapping document (snippet) below I generate out a
java interface, a default implementation, a java 'GET' validator, a java
'POST' validator and a java 'POST' persistor. I can do all of that with
the standard mappings. But I also want to generate out a useable UI in
HTML. I can't do that from the standard mapping. So, I am using JSON to
augment the syntax while maintaining validity.

I run the mapping document through a very simple XSL 2.0 transformation
(even further below) that uses Dmitre Novatchev's FXSL library and
specifically the json-document function to create an intermediary XML
that is then used to generate the HTML for the form.

Here is a snippet of the mapping document:

<property column="MARRIED" length="1" name="married" not-null="true"
type="boolean">
  <meta attribute="ui"><![CDATA[{
    "uses": "focusObject",
    "row": 1,
    "region": "wide",
    "legend": "Marital Status",
    "err-msg": "Please select your marital status.",
    "as": "radioGroup",
    "items": {
        "radio": {
          "id": "marriedYes",
          "value": true,
          "label": "Married"
        },
        "radio": {
          "id": "marriedNo",
          "value": false,
          "label": "Single",
          "default": true,
          "event": {
            "type": "click",
            "handler": {
              "name": "toggle",
              "args": {
                "container-idref": "domPartnerInfoCont"
              }
            }
          }
        }
      }
    }
  }]]></meta>
</property>
<property column="DOMESTIC_PARTNER_INFO" name="domPartnerInfo"
not-null="true" type="boolean">
  <meta attribute="ui"><![CDATA[{
    "uses": "focusObject",
    "row": 1,
    "region": "wide",
    "container-id": "domPartnerInfoCont",
    "label": "Display Domestic Partner information?",
    "as": "checkbox"
  }]]></meta>
</property>

Running it through the json to xml transform produces:

<hibernate-ui>
  <group column="MARRIED" length="1" name="married" not-null="true"
type="boolean"
    unique="false"
    optimistic-lock="true"
    lazy="false"
    generated="never">
    <uses>focusObject</uses>
    <row>1</row>
    <region>wide</region>
    <legend>Marital Status</legend>
    <err-msg>Please select your marital status.</err-msg>
    <as>radioGroup</as>
    <items>
      <radio>
        <id>marriedYes</id>
        <value>true</value>
        <label>Married</label>
      </radio>
      <radio>
        <id>marriedNo</id>
        <value>false</value>
        <label>Single</label>
        <default>true</default>
        <event>
          <type>click</type>
          <handler>
            <name>toggle</name>
            <args>
              <container-idref>domPartnerInfoCont</container-idref>
            </args>
          </handler>
        </event>
      </radio>
    </items>
  </group>
  <group column="DOMESTIC_PARTNER_INFO" name="domPartnerInfo"
not-null="true"
    type="boolean"
    unique="false"
    optimistic-lock="true"
    lazy="false"
    generated="never">
    <uses>focusObject</uses>
    <row>1</row>
    <region>wide</region>
    <container-id>domPartnerInfoCont</container-id>
    <label>Display Domestic Partner information?</label>
    <as>checkbox</as>
  </group>
</hibernate-ui>

And the XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  version="2.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns:f="http://fxsl.sf.net/"
  exclude-result-prefixes="f xs">
  
  <xsl:import href="fxsl-xslt2/f/func-json-document.xsl"/>
  
  <xsl:output encoding="UTF-8" indent="yes" method="xml"/>
  
  <xsl:template match="/">
    <hibernate-ui>
      <xsl:apply-templates select="//*[meta[@attribute='ui']]"/>
    </hibernate-ui>
  </xsl:template>
  
  <xsl:template match="*[meta[@attribute='ui']]">
    <group>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </group>
  </xsl:template>
  
  <xsl:template match="meta[@attribute='ui']">
    <xsl:copy-of select="f:json-document(.)"/>
  </xsl:template>

</xsl:stylesheet>


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent