gpt4 book ai didi

xml - relaxng 可以指定一组无序的同名但不同属性的元素吗?

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

我的工作是自动测试接受和返回 XML 的 API,因此我想尽可能多地将 API 的记录返回数据转换为模式。基于易用性和易学性,我选择 RelaxNG 来完成这项任务。

在我输入所有信息之前,这是一个问题:

是否可以描述“无序的元素集,具有相同的名称但不同的属性”?

这是一个示例对象,用于说明我在描述时遇到的问题:

<item>
<id>d395136e-d060-4a6e-887c-c0337dd7ad09</id>
<name>The item has a name</name>
<link rel="self" type="type1" href="url" />
<link rel="download" type="type2" href="url" />
<link rel="relatedData" type="type3" href="url" />
</item>

链接对象是我被挂断的地方。这是问题所在:

  • 无法保证 item 中元素的顺序,因此我尝试将所有元素放在 <interleave> 中结构。
  • 会有多个<link>里面的元素<item> ,具有不同的属性集(即,<item> 必须具有“ self ”链接、“下载”链接和“相关数据”链接才有效)。
  • 需要每种链接类型之一,但同样不能保证顺序。

我试着这样描述架构:

<element name="item">
<interleave>
<element name="id"><text/></element>
<element name="name"><text/></element>
<ref name="selfLink"/>
<ref name="launchLink"/>
<ref name="thumbnailLink"/>
</interleave>
</element>

“链接”引用在别处定义如下:

 <define name="selfLink">
<element name="link">
<attribute name="href"><text/></attribute>
<attribute name="rel"><value>self</value></attribute>
<attribute name="type"><value>type1</value></attribute>
</element>
</define>

解析器对此不满意 - 从 jing 我得到 error: the element "link" can occur in more than one operand of "interleave" .我明白它的意思,但我希望它能够将“具有相同名称但属性不同的元素”作为唯一项来处理。

将链接引用从 interleave 中移出可以对其进行解析,但只要返回数据中的顺序发生变化,我将等待验证器崩溃。

任何想法,或者这是不可能的?我正在处理的 XML 是否存在固有问题,需要我将其中一些问题移至测试应用程序中的更高处理逻辑(在运行更通用的 XML 验证后手动检查每个链接类型?)

最佳答案

看起来您偶然发现了 restriction on interleave在 RELAX NG 中。我会尝试在 Schematron 中做到这一点,或者 RELAX NG 和 Schematron 的组合。

这是检查您的 <link> 的片段使用版本为 supported by Jing 的 Schematron 的元素:

<schema xmlns="http://www.ascc.net/xml/schematron">
<pattern name="link pattern">
<rule context="item">
<assert test='count(link) = 3'>There must be 3 link elements.</assert>
<assert test="count(link[@rel = 'self' and @type ='type1']) = 1">There must be 1 link element wwhere @rel='self' and @type='type1'.</assert>
<assert test="count(link[@rel = 'download' and @type ='type2']) = 1">There must be 1 link element where @rel='download' and @type='type2'.</assert>
<assert test="count(link[@rel = 'relatedData' and @type = 'type3']) = 1">There must be 1 link element where @rel='relatedData' and @type='type3'.</assert>
</rule>
</pattern>
</schema>

关于xml - relaxng 可以指定一组无序的同名但不同属性的元素吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11786196/

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