来自多个XML的XSD生成器

Rag*_*ler 16 xml xsd

我知道可以从XML生成骨架XSD.例如,这篇文章有很好的答案.

问题是如何基于多个 XML 生成XSD .这个想法是每个XML可能有几个不同的可选,数组,选择等.从所有这些例子中,我想编写最准确的XSD.

我知道可能存在冲突等,但假设所有的XML都来自一个未知的XSD,那么它应该是理论上可行的.但是有这样的工具吗?

谢谢

Knu*_*gen 16

Trang就是由着名的詹姆斯·克拉克写的这样一个工具.它可以在不同形式的xml定义之间进行转换,例如Relax NG normal和compact syntax,old school DTD和XML schema.它还可以从一个或多个xml文件中推断出架构.

如果运行ubuntu trang打包在Universe存储库中,但该版本似乎有点坏了,从上面的链接中干净下载可能是您的最佳选择.假设trang.jar在当前目录中:

java -jar trang.jar -I xml -O xsd file1.xml file2.xml definition.xsd
Run Code Online (Sandbox Code Playgroud)

应该做你想做的事.

  • Trang主页仍链接到Google Code,但该项目已移至Github.对于将来发现它的任何人,https://github.com/relaxng/jing-trang是Trang repo的新位置. (3认同)

S M*_*den 5

.Net 4.5具有架构推断...

https://msdn.microsoft.com/zh-CN/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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)