放松:在Interleave>

发布于 2025-01-20 09:31:52 字数 2049 浏览 3 评论 0 原文

要求是这样的:

  • 一个提要将有多个链接
  • “B”类型链接必须至少出现一次
  • “A”类型链接可以出现零次或多次
  • “A”类型链接也可以出现在其他地方,因此需要保留定义独立

我的代码出现以下错误
lxml.etree.RelaxNGParseError:交错中的元素或文本冲突,第 6 行

下面的示例代码

from io import StringIO
from lxml import etree

XML = StringIO('''
<root xmlns="http://www.w3.org/2005/Atom">
    <link rel="a1" />
    <link rel="b1" />
    <link rel="a2" />
    <link rel="b2" />
</root>
''')

RNG = StringIO('''
<grammar xmlns="http://relaxng.org/ns/structure/1.0">

    <start>
        <element name="root" ns="http://www.w3.org/2005/Atom">
            <interleave>
                <zeroOrMore>
                    <ref name="aLink"/>
                </zeroOrMore>
                <oneOrMore>
                    <ref name="bLink"/>
                </oneOrMore>
            </interleave>
        </element>
    </start>

    <define name="aLink" ns="http://www.w3.org/2005/Atom">
        <element name="link">
            <choice>
                <attribute name="rel">
                    <value>a1</value>
                </attribute>
                <attribute name="rel">
                    <value>a2</value>
                </attribute>
            </choice>
        </element>
    </define>

    <define name="bLink" ns="http://www.w3.org/2005/Atom">
        <element name="link">
            <choice>
                <attribute name="rel">
                    <value>b1</value>
                </attribute>
                <attribute name="rel">
                    <value>b2</value>
                </attribute>
            </choice>
        </element>
    </define>

</grammar>
''')

rng_tree = etree.parse(RNG)
rng_schema = etree.RelaxNG(rng_tree)

rng_schema.validate(XML)

The requirement is as such:

  • A feed will have multiple links
  • A "B" type link MUST appear atleast once
  • An "A" type link MAY appear zero or more times
  • "A" type links can appear in other places too hence need to keep the define independent

I get the following error with the code
lxml.etree.RelaxNGParseError: Element or text conflicts in interleave, line 6

Example code below

from io import StringIO
from lxml import etree

XML = StringIO('''
<root xmlns="http://www.w3.org/2005/Atom">
    <link rel="a1" />
    <link rel="b1" />
    <link rel="a2" />
    <link rel="b2" />
</root>
''')

RNG = StringIO('''
<grammar xmlns="http://relaxng.org/ns/structure/1.0">

    <start>
        <element name="root" ns="http://www.w3.org/2005/Atom">
            <interleave>
                <zeroOrMore>
                    <ref name="aLink"/>
                </zeroOrMore>
                <oneOrMore>
                    <ref name="bLink"/>
                </oneOrMore>
            </interleave>
        </element>
    </start>

    <define name="aLink" ns="http://www.w3.org/2005/Atom">
        <element name="link">
            <choice>
                <attribute name="rel">
                    <value>a1</value>
                </attribute>
                <attribute name="rel">
                    <value>a2</value>
                </attribute>
            </choice>
        </element>
    </define>

    <define name="bLink" ns="http://www.w3.org/2005/Atom">
        <element name="link">
            <choice>
                <attribute name="rel">
                    <value>b1</value>
                </attribute>
                <attribute name="rel">
                    <value>b2</value>
                </attribute>
            </choice>
        </element>
    </define>

</grammar>
''')

rng_tree = etree.parse(RNG)
rng_schema = etree.RelaxNG(rng_tree)

rng_schema.validate(XML)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一花一树开 2025-01-27 09:31:52

我担心这是一个典型的情况,被放松的规格禁止

7.4。交织的限制

对于模式P1 P2,

  • 一定不能有一个属于p1中引用的ref模式的元素模式的名称类别,而属于P2中引用的元素模式的名称类别的名称类别,
    在P1和P2中不得出现文本模式。

I fear that it's a typical case that is forbidden by the specification of Relax NG

7.4. Restrictions on interleave

For a pattern p1 p2 ,

  • there must not be a name that belongs to both the name class of an element pattern referenced by a ref pattern occurring in p1 and the name class of an element pattern referenced by a ref pattern occurring in p2, and
    a text pattern must not occur in both p1 and p2.

https://www.oasis-open.org/committees/relax-ng/spec-20011203.html#interleave-restrictions

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