Generating empty XML instance from XML Schema

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jagadish Babu
    New Member
    • Mar 2008
    • 1

    Generating empty XML instance from XML Schema

    Hi,

    Can somebody let me know on how to generate an empty XML instance from XML Schema using Java code or XSLT.
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Interesting problem. You are aware that this is not a small problem correct?

    Start with
    <xsl:template match="xs:schem a">
    <xsl:apply-templates select="xs:elem ent[1]"/>
    </xsl:template>


    Problems:
    Create a text for any given Regular Expression.
    Enforcing keys, and/or uniqueness.

    Will make an attempt to write an xsl for basic use. You may hear back later.

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      Here is a pretty basic one that doesn't cover everything.
      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      	<xsl:variable name="namespace">
      		<xsl:choose>
      			<xsl:when test="/xs:schema/@elementFormDefault = 'qualified'">
      				<xsl:value-of select="/xs:schema/@targetNamespace"/>
      			</xsl:when>
      			<xsl:otherwise>
      			</xsl:otherwise>
      		</xsl:choose>
      	</xsl:variable>
      	<xsl:template match="/xs:schema">
      		<xsl:apply-templates select="xs:element[1]"/>
      	</xsl:template>
      	<xsl:template match="xs:element[@minOccurs=0] "/>	<!-- no output -->	
      	<xsl:template match="xs:element[@ref]">
      		<xsl:variable name="elementRef" select="@ref"/>  
      		<xsl:apply-templates select="//xs:element[@name = $elementRef]"/>
      		<xsl:if test="@minOccurs &gt; 1">
      			<xsl:variable name="repeat" select="//xs:element[@name = $elementRef]"/>
      			<xsl:for-each select="document('')[position() &lt; current()/@minOccurs]">
      				<xsl:apply-templates select="$repeat"/>
      			</xsl:for-each>
      		</xsl:if>
      	</xsl:template>	
      	<xsl:template match="xs:element[@name]">
      		<xsl:element name="{@name}">
      			<xsl:apply-templates select="node()"/>
      		</xsl:element>
      	</xsl:template>
      	<xsl:template match="xs:annotation"/>
      	<xsl:template match="xs:complexType">
      		<xsl:apply-templates/>
      	</xsl:template>
      	<xsl:template match="xs:simpleType">
      		<xsl:apply-templates select="xs:extension/@base | xs:restriction/@base | @type"/>
      	</xsl:template>
      	
      	<xsl:template match="@base|@type">
      			<xsl:choose>
      				<xsl:when test=".='xs:string'"></xsl:when>              
      				<xsl:when test=".='xs:boolean'">true</xsl:when>         
      				<xsl:when test=".='xs:decimal'">1.1</xsl:when>          
      				<xsl:when test=".='xs:float'">1.2</xsl:when>            
      				<xsl:when test=".='xs:double'">1.3</xsl:when>           
      				<xsl:when test=".='xs:duration'">P15D</xsl:when>        
      				<xsl:when test=".='xs:double'">1.3</xsl:when>           
      				<xsl:when test=".='xs:dateTime'">2002-05-30T09:30:10</xsl:when>
      				<xsl:when test=".='xs:time'">09:30:10</xsl:when>        
      				<xsl:when test=".='xs:date'">2002-05-30</xsl:when>      
      				<xsl:when test=".='xs:integer'">-1</xsl:when>           
      				<xsl:when test=".='xs:positiveInteger'">1</xsl:when>	
      			</xsl:choose>		
      	</xsl:template>
      	<xsl:template match="xs:sequence">
      		<xsl:choose>
      			<xsl:when test="@minOccurs = 0"/>
      			<!-- truncate -->
      			<xsl:when test="@minOccurs &gt; 1">
      				<!-- multiple iterations -->
      				<xsl:variable name="iterations" select="@minOccurs"/>
      				<xsl:variable name="apply" select="node()"/>
      				<xsl:for-each select="document('')//*[position() &lt;= $iterations]">
      					<xsl:apply-templates select="$apply"/>
      				</xsl:for-each>
      			</xsl:when>
      			<xsl:otherwise>
      				<xsl:apply-templates/>
      			</xsl:otherwise>
      		</xsl:choose>
      	</xsl:template>
      	<xsl:template match="xs:attribute[@use = 'optional']"/>
      	<xsl:template match="xs:attribute">
      		<xsl:attribute name="{@name}">
      			<xsl:choose>
      				<xsl:when test="@type"><xsl:apply-templates select="simpleType[@name = current()/@type]"/></xsl:when>
      				<xsl:otherwise><xsl:apply-templates/></xsl:otherwise>
      			</xsl:choose>
      		</xsl:attribute>
      	</xsl:template>
      	<!--
          <xsl:template match="xs:element[@type]">
             <xsl:variable name="elementType" select="@type"/>
             <xsl:apply-templates select="//xs:complexType[@name = $elementType]"/>
             <xsl:apply-templates select="//xs:simpleType[@name = $elementType]"/>
          </xsl:template>-->
      	<!-- has to be one or the other -->
      </xsl:stylesheet>

      Comment

      Working...