private static void Main(string[] args) { List <RNGElement> rngElements = new List <RNGElement>(); XDocument xDocument = XDocument.Load(@"c:\users\msavidg\Google Drive\Adobe Specs\Schemas\xfa_3.3\template.rng"); XNamespace rng = "http://relaxng.org/ns/structure/1.0"; foreach (XElement rngDefineElement in xDocument.Descendants(rng + "define").Where(e => e.Descendants(rng + "element").Any())) { RNGElement e = new RNGElement(); var rngElement = rngDefineElement.Element(rng + "element"); if (rngElement != null) { //Console.WriteLine("element=" + rngDefineElement.Attribute("name").Value); e.Name = rngDefineElement.Attribute("name").Value.Replace("Element", String.Empty); var rngComment = rngElement.DescendantNodes().FirstOrDefault(x => x.NodeType == XmlNodeType.Comment); if (rngComment != null) { //Console.WriteLine("comment=" + rngComment); e.Comment = rngComment.ToString(); } var rngRef = rngElement.Element(rng + "ref"); if (rngRef != null) { var rngRefName = rngRef.Attribute("name").Value; var rngDefineContent = xDocument.Descendants(rng + "define").First(d => d.Attribute("name").Value == rngRefName); ProcessDefineContent(e, rngDefineContent, rng); } } //Console.WriteLine(); rngElements.Add(e); } CreateCode(rngElements); foreach (var r in rngElements) { Console.WriteLine("xfaElements.Add(\"" + r.Name + "\", new " + r.Name.FirstCharToUpper() + "Element());"); } }
private static void ProcessDefineContent(RNGElement e, XElement rngDefine, XNamespace rng) { foreach (var rngOptional in rngDefine.Elements(rng + "optional")) { RNGAttribute a = new RNGAttribute(); var attribute = rngOptional.Element(rng + "attribute"); var attributeName = attribute.Attribute("name").Value; //Console.WriteLine("define.optional.attribute=" + attributeName); a.Name = attributeName.Replace("Element", String.Empty); // rng:choice var rngChoice = attribute.Element(rng + "choice"); if (rngChoice != null) { foreach (var rngValue in rngChoice.Elements(rng + "value")) { //Console.WriteLine("define.optional.attribute.choice=" + rngValue.Value); a.Choices.Add(rngValue.Value); } } // rng:data var rngData = attribute.Element(rng + "data"); if (rngData != null) { //Console.WriteLine("define.optional.attribute.data=" + rngData.Attribute("type").Value); a.DataType = rngData.Attribute("type").Value; } //rng:ref var rngRef = attribute.Element(rng + "ref"); if (rngRef != null) { //Console.WriteLine("define.optional.attribute.ref=" + rngRef.Attribute("name").Value); a.DataType = rngRef.Attribute("name").Value.Replace("Element", String.Empty); } e.Attributes.Add(a); } foreach (var rngInterleave in rngDefine.Elements(rng + "interleave")) { foreach (var rngOptional in rngInterleave.Elements(rng + "optional")) { //rng:choice var rngChoice = rngOptional.Element(rng + "choice"); if (rngChoice != null) { foreach (var rngValue in rngChoice.Elements(rng + "value")) { //Console.WriteLine("define.interleave.optional.choice=" + rngValue.Value); e.Children.Add(new RNGChild() { Name = rngValue.Value }); } } //rng:ref var rngRef = rngOptional.Element(rng + "ref"); if (rngRef != null) { //Console.WriteLine("define.interleave.optional.ref=" + rngRef.Attribute("name").Value); e.Children.Add(new RNGChild() { Name = rngRef.Attribute("name").Value.Replace("Element", String.Empty) }); } } foreach (var rngZeroOrMore in rngInterleave.Elements(rng + "zeroOrMore")) { //rng:ref var rngRef = rngZeroOrMore.Element(rng + "ref"); if (rngRef != null) { //Console.WriteLine("define.interleave.zeroOrMore.ref=" + rngRef.Attribute("name").Value); e.Children.Add(new RNGChild() { Name = rngRef.Attribute("name").Value.Replace("Element", String.Empty) }); } } } }