XML 模式:用相应的模式替换导入
我有一个 XML 架构,其中包含多个导入,而这些导入又包含多个导入。我需要生成语义上相等的模式,其中所有导入都是内联的。我想
<xs:import namespace="http://some.name/" schemaLocation="./path/to/it.xsd"/>
用引用的模式的内容替换这些:。我需要将输出作为字符串,而不是作为某种内部表示形式。
我尝试了 Apache Xerces 但没有找到将 XSModel 写入字符串的方法。有没有?
我尝试了 Apache XmlSchema 2 但当它写入 XML 架构时,它不会用架构替换导入声明。
有什么图书馆可以做到吗?有什么建议吗?
谢谢。
I have a XML Schema that contains multiple imports which in turn contain imports. I need to generate semantically equal schema where all imports are inlined. I want to replace these:
<xs:import namespace="http://some.name/" schemaLocation="./path/to/it.xsd"/>
with the contents of referenced schemata. And I need to get output as a string, not as some internal representation.
I tried Apache Xerces but did not find a way to write XSModel to a string. Is there?
I tried Apache XmlSchema 2 but when it writes XML Schema it does not replace import declarations with schema.
Is there any library that can do it? Any suggestions?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
xsd:include 通常可以内联,但 xsd:import 不能。这是因为 xsd:import 用于引用不同目标命名空间的架构文档,并且同一架构文档中不能包含具有不同目标命名空间的组件。
xsd:include can usually be inlined, but xsd:import can't. That's because xsd:import is used to reference a schema document for a different target namespace, and you can't have components with different target namespaces in the same schema document.
正如此处和<所指出的一个href="https://stackoverflow.com/questions/7066951/need-a-tool-to-merge-an-xsd-schema-with-imports-and-includes-into-a-single-file/9055574#9055574 ">此处,XSD 对每个文件只有一个目标命名空间的限制使得您的“语义等效”请求无法得到解决。在命名空间本身用于定义(或细化)语义集边界的所有场景中,这是事实,也是典型的。
对于一次性或设计时重构,您不必以循环方式或动态地以编程方式处理此类事情,您也可以尝试看一下 此处;也许您的情况的问题不是不支持导入(我觉得很奇怪),而是包含/导入的复杂性使图表对于您的工具来说过于复杂。如后一篇文章所示,通过折叠包含,减少所需导入数量的净效果,问题得到了解决。
或者,如果您的“语义等价”不涉及命名空间(例如,我见过对从 XSD 开发关系模型相当感兴趣的人),则可以通过重构将所有命名空间合并为一个(或无,即没有目标命名空间),然后将其提供给您的工具。从自动重构的角度来看,这里唯一的问题是确保不同命名空间之间不存在重复的命名 XSD 组件;例如,不同命名空间中的元素、类型、属性或组等不能具有相同的名称。
As pointed out here and here, XSD's limitation of having one target namespaces per file makes your "semantically equivalent" request impossible to solve. This is true, and also typical, in all scenarios where the namespace itself is used to define boundaries of (or refine) semantic sets.
For one time or design time refactoring, where you don't have to programatically deal with such a thing in a recurrent way or dynamically, you may also try to take a look here; Maybe the issue in your case is not that the imports are not supported (which I would find strange) but rather that the complexity of the include/imports makes the graph too complicated for your tooling. As shown in the latter post, by collapsing the includes, with a net effect of reducing the number of required imports, the problem was solved.
Alternatively, if somehow your "semantic equivalence" doesn't involve namespaces (for e.g. I've seen people that were rather interested in developing a relational model from XSD), it may be possible, through refactoring, to bring all namespaces into one (or none i.e. no target namespace) and then feed it to your tool. The only catch here, from an automatic refactoring perspective, is to ensure that there are no duplicate named XSD components across different namespaces; e.g. can't have same name for an element, or type, or attribute, or group, etc. in different namespaces.