如何从XSD获取所有xpath? [英] How to get all xpaths from a XSD?

查看:145
本文介绍了如何从XSD获取所有xpath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XSD,要求是将XSD中存在的所有元素的xpath列入UI,这样用户就可以使用它来执行一些与DOM相关的操作。

I have an XSD and the requirement is to list the xpath of all the elements present in XSD into UI, so users can use it to perform some DOM related operations.

我可以以编程方式从XSD中提取所有元素的xpath吗?

Can I programatically extract xpaths of all the elements from XSD?

推荐答案

可以这样做,但需要注意所有允许路径的集合是无限的(例如因为递归或因为通配符)所以你需要这个无限集的智能表示,否则你的代码将需要放弃并返回类似任何东西的东西,如果你发现列表无法枚举。当检查路径表达式(例如.//para对模式)时,模式感知的Saxon产品做了类似的事情:如果它知道上下文项的类型,它可以确定.//para是否能够选择任何东西,如果没有,则给出警告。

It can be done, though you need to be aware that the set of all permitted paths is infinite (for example because of recursion or because of wildcards) so you will need a smart representation of this infinite set, or your code will need to give up and return something like "anything goes" if you find that the list can't be enumerated. The schema-aware Saxon product does something rather like this when checking a path expression such as .//para against the schema: if it knows the type of the context item, it can determine whether .//para is capable of selecting anything, and giving you a warning if not.

作为第一步,您需要从源模式文档构建模式组件模型的(相关部分)。不要试图自己这样做,这是太多的工作。许多产品都有一个API,允许您访问架构组件模型。 Saxon允许您使用Validate命令行上的-scmout标志从源模式文档生成模式组件模型作为XML表示。

As the first step, you need to build the (relevant part of) the schema component model from the source schema documents. Don't try to do this yourself, it is far too much work. A number of products have an API that allows you to access the schema component model. Saxon allows you to generate the schema component model from source schema documents as an XML representation, using the -scmout flag on the Validate command line.

获得模式组件后模型,您可以通过转到其复杂类型(如果它是一个简单类型,然后答案是微不足道的)找到元素的允许子元素,并递归遍历粒子树,只查找元素粒子和通配符粒子(您可能决定如果有通配符粒子,最好放弃)。您可能不仅要考虑元素的声明类型,还要考虑通过扩展从该元素派生的其他类型。您需要知道允许的子元素的元素声明,而不仅仅是允许的子元素名称,因为当然在查找允许的孙子元素时,您需要从元素声明开始,因为可能存在元素的局部声明同一个名字。

Once you have the schema component model, you can find the permitted children of an element by going to its complex type (if it's a simple type then the answer is trivial) and traversing the tree of particles recursively, looking only for the element particles and wildcard particles (you might decide that if there are wildcard particles, it's best to give up). You might want to consider not only the declared type of the element, but other types derived from that one by extension. You need to know the element declarations of the permitted children, not just the permitted child element names, because of course when it comes to finding the permitted grandchildren, you need to start from the element declaration, as there may be local declarations of elements with the same name.

当然,当你知道元素名称和它们允许的子元素之间的关系时,路径集就是这种关系的传递闭包。

And of course when you know the relation between element names and their permitted child elements, the set of paths is the transitive closure of this relation.

这篇关于如何从XSD获取所有xpath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆