private string FindOrAddDataTypeTemplate(string dataTypeName)
        {
            string templateName = "dataType_" + dataTypeName;

            if (this.dataTypeTemplates.ContainsKey(dataTypeName))
            {
                return(templateName);
            }

            SimpleSchema.SchemaObject simpleComplexType = this.simpleSchema.ComplexTypes.SingleOrDefault(y => y.Name == dataTypeName);

            bool dataTypeDefined = this.tdb.ImplementationGuideTypeDataTypes.Count(y =>
                                                                                   y.ImplementationGuideTypeId == this.rootTemplate.ImplementationGuideTypeId &&
                                                                                   y.DataTypeName == dataTypeName) > 0;

            if (simpleComplexType == null || !dataTypeDefined)
            {
                return(string.Empty);
            }

            XmlElement dataTypeTemplate = TransformHelper.CreateXslTemplate(this.transformDoc, templateName, string.Empty);

            dataTypeTemplate.AppendChild(
                TransformHelper.CreateXsltTemplateParam(this.transformDoc, "instance"));

            CompleteDataTypeTemplate(dataTypeTemplate, simpleComplexType, "$instance");
            this.dataTypeTemplates.Add(dataTypeName, dataTypeTemplate);

            return(templateName);
        }
        private void CreateTemplate(GreenTemplate greenTemplate)
        {
            // Create the xsl:template for the green template and add it to the document
            string     name            = Helper.NormalizeName(greenTemplate.Name);
            XmlElement templateElement = TransformHelper.CreateXslTemplate(this.transformDoc, name, Helper.NormalizeName(greenTemplate.Name));

            this.transformDoc.DocumentElement.AppendChild(templateElement);

            // Identify all the root-level constraints within the template
            List <TemplateConstraint> rootTemplateConstraints = (from tc in tdb.TemplateConstraints
                                                                 where tc.TemplateId == greenTemplate.TemplateId &&
                                                                 tc.ParentConstraintId == null
                                                                 select tc).ToList();

            if (!string.IsNullOrEmpty(greenTemplate.Template.TemplateType.RootContext))
            {
                // Create top-level context element
                XmlElement contextElement = this.transformDoc.CreateElement(TransformHelper.IgNamespacePrefix, greenTemplate.Template.TemplateType.RootContext, this.schemaNamespace);
                templateElement.AppendChild(contextElement);

                // TODO: This should be replaced with more generic code for IG types in the future
                // TODO: This should be put in the right location in the output. If there is an ID constraint, the order of elements won't match CDA.xsd
                TemplateConstraint foundTemplateId = (from tc in rootTemplateConstraints
                                                      join tcc in tdb.TemplateConstraints on tc.Id equals tcc.ParentConstraintId
                                                      where tc.Context == "templateId" &&
                                                      tcc.Context == "@root" &&
                                                      tcc.Value == greenTemplate.Template.Oid
                                                      select tc).FirstOrDefault();

                // If a templateId constraint has not been defined at the top level for this template, then we should create
                // a templateId element for it, and add it to the output
                if (foundTemplateId == null)
                {
                    XmlElement templateIdElement = this.transformDoc.CreateElement(TransformHelper.IgNamespacePrefix, "templateId", this.schemaNamespace);
                    templateIdElement.Attributes.Append(
                        TransformHelper.CreateXsltAttribute(this.transformDoc, "root", greenTemplate.Template.Oid));
                    contextElement.AppendChild(templateIdElement);
                }

                rootTemplateConstraints.ForEach(y => CreateTemplateConstraint(contextElement, y));
            }
            else
            {
                rootTemplateConstraints.ForEach(y => CreateTemplateConstraint(templateElement, y));
            }
        }