gpt4 book ai didi

xml - 根据属性值限制元素出现的次数

转载 作者:数据小太阳 更新时间:2023-10-29 02:08:13 25 4
gpt4 key购买 nike

我有一个如下所示的 XML:

<Artifacts count="2">
<Artifact>
...
</Artifact>
<Artifact>
...
</Artifact>
</Artifacts>

我正在寻找一种方法,通过使用XSD 架构。

即使我找到了通过使用 XSD 1.1 规范来实现此目的的可能方法,我想知道如果没有它是否完全可行,即基于 XSD 1.0 规范。


编辑:我将尝试为问题提供更多背景信息,以便更加准确。
XML 文件将作为 C++ 应用程序的输入提供。问题在于开发环境强制使用 Xerces v. 2.8.0 库进行解析。据我了解,此版本不支持 XSD 1.1 标准。

当然,我可以添加额外的代码,以便在 XSD 验证后检查 Artifact 元素出现的正确次数。我希望有一种方法可以避免额外的代码段并仅基于 XSD 完全验证输入文件。

最佳答案

您要建模的约束类型的正确术语是断言。不,断言在 XML Schema 1.0 中是不可能的。唯一支持断言的验证语言是 XML Schema 1.1 (xs:assert) 和 Schematron(sch:assertsch:report)。

除此之外,您可以编写 XPath 表达式或 XSLT 样式表来测试 Artifact 元素的正确数量:

/Artifacts/@count = count(/Artifacts/Artifact)

The XML file will be provided as input to a C++ application. The problem is that the development environment enforces the usage of the Xerces v. 2.8.0 library for parsing. To my understanding this version does not support the XSD 1.1 standard.

我的猜测是 Xerces 2.8.0(不再受支持的过时版本)不符合 XSD 1.1,但最简单的找出方法是简单地测试它。在架构中包含任何 xs:assert,看看会发生什么。

Of course, I can add extra code in order to check for the correct number of occurrences of "Artifact" elements after the XSD validation. I was hoping that there would be a way to avoid the extra code segment and completely validate the input file based on the XSD alone.

不,只有 XSD 1.0,您无法避免那些额外的代码行。


编辑 @kjhughes 在评论中写的内容实际上应该是答案的一部分:

This is correct, and I would also challenge OP's design that requires an attribute that merely mirrors the number of child attributes.

从 XML 设计的角度来看,如果此属性只是说明有多少 Artifact 元素,那么您为什么首先要包含它?您应该始终不愿意存储冗余和可恢复 信息。例如,不需要像这样存储子元素的位置:

<root>
<child n="1"/>
<child n="2"/>
</root>

您的 count 属性的作用非常相似。

关于xml - 根据属性值限制元素出现的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29389398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com