使用 XSD 进行 XML 验证
我有一个具有以下格式的 XML。
<RootNode>
<Parent>
<Child1>Some text about child 1</Child1>
<Child2>Some text about child 2</Child2>
....
....
<Child5>Some text about child 5</Child5>
....
....
<Childn>Some text about child n</Childn>
</Parent>
</RootNode>
现在我想根据 xsd 验证此 xml。
但问题是孩子的数量和节点名称不固定。
请检查我的文件的以下示例
示例 1:节点名称为 Child1 和 Child2 的两个子节点
<RootNode>
<Parent>
<Child1>Some text about child 1</Child1>
<Child2>Some text about child 2</Child2>
</Parent>
</RootNode>
示例 2:节点名称为 Child4 Child5 和 Child8 的三个子节点
<RootNode>
<Parent>
<Child4>Some text about child 4</Child4>
<Child5>Some text about child 5</Child4>
<Child8>Some text about child 8</Child8>
</Parent>
</RootNode>
我想要验证 Child 节点内的文本(显示为“某些文本”)关于子节点 n"),具有以下规则
规则:如果任何子节点 -(父节点的子节点)的内部文本长度超过 256,则 xml 无效。
这意味着如果“父”节点的任何子节点具有内部文本,则认为 xml 无效长于 256 个字符
使用 XSD 模式验证可以吗?您能帮我创建 .XSD 文件来验证这一点吗?
提前致谢
I have an XML with following format.
<RootNode>
<Parent>
<Child1>Some text about child 1</Child1>
<Child2>Some text about child 2</Child2>
....
....
<Child5>Some text about child 5</Child5>
....
....
<Childn>Some text about child n</Childn>
</Parent>
</RootNode>
Now I want to validate this xml against xsd.
But the problem is the number and node name of children are not fixed.
Please check following samples of my file
Sample 1: Two children with node name Child1 and Child2
<RootNode>
<Parent>
<Child1>Some text about child 1</Child1>
<Child2>Some text about child 2</Child2>
</Parent>
</RootNode>
Sample 2: Three children with node name Child4 Child5, and Child8
<RootNode>
<Parent>
<Child4>Some text about child 4</Child4>
<Child5>Some text about child 5</Child4>
<Child8>Some text about child 8</Child8>
</Parent>
</RootNode>
I want a to validate the text inside the Child node (which is shown as "Some text about child n") with following rule
Rule: The xml is invalid if any Child node - (Children of Parent) has iner text length more then 256.
It means consider the xml invalid if any child of the "Parent" node has inner text longer then 256 characters
Is this possible using XSD schema validation? Could you please help me creating .XSD file to validate this?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无法自动验证这一点,因为子节点的最大数量未知。您也许可以通过编写一些手动验证来获得。
如果您能够更改此 XML 的格式,您应该这样做,因为它设计得很糟糕(尤其是因为它不可验证......)。您应该追求类似的东西:
或者,如果父级可以包含其他东西以及子级,那么为子级提供一个容器是有用的:
It's not possible to validate this automatically because the maximum number of child nodes is unknown. You may be able to get by writing some manual validation.
If you are able to change the format of this XML, you should, because it's badly designed (not least of all because it's not validatable...). You should shoot for something more like:
or, if the parent can contain other things as well as the children, then having a container for the children is useful:
正如前面所说,您无法使用 XSD 解决这个问题,因为您事先不知道所有元素名称是什么。如果您不完全依赖 XSD 验证,您可能希望看看 Schematron ,它可以完全执行此操作的验证。
You can't solve that with XSD as said before simply because you don't know what all the element names will be in advance. If you are not totally tied to XSD validation you may wish to take a look at Schematron which can do exactly this sort of validation.