<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Metah.XXML 元編程語言

          聯(lián)合創(chuàng)作 · 2023-09-29 22:48

          Metah.X(簡稱MX)用自創(chuàng)的語法實現(xiàn)了XML Schema 1.0的語義,并且用C#實現(xiàn)了一個Schema-lized Document Object Model (SDOM),編譯器編譯MX代碼后將生成使用SDOM的C#代碼,這將XML Schema的語義映射到C#上,從而完全釋放出XML Schema的力量。盡管現(xiàn)在只有C#版,實現(xiàn)Java版或其它語言版本是完全可能的。

          MX是個開源項目,歡迎參與,比如實現(xiàn)Java版或其它語言版本;MX沒有定型,歡迎提出修改意見。

          XML Schema定義了XML數(shù)據(jù)(或叫XML實例)的形狀及需要遵守的規(guī)則,比如下面的XSD代碼:

          <E1 xmlns="http://ns1">
            <E2 A1="123">abc</E2>
            <E3 xmlns="">
              <p:E4 xmlns:p="http://ns2" p:A1="true" A1="123" />
              def
            </E3>
          </E1>

          下面是合法的XML數(shù)據(jù):

          12345678-33487654321

          因為XSD相當(dāng)繁瑣不便于書寫,MX自創(chuàng)了用戶友好的語法來表達(dá)XML Schema的語義,下面的MX代碼和上面的XSD代碼表達(dá)了相同的語義:

          //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);
                  ...
              }
          }

          使用編譯器生成的代碼,就可以創(chuàng)建、查詢、修改、保存、裝載及驗證XML數(shù)據(jù),下面的手寫代碼演示了如何使用編譯器生成的代碼:

          //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 = "[email protected]";
                      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的內(nèi)容:

          12345678-33487654321

          下面是d:\customer2.xml的內(nèi)容:

          12345678-3348765432111223344

          也就是說,MX的用處 = XML Schema的用處 + Document Object Model的用處。

          瀏覽 21
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  成人三级片网站视频 | 亚洲人视频网站 | 无码人妻精品一区 | 那个视频可以看A片 | 久久久久久AV少妇 |