I am creating a XSD based on the below Xml using a XSL file. Actually I want to make Element2 block mandatory (minOccurs=1) only when Element1 block is available in each node (Sample, Sample1, etc..). If Element1 block is not available then Element2 block should become optional (minOccurs=0). I tried ancestor::[], match etc., but nothing is working. Please help.
Xml
<Root>
<Sample>
<Element1>
<SubElement1>0</SubElement1>
</Element1>
<Element2>
<X>1</X>
<Y>2</Y>
</Element2>
</Sample>
<Sample1>
<Element2>
<X>1</X>
<Y>2</Y>
</Element2>
</Sample1>
</Root>
XSL
<xsl:choose>
<xsl:when test="(local-name() = 'Element2' and //*[matches(local-name(), 'Element1')])">
<xs:element name="{local-name()}" minOccurs="1">
<xsl:call-template name="renderChildElements" />
</xs:element>
</xsl:when>
<xsl:otherwise>
<xs:element name="{local-name()}" minOccurs="0">
<xsl:call-template name="renderChildElements" />
</xs:element>
</xsl:otherwise>
</xsl:choose>
<xsl:template name="renderChildElements">
<xs:complexType>
<xs:all minOccurs="0">
<xsl:apply-templates select="*"/>
</xs:all>
<xsl:apply-templates select="@*"/>
</xs:complexType>
</xsl:template>