Hi,
Can somebody let me know on how to generate an empty XML instance from XML Schema using Java code or XSLT.
Can somebody let me know on how to generate an empty XML instance from XML Schema using Java code or XSLT.
<?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 > 1">
<xsl:variable name="repeat" select="//xs:element[@name = $elementRef]"/>
<xsl:for-each select="document('')[position() < 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 > 1">
<!-- multiple iterations -->
<xsl:variable name="iterations" select="@minOccurs"/>
<xsl:variable name="apply" select="node()"/>
<xsl:for-each select="document('')//*[position() <= $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