创建 XSD 架构
我有一个 xml 标签:
<ROW field1="value 1" field2="value 2" ... />
fieldi 有一个字符串值,fieldi 的属性数量是可变的,但不少于 1。是否可以为此标签创建 xsd 架构?
我从 Web 服务器收到的此 xml 文档中可能的 xml 文档
<ROWDATA>
<ROW field1="dfgdf" field2="ddfg"></ROW>
<ROW field1="dfedf" field2="djkfg" field3="cdffd"></ROW>
<ROW field1="dfedf" field2="djkfg" field3="cdffd" field4="dfedf" field5="djkfg" field6="cdffd"></ROW>
</ROWDATA>
可以是可变数量的属性字段(我将它们记为 fieldi,其中 i 表示特定属性字段的顺序) 所以我有未知数量的 ROW 元素和未知数量的 ROW 元素中的字段属性
谢谢
I have an xml tag:
<ROW field1="value 1" field2="value 2" ... />
fieldi has a string value, and number of attributes fieldi is variable, but not less than 1. Is it possible to create an xsd schema for this tag?
possible xml document
<ROWDATA>
<ROW field1="dfgdf" field2="ddfg"></ROW>
<ROW field1="dfedf" field2="djkfg" field3="cdffd"></ROW>
<ROW field1="dfedf" field2="djkfg" field3="cdffd" field4="dfedf" field5="djkfg" field6="cdffd"></ROW>
</ROWDATA>
in this xml document, which I receive from a web server, can be a variable number of attributes field (I noted them as fieldi, where i means the order of a specific attribute field)
So I have, unknown number of ROW elements and unknown number of field attributes in the ROW element
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用的是 Visual Studio 2008:
这将生成您的 xsd 架构
编辑
尝试此示例了解有关设置 minOccurs (在元素上)或 required (在属性上)的详细信息这样你就可以操纵你的派生模式。
If you're using Visual Studio 2008:
This will generate your xsd schema(s)
EDIT
Try this example for details on setting minOccurs (on elements) or required (on attributes) so you can manipulate your derived schema.
如果您不习惯自己编写 XSD,请使用一些生成器,例如 这个。
编辑:根据您在评论中的 XML,我可以想到 XSD 的以下结构
If you are not comfortable in writing XSD yourself, use some generator like this.
EDIT: Based on your XML in comments, I can think of below structure of XSD
我想我已经理解你的要求了。为了避免误解,让我透露一下我曾经理解过的内容:
“您有一个 xml 文件,其中包含一个名为
fieldi
的元素,带有一组未知属性。这意味着您不知道 [或者说不想要] 的名称和值只是想看看,至少有 1 个属性出现”,好吧。遗憾的是,这个要求已经超出了 XML 模式的能力。 :-[
模式中不能有未声明的属性。如果它出现在 xml 中,则需要有一个正确的定义。有一种叫做
[点击此处] 再次需要定义[某个地方,在另一个链接模式中]。1) 定义所有可能的属性,使得
use="optional"
看起来实际上是不可能的。并且您的最后一个要求也被跳过。2)如果可能的话,将所有属性转换为元素[使用转换,或者您可以要求发送者这样做,我不知道您的情况有多复杂],并定义元素
< ;any/>
,听起来有点舒服。但你的要求[至少必须出现一个属性]仍然没有达到。这就是我可以为你添加的内容。如果您可以更改要求或输入 xml 结构,请告诉我,我会看看是否可以以任何其他方式帮助您..
问候,
婴儿专业
I think I have understood your requirement. Just to avoid misconceptions, let me reveal what I have understood once:
"You have an xml file which contains an element with the name
fieldi
, comes with set of some unknown attributes. Which means you don't know [or say don't want] the names and values of those attributes. Just want to see, there is at-least 1 attribute appearing",Well. sorry to say that, this requirement is running out of capability of XML-schema. :-[
You cannot have attributes undeclared in schema. If it appears in xml, it requires to have a proper definition for that. There is something called
<anyAttribute/>
[click-here] which again requires definition [somewhere, in another linked schema].1) Defining all the possible attributes making
use="optional"
, doesn't look practically possible. And also your last requirement go skipped.2) If it is possible then, convert all the attributes to elements [using transformation, or you can either ask the sender to do so, I don't know how complicate it is in your case], And define element
<any/>
, which sounds somewhat comfortable. but your requirement [at-least one attribute must appear] is still not achieved.so this is it I can add-up for you. If you can change the requirement or input xml structure then let me know, I will see, whether I can help you in any-other ways ..
regards,
infant-pro
我解决了这个问题,但是以其他方式,通过以我需要的方式控制 xml 文档的反序列化。但是,我不喜欢这个解决方案,因为我想从 xsd 方案创建类并在我的代码中使用它们。
不管怎样,谢谢大家
I solved the problem but in other way, by controlling the deserialization of the xml document in the way I need. However, I don't like this solution, because I had wanted to create classes from the xsd scheme and use them in my code.
Anyway, thanks to everybody