2009-06-30 125 views
14

我知道可以從XML生成框架XSD。例如,這個post有很好的答案。來自多個XML的XSD生成器

問題是如何基於幾個 XML生成XSD。這個想法是,每個XML可能有幾個不同的可選項,數組,選擇等等。 從所有這些例子中,我想編寫最準確的XSD。

我知道可能存在衝突等,但假設所有的XML都來自不知名的XSD,理論上應該是可能的。但是有這樣的工具嗎?

感謝

回答

14

Trang就是這樣由著名的詹姆斯·克拉克寫的工具。它可以在不同形式的xml定義之間進行轉換,如Relax NG正常語法和緊湊語法,老式DTD和XML架構。它也可以從一個或多個XML文件中推斷出模式。

如果您運行ubuntu trang已打包到Universe資源庫中,但該版本似乎有點故障,並且從上面的鏈接進行乾淨的下載可能是您的最佳選擇。假設trang.jar在當前目錄中:

java -jar trang.jar -I xml -O xsd file1.xml file2.xml definition.xsd 

應該做你想做的。

+3

Trang的主頁仍然鏈接到谷歌代碼,但項目已轉移到Github。對於任何未來發現它的人來說,https://github.com/relaxng/jing-trang是Trang回購的新位置。 – rmunn 2016-08-04 03:40:44

0

這是從一個XML創建模式的代碼: 演示如何使用此類的代碼示例(它假定存在「XmlSchemaSet set」類成員累積結果並通過調用來優化它們):

 var si = new XmlSchemaInference(); 
     var reader = XmlReader.Create(new StringReader(textBox1.Text)); 
     var en = si.InferSchema(reader, set).Schemas().GetEnumerator(); 
     en.MoveNext(); 
     var schema = en.Current as XmlSchema; 
     var stream = new MemoryStream(); 
     if (schema != null) 
     { 
      schema.Write(stream); 
      set.Add(schema); 
     } 
     stream.Flush(); 
     stream.Position = 0; 
     var streamReader = new StreamReader(stream); 
     var str = streamReader.ReadToEnd(); 
     grid1.Model.LoadSchema(str); 
     reader.Close(); 
     stream.Close(); 
     streamReader.Close(); 

如果再次運行它,並給XMLSchemaInference生成的模式和另一個XML,它會增強模式

4

.NET 4.5具有架構推理...

https://msdn.microsoft.com/en-us/library/xz2797k1(v=vs.110).aspx

這可以接受多個來源!

我需要這個,所以我寫了代碼,可能共享,傳遞多個文件路徑,第一個文件路徑是您要編寫的xsd文件,後續文件是輸入Xml文件。這是一個控制檯應用程序。

using System; 
using System.IO; 
using System.Xml; 
using System.Xml.Schema; 

namespace SchemaInferrer 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string xsdFile=""; 
      string[] xmlFiles=null; 
      DivideArguments(args, ref xsdFile, ref xmlFiles); 

      if (FilesExist(xmlFiles)) 
      { 
       Console.WriteLine("All files exist, good to infer..."); 
       XmlSchemaSet schemaSet = new XmlSchemaSet(); 
       XmlSchemaInference inference = new XmlSchemaInference(); 


       bool bFirstTime = true; 
       foreach (string sFile in xmlFiles) 
       { 
        XmlReader reader = XmlReader.Create(sFile); 
        if (bFirstTime) 
        { 
         schemaSet = inference.InferSchema(reader); 
        } else 
        { 
         schemaSet = inference.InferSchema(reader, schemaSet); 
        } 
        bFirstTime = false; 
       } 


       XmlWriterSettings xmlWriterSettings = new XmlWriterSettings() 
       { 
        Indent = true, 
        IndentChars = "\t" 
       }; 

       XmlWriter writer = XmlWriter.Create(xsdFile, xmlWriterSettings); 

       foreach (XmlSchema schema in schemaSet.Schemas()) 
       { 

        //schema.Write(Console.Out); 
        schema.Write(writer); 
       } 
       Console.WriteLine("Finished, wrote file to {0}...",xsdFile); 
       //Console.ReadLine(); 
      } 

     } 

     static void DivideArguments(string [] args, ref string xsdFile, ref string[] xmlFiles) 
     { 
      xsdFile = args[0]; 
      xmlFiles=new string[args.Length-1]; 

      for (int i = 0; i < args.Length-1; i++) 
      { 
       xmlFiles[i] = args[i + 1]; 
      } 
     } 

     static bool FilesExist(string[] args) 
     { 
      bool bFilesExist=true; //* until proven otherwise 

      if (args.Length>0) 
      { 
       foreach (string sFile in args) 
       { 
       if (!File.Exists(sFile)) 
        bFilesExist=false; 
       } 
      } 
      return bFilesExist; 
     } 
    } 
}