SQL 如何从 XSD 文件创建数据库表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/138575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 23:38:26  来源:igfitidea点击:

How can I create database tables from XSD files?

.netsqldatabasexsdcode-generation

提问by Oded

I have a set of XSDs from which I generate data access classes, stored procedures and more.

我有一组 XSD,从中可以生成数据访问类、存储过程等。

What I don't have is a way to generate database table from these - is there a tool that will generate the DDL statements for me?

我没有的是一种从这些生成数据库表的方法 - 有没有一种工具可以为我生成 DDL 语句?

This is not the same as Create DB table from dataset table, as I do not have dataset tables, but XSDs.

这与Create DB table from dataset table 不同,因为我没有数据集表,而是 XSD。

采纳答案by S.Lott

Commercial Product: Altova's XML Spy.

商业产品:Altova 的XML Spy

Note that there's no general solution to this. An XSD can easily describe something that does not map to a relational database.

请注意,对此没有通用的解决方案。XSD 可以轻松描述未映射到关系数据库的内容。

While you can try to "automate" this, your XSD's must be designed with a relational database in mind, or it won't work out well.

虽然您可以尝试“自动化”此操作,但您的 XSD 必须在设计时考虑到关系数据库,否则效果不佳。

If the XSD's have features that don't map well you'll have to (1) design a mapping of some kind and then (2) write your own application to translate the XSD's into DDL.

如果 XSD 的功能不能很好地映射,您将必须 (1) 设计某种映射,然后 (2) 编写您自己的应用程序以将 XSD 转换为 DDL。

Been there, done that. Work for hire -- no open source available.

去过也做过。为雇佣工作——没有可用的开源。

回答by Spoike

There is a command-line tool called XSD2DB, that generates database from xsd-files, available at sourceforge.

有一个名为XSD2DB的命令行工具,它从 xsd 文件生成数据库,可在 sourceforge 获得。

回答by Robert Gould

I use XSLT to do that. Write up your XSD then pass your data models through a hand written XSLT that outputs SQL commands. Writing an XSLT is way faster and reusable than a custom program /script you may write.

我使用 XSLT 来做到这一点。编写您的 XSD,然后通过输出 SQL 命令的手写 XSLT 传递您的数据模型。与您可能编写的自定义程序/脚本相比,编写 XSLT 更快且可重用。

At least thats how I do it at work, and thanks to that I got time to hang out on SO :)

至少我在工作中就是这样做的,多亏了这一点,我才有时间出去玩 :)

回答by user3043225

The best way to create the SQL database schema using an XSD file is a program called Altova XMLSpy, this is very simple:

使用 XSD 文件创建 SQL 数据库模式的最佳方法是一个名为 Altova XMLSpy 的程序,这非常简单:

  1. Create a new project
  2. On the DTDs / Schemas folder right clicking and selecting add files
  3. Selects the XSD file
  4. Open the XSD file added by double-clicking
  5. Go to the toolbar and look Conversion
  6. They select Create structure database from XML schema
  7. Selects the data source
  8. And finally give it to export the route calls immediately leave their scrip schemas with SQL Server to execute the query.
  1. 创建一个新项目
  2. 在 DTDs / Schemas 文件夹上右键单击并选择添加文件
  3. 选择 XSD 文件
  4. 双击打开添加的XSD文件
  5. 转到工具栏并查看转换
  6. 他们选择从 XML 模式创建结构数据库
  7. 选择数据源
  8. 最后让它导出路由调用,立即将它们的脚本架构留给 SQL Server 来执行查询。

Hope it helps.

希望能帮助到你。

回答by BeWarned

XML Schemas describe hierarchial data models and may not map well to a relational data model. Mapping XSD's to database tables is very similar mapping objects to database tables, in fact you could use a framework like Castor that does both, it allows you to take a XML schema and generate classes, database tables, and data access code. I suppose there are now many tools that do the same thing, but there will be a learning curve and the default mappings will most like not be what you want, so you have to spend time customizing whatever tool you use.

XML 模式描述分层数据模型,可能无法很好地映射到关系数据模型。将 XSD 映射到数据库表与将对象映射到数据库表非常相似,实际上您可以使用像 Castor 这样的框架来实现这两种功能,它允许您采用 XML 模式并生成类、数据库表和数据访问代码。我想现在有很多工具可以做同样的事情,但是会有一个学习曲线,而且默认映射很可能不是你想要的,所以你必须花时间定制你使用的任何工具。

XSLT might be the fastest way to generate exactly the code that you want. If it is a small schema hardcoding it might be faster than evaluating and learing a bunch of new technologies.

XSLT 可能是准确生成所需代码的最快方式。如果它是一个小的模式硬编码,它可能比评估和学习一堆新技术更快。

回答by Shawn

Might take a look at the XSD tool in visual studio 2k8... I have created a relational dataset from an xsd and it might help your out somehow.

可以看看 Visual Studio 2k8 中的 XSD 工具......我已经从 xsd 创建了一个关系数据集,它可能会以某种方式帮助你。

回答by Shawn

hyperjaxb (versions 2 and 3) actually generates hibernate mapping files and related entity objects and also does a round trip test for a given XSD and sample XML file. You can capture the log output and see the DDL statements for yourself. I had to tweak them a little bit, but it gives you a basic blue print to start with.

hyperjaxb(版本 2 和 3)实际上会生成休眠映射文件和相关实体对象,并且还会对给定的 XSD 和示例 XML 文件进行往返测试。您可以捕获日志输出并自己查看 DDL 语句。我不得不稍微调整一下,但它为您提供了一个基本的蓝图。

回答by JeeBee

Create a Java Model using Axis wsdl2java (which can take in .xsd files).

使用 Axis wsdl2java(可以接收 .xsd 文件)创建 Java 模型。

Use a database generation tool for Java that takes in a Java Model. Surely something like Hibernate can do this? I wrote my own tool (takes a couple of days, also generates CRUD code in Java too) to save myself time at work, maybe this would be a nice personal project?

使用接受 Java 模型的 Java 数据库生成工具。像 Hibernate 这样的东西肯定可以做到这一点吗?我编写了自己的工具(需要几天时间,也用 Java 生成 CRUD 代码)来节省自己的工作时间,也许这会是一个不错的个人项目?

Or just do it manually so that you can check everything is correct and good! Database tools are good enough now that you can zip through creating tables for a model without too many problems.

或者只是手动进行,以便您可以检查一切是否正确且良好!数据库工具现在已经足够好了,您可以轻松地为模型创建表而不会出现太多问题。