Metah.X: An XML Metaprogramming Language
Metah.X(简称MX)用自创的语法实现了XML Schema 1.0的语义,并且用C#实现了一个Schema-lized Document Object Model (SDOM),编译器编译MX代码后将生成使用SDOM的C#代码,这将XML Schema的语义映射到C#上,从而完全释放出XML Schema的力量。尽管现在只有C#版,实现Java版或其它语言版本是完全可能的。
MX是个开源项目,欢迎参与,比如实现Java版或其它语言版本;MX没有定型,欢迎提出修改意见。请访问:http://metah.codeplex.com/。
XML Schema定义了XML数据(或叫XML实例)的形状及需要遵守的规则,比如下面的XSD代码:
下面是合法的XML数据:
因为XSD相当繁琐不便于书写,MX自创了用户友好的语法来表达XML Schema的语义,下面的MX代码和上面的XSD代码表达了相同的语义:
MX编译器编译HelloWorld.mxcs后,将生成如下的C#代码:
使用编译器生成的代码,就可以创建、查询、修改、保存、装载及验证XML数据,下面的手写代码演示了如何使用编译器生成的代码:
下面是d:\customer.xml的内容:
下面是d:\customer2.xml的内容:
也就是说,MX的用处 = XML Schema的用处 + Document Object Model的用处。欲知详情,请访问:http://metah.codeplex.com/
MX是个开源项目,欢迎参与,比如实现Java版或其它语言版本;MX没有定型,欢迎提出修改意见。请访问:http://metah.codeplex.com/。
XML Schema定义了XML数据(或叫XML实例)的形状及需要遵守的规则,比如下面的XSD代码:
<?xml version="1.0" encoding="utf-8"?> <!--HelloWorld.xsd--> <schema targetNamespace="http://schemas.example.com/projecta" elementFormDefault="qualified" xmlns:tns="http://schemas.example.com/projecta" xmlns="http://www.w3.org/2001/XMLSchema"> <simpleType name="String20"> <restriction base="string"> <minLength value="1"></minLength> <maxLength value="20"></maxLength> </restriction> </simpleType> <simpleType name="PhoneCategory"> <restriction base="string"> <enumeration value="Unknown"></enumeration> <enumeration value="Work"></enumeration> <enumeration value="Home"></enumeration> </restriction> </simpleType> <complexType name="Phone"> <simpleContent> <extension base="tns:String20"> <attribute name="Category" use="optional" type="tns:PhoneCategory"></attribute> </extension> </simpleContent> </complexType> <complexType name="Customer"> <sequence> <element name="Phone" minOccurs="1" maxOccurs="unbounded" type="tns:Phone"></element> </sequence> <attribute name="Name" use="required" type="tns:String20"></attribute> <attribute name="Email" use="required"> <simpleType> <restriction base="tns:String20"> <pattern value="[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}"></pattern> </restriction> </simpleType> </attribute> <attribute name="RegistrationDate" use="optional" type="dateTime"></attribute> </complexType> <element name="Customer" type="tns:Customer"></element> </schema>
下面是合法的XML数据:
<e0:Customer Name="Tank" Email="tank@example.com" RegistrationDate="2014-04-15T16:56:53.0568964Z" xmlns:e0="http://schemas.example.com/projecta"> <e0:Phone Category="Work">12345678-334</e0:Phone> <e0:Phone>87654321</e0:Phone> </e0:Customer>
因为XSD相当繁琐不便于书写,MX自创了用户友好的语法来表达XML Schema的语义,下面的MX代码和上面的XSD代码表达了相同的语义:
//HelloWorld.mxcs xnamespace {"http://schemas.example.com/projecta"} [namespace: Example.ProjectA] { type String20 restrict String facets { lengthrange: 1..20; }; ; type PhoneCategory restrict String facets{ enums: Unknown = "Unknown", Work = "Work", Home = "Home" }; ; type Phone extend String20 attributes { attribute Category[?] as PhoneCategory; }; ; type Customer attributes { attribute Name as String20; attribute Email as type restrict String20 facets { patterns: @"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}"; }; ; ; attribute RegistrationDate[?] as DateTime; }; children { element Phone[+; membername: Phones] as Phone; }; ; element Customer as Customer; }
MX编译器编译HelloWorld.mxcs后,将生成如下的C#代码:
//HelloWorld.mxcs.cs //Generated by MX compiler namespace Example.ProjectA { public partial class PhoneCategory : ... { public static readonly string @Unknown = "Unknown"; public static readonly string @Work = "Work"; public static readonly string @Home = "Home"; ... } public partial class Phone : ... { public partial class AttributeSetClass : ... { public string Category_Value { get; set; } ... } public AttributeSetClass AttributeSet { get; set; } public AttributeSetClass EnsureAttributeSet(); public string Value { get; set; } ... } public partial class Customer : ... { public partial class AttributeSetClass : ... { public string Name_Value { get; set; } public string Email_Value { get; set; } public DateTime? RegistrationDate_Value { get; set; } ... } public AttributeSetClass AttributeSet { get; set; } public AttributeSetClass EnsureAttributeSet(); public partial class ComplexChildClass : ... { public partial class Phones_Class : ... { public partial class ItemClass : ... { public Phone Type { get; set; } ... } public ItemClass CreateAndAddItem(); ... } public Phones_Class Phones { get; set; } public Phones_Class Ensure_Phones(); ... } public ComplexChildClass ComplexChild { get; set; } public ComplexChildClass EnsureComplexChild(); ... } public partial class Customer_ElementClass : ... { public Customer Type { get; set; } public static bool TryLoadAndValidate(XmlReader reader, Metah.X.Context context, out Customer_ElementClass result); ... } }
使用编译器生成的代码,就可以创建、查询、修改、保存、装载及验证XML数据,下面的手写代码演示了如何使用编译器生成的代码:
//Program.cs using System; using System.Xml;//for XmlReader & XmlWriter using X = Metah.X; namespace Example.ProjectA { class Program { static void Main(string[] args) { var customer = new Customer(); var cattset = customer.EnsureAttributeSet(); cattset.Name_Value = "Tank"; cattset.Email_Value = "tank@example.com"; cattset.RegistrationDate_Value = DateTime.Now; var phones = customer.EnsureComplexChild().Ensure_Phones(); var phone = phones.CreateAndAddItem(); phone.EnsureAttributeSet().Category_Value = PhoneCategory.Work; phone.Value = "12345678-334"; phones.CreateAndAddItem().Value = "87654321"; var customerElement = new Customer_ElementClass { Type = customer }; using (var writer = XmlWriter.Create(@"d:\customer.xml", new XmlWriterSettings { Indent = true })) customerElement.Save(writer); // var ctx = new X.Context(); using (var reader = XmlReader.Create(@"d:\customer.xml")) { Customer_ElementClass customerElement2; if (Customer_ElementClass.TryLoadAndValidate(reader, ctx, out customerElement2)) { var customer2 = customerElement2.Type; Console.WriteLine("Name={0}, Email={1}, RegistrationDate={2}", customer2.AttributeSet.Name_Value, customer2.AttributeSet.Email_Value, customer2.AttributeSet.RegistrationDate_Value); foreach (var phone2 in customer2.ComplexChild.Phones) { Console.WriteLine("\tCategory={0}, Value={1}", phone2.Type.AttributeSet.Category_Value, phone2.Type.Value); } customer2.AttributeSet.Name_Value += "-Knat"; customer2.AttributeSet.RegistrationDate = null; var phone3 = customer2.ComplexChild.Phones.CreateAndAddItem(); phone3.EnsureAttributeSet().Category_Value = PhoneCategory.Home; phone3.Value = "11223344"; using (var writer = XmlWriter.Create(@"d:\customer2.xml", new XmlWriterSettings { Indent = true })) customerElement2.Save(writer); } else { foreach (var diag in ctx.Diagnostics) Console.WriteLine(diag); } } } } }
下面是d:\customer.xml的内容:
<?xml version="1.0" encoding="utf-8"?> <e0:Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="e0:Customer" Name="Tank" Email="tank@example.com" RegistrationDate="2014-04-17T08:45:21.4460232Z" xmlns:e0="http://schemas.example.com/projecta"> <e0:Phone xsi:type="e0:Phone" Category="Work">12345678-334</e0:Phone> <e0:Phone xsi:type="e0:Phone">87654321</e0:Phone> </e0:Customer>
下面是d:\customer2.xml的内容:
<?xml version="1.0" encoding="utf-8"?> <e0:Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="e0:Customer" Name="Tank-Knat" Email="tank@example.com" xmlns:e0="http://schemas.example.com/projecta"> <e0:Phone xsi:type="e0:Phone" Category="Work">12345678-334</e0:Phone> <e0:Phone xsi:type="e0:Phone">87654321</e0:Phone> <e0:Phone xsi:type="e0:Phone" Category="Home">11223344</e0:Phone> </e0:Customer>
也就是说,MX的用处 = XML Schema的用处 + Document Object Model的用处。欲知详情,请访问:http://metah.codeplex.com/
相关推荐
Serving as a tutorial as well as a handbook for experts, this is the book on C++ template metaprogramming."Chuck Allison, Editor, The C++ Source C++ Template Metaprogramming sheds light on the most...
《Addison.Wesley.C++.Template.Metaprogramming.Concepts.Tools.and.Techniques》这本书深入探讨了C++模板元编程这一高级编程技术。模板元编程是C++语言中的一个独特领域,它允许程序员在编译时执行计算,极大地...
内容简介 本书是关于C++模板元编程的著作。本书主要介绍Traits和类型操纵、深入...附赠光盘内容包含所有Boost C++ 程序库:Boost Metaprogramming Library及其参考文件,还包含所有随书代码示例以及大量的补充材料。
Metaprogramming is one of Elixir's greatest features. Maybe you've played with the basics or written a few macros. Now you want to take it to the next level. This book is a guided series of ...
Author Luciano Ramalho takes you through Python’s core language features and libraries, and shows you how to make your code shorter, faster, and more readable at the same time. Many experienced ...
C++.Template.Metaprogramming.-.companion.cd.rar
be aware of template metaprogramming. 8. 定制new和delete customizing new and delete 条款49:了解new-handler的行为 understand the behavior of the new-handler. 条款50:了解new和delete的合理替换时机 ...
Title: Learn C++ Programming Language: Become A Complete C++ Programmer Author: Virender Singh Length: 1018 pages Edition: 1 Language: English Publication Date: 2015-10-01 This Book Teaches C++ In ...
在 SimpleMock.jl 中,你还可以利用元编程(metaprogramming)特性来实现更复杂的模拟场景,例如动态改变模拟行为或模拟静态方法。这使得 SimpleMock.jl 在处理 Julia 的多态性和类型系统时具有很高的灵活性。 总的...
这种表示方式非常适合表示程序代码,因为代码可以被视作数据来处理,从而实现元编程(metaprogramming)的能力。在Lisp中,整个程序就是一系列S表达式的集合。而在Julia中,虽然其语法与Lisp有所不同,但...
库中的另一个亮点是它的元编程(Metaprogramming)支持。ConstructionBase.jl允许开发者动态地生成构造函数,这在处理大量相似结构体或者需要根据运行时信息生成构造函数的场景中特别有用。元编程技术让代码更具有...
1. **元编程(Metaprogramming)** Elixir的元编程能力是其一大亮点,允许开发者在运行时编写和修改代码。例如,`Macro`模块提供了创建宏的能力,可以在编译时生成代码,这在定义DSL(领域特定语言)时非常有用。 ...
《Soss.jl:探索源代码重写在概率编程中的应用》 Soss.jl 是一个基于 Julia 语言的概率编程库,它利用元编程技术,为开发者提供了强大的工具,用于构建和执行贝叶斯推断。这个库的核心理念是通过源代码重写,将概率...
28. Metaprogramming ....................................................................... 779 29. A Matrix Design ......................................................................... 827 Part ...
《Reduce.jl:基于REDUCE的Julia语言符号解析器生成器详解》 Reduce.jl是Julia编程语言中一个非常独特的工具,它利用REDUCE代数术语重写器来生成解析器,处理数学表达式。REDUCE是一款历史悠久的计算机代数系统...
inline def safeDivide[A](x: A, y: A)(implicit ev: Numeric[A]): A = ev.div(x, y) ``` 这个`safeDivide`函数使用内联和类型类`Numeric`来确保除法操作的安全性。 最后,我们提到了Scala中的宏(Macro)。宏是...
Mocking.jl的工作原理是通过元编程(Metaprogramming)技术,动态地替换目标函数的定义,将其替换为你自定义的模拟函数(mock function)。在测试完成后,Mocking.jl会自动恢复原始函数的行为,确保不会影响到其他非...
Rubyinheritscharacteristicsfromvariouslanguages—Lisp,Small talk,C,andPerl,tonameafew.MetaprogrammingcomesfromLisp (andSmalltalk).It’sabitlikemagic,whichmakessomethingastonish ingpossible....
`@force_import`宏是元编程(Metaprogramming)的一种应用,元编程是指程序能够自动生成、修改或分析代码的能力。在Julia中,元编程是语言的核心特性之一,它允许开发者在运行时动态创建和操作类型和函数。这个宏的...