如何定义允许未知(通配符)元素的 XSD 文件? [英] How can I define an XSD file that allows unknown (wildcard) elements?
问题描述
我收到一条带有未知变量名称元素的 XML 消息……也就是说,它们不是预定义的……
I'm receiving an XML message with unknown variable name elements... that is, they are not predefined...
我只知道可以有 0 个或更多这些元素,以及其他一些强制性元素...
I only know there can be 0 or more of those elements, along with some other that are mandatory...
例如
<root>
<service>my service</service>
<resource>my resource</resource>
<action>update</action>
<parameters>
<field1>value1</field1>
<field2>value2</field2>
<field3>value3</field3>
</parameters>
</root>
也就是说,我不知道什么会作为参数"传递,我只知道会有0个或更多元素带值,不允许更深的标签嵌套....
that is, I don't know what will be passed as "parameters", I only know there will be 0 or more elements with a value, no deeper tag nesting allowed....
我在考虑类似的事情
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="service" type="xs:string" minOccurs="1" maxOccurs="1" nillable="false"/>
<xs:element name="resource" type="xs:string" minOccurs="1" maxOccurs="1" nillable="false"/>
<xs:element name="action" type="xs:string" minOccurs="1" maxOccurs="1" nillable="false"/>
<xs:element name="parameters">
<xs:complexType>
<xs:element name="*" maxOccurs="unbounded">
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
当然,最难的部分是
<xs:element name="*" maxOccurs="unbounded">
有没有可能做这样的事情?
如何定义验证此类消息的 XSD 文件?
Is it possible to do such a thing?
How can I define an XSD file that validates such a message?
--
我检查了 w3c 参考
I checked the w3c reference at
http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/datatypes.html#NCName
它说:
NCName 的·词法空间·是所有与[XML 中的命名空间] 的 NCName 产生式匹配的字符串的集合.
The ·lexical space· of NCName is the set of all strings which ·match· the NCName production of [Namespaces in XML].
那是什么意思?
此外...你能推荐我一些简单的方法来测试是否符合 XSD 定义吗?
So what does it mean?
besides... could you recommend me some easy way to test compliance with an XSD definition?
推荐答案
你要的是通配符粒子,详情见http://www.w3.org/TR/xmlschema-1/#Wildcards一个>
What you want is a wildcard particle, for details see http://www.w3.org/TR/xmlschema-1/#Wildcards
要做到这一点,您可以使用 xs:any.请注意,xs:element 和 xs:any 不能直接放在 xs:complexType 中.您需要一个容器,如序列或选择.
To do it you can use xs:any. Note that xs:element and xs:any cannot be placed directly inside an xs:complexType. You need a container like a sequence or choice.
处理通配符的有效架构如下:
A valid schema that handles wildcards is below:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="service" type="xs:string"/>
<xs:element name="resource" type="xs:string"/>
<xs:element name="action" type="xs:string"/>
<xs:element name="parameters">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:any processContents="lax"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
这篇关于如何定义允许未知(通配符)元素的 XSD 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!