创建使用< all>打开的有效XSD和< any>分子 [英] Creating a valid XSD that is open using <all> and <any> elements
问题描述
我需要指定一个XSD来验证XML文档。 XSD将用于JAXB生成的Java绑定。
我的问题是指定可选元素,我不知道其名称以及我一般对解析不感兴趣。
I need to specify a XSD for validating XML documents. The XSD will be used for a JAXB generation of Java bindings. My problem is specifying optional elements which I do not know the names of and which I in general am not interested in parsing.
XML文档的结构如下:
The structure of the XML documents is like:
<TRADE>
<TIME>12:12</TIME>
<MJELLO>12345</MJELLO>
<OPTIONAL>12:12</OPTIONAL>
<DATE>25-10-2011</DATE>
<HELLO>hello should be ignored</HELLO>
</TRADE>
重要的是:
- 我不能假设任何订单,下一个XML文档实例migtht有不同顺序的标签
- 我只对解析某些标签感兴趣,一些是强制性的,一些是可选的
- XML文档可以使用我不感兴趣解析的新元素进行扩展
我的XSD结构就像( 不是有效的xsd ):
The structure of my XSD is like (not a valid xsd):
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- *********************************************** -->
<!-- Trade element definitions for the XML Documents -->
<!-- *********************************************** -->
<xs:complexType name="Trade">
<!-- Using the all construction ensures that the order does not matter -->
<xs:all>
<xs:element name="DATE" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="TIME" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="OPTIONAL" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:any minOccurs="0"/>
</xs:all>
</xs:complexType>
<!-- TRADE is the mandatory top-level tag -->
<xs:element name="TRADE" type="Trade"/>
</xs:schema>
因此,在此示例中:DATE和TIME是必需的(它们必须在XML中一次) ,OPTIONAL可能会出现一次然后我想指定,所有其他标签都是允许的。订单无关紧要。
So, in this example: DATE and TIME are mandatory (they must be in the XML exactly once), OPTIONAL might be present once and then I would like to specify, that all other tags are allowed. The order does not matter.
如何为此指定有效的XSD?
How do I specify a valid XSD for this?
推荐答案
这是一个经典的解析器问题。
This is a classic parser problem.
基本上,你的BNF是:
Basically, your BNF is:
Trade = whatever whatever*
whatever = "DATE" | "TIME" | anything
anything = a-z a-z*
但这是不明智的。字符串DATE都可以在DATE和任何内容的任何规则下接受。
But this is ambigous. The string "DATE" can both be accepted under the whatever rule as "DATE" and as anything.
所以如果你有
<TRADE>
<TIME>12:12</TIME>
<DATE>25-10-2011</DATE>
<DATE>25-12-2011</DATE>
</TRADE>
目前还不清楚是否应该接受。
it is unclear whether that should be accepted or not.
可以解释其中一个
"TIME", "DATE", anything
anything, anything, "DATE"
anything, anything, anything
"TIME", "DATE", anything
"TIME", "DATE", "DATE"
etc.
这一切归结为:如果你有一个通配符和随机序列,你就无法有意义地决定哪个令牌匹配哪个规则。
It all boils down to: If you have a wildcard combined with random sequence, you cannot meaningfully decide which token matches which rule.
将可选元素与wilcard一起使用尤其没用。
It especially does not make sense to have optional elements together with a wilcard.
你有两个选项:
- 使用xs:sequence而不是xs:all
- 不要使用通配符
据我了解,这两个选项都与您的意愿相冲突。
As I understand it, both options are in conflict with your wishes.
也许你可以构建一个匹配所有的通配符,除了 DATE,TIME等。
Perhaps you can construct a wildcard that matches everything except DATE, TIME etc.
这篇关于创建使用< all>打开的有效XSD和< any>分子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!