使用 XSD 进行 XML 验证

发布于 2024-10-04 22:37:31 字数 1310 浏览 0 评论 0原文

我有一个具有以下格式的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风渺 2024-10-11 22:37:31

无法自动验证这一点,因为子节点的最大数量未知。您也许可以通过编写一些手动验证来获得。

如果您能够更改此 XML 的格式,您应该这样做,因为它设计得很糟糕(尤其是因为它不可验证......)。您应该追求类似的东西:

<RootNode>
    <Parent>
        <Child num="1">...</Child>
        <Child num="2">...</Child>
    </Parent>
</RootNode>

或者,如果父级可以包含其他东西以及子级,那么为子级提供一个容器是有用的:

<RootNode>
    <Parent>
        <OtherStuff/>
        <Children>
            <Child num="1">...</Child>
            <Child num="2">...</Child>
        </Children>
        <MoreStuff/>
    </Parent>
</RootNode>

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:

<RootNode>
    <Parent>
        <Child num="1">...</Child>
        <Child num="2">...</Child>
    </Parent>
</RootNode>

or, if the parent can contain other things as well as the children, then having a container for the children is useful:

<RootNode>
    <Parent>
        <OtherStuff/>
        <Children>
            <Child num="1">...</Child>
            <Child num="2">...</Child>
        </Children>
        <MoreStuff/>
    </Parent>
</RootNode>
柠北森屋 2024-10-11 22:37:31

正如前面所说,您无法使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文