XSD 模式是否允许循环组?

发布于 2024-09-29 20:47:12 字数 1238 浏览 1 评论 0原文

对于这个 xml:

<elem1 xmlns="http://www.fixprotocol.org/ns/fast/t/1.0">
 <elem2>
   <elem2/>
 </elem2>
</elem1>

我有这个模式,它似乎可以很好地针对 w3 模式验证服务,并且模式很好地验证了上述 XML。遗憾的是,xsd.exe 和其他一些工具报告它是一个错误。这是正确的吗? XML 模式是否不允许循环组引用?谢谢!

更新:架构不是我的,无法更改它:(

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.fixprotocol.org/ns/fast/t/1.0" xmlns:t="http://www.fixprotocol.org/ns/fast/t/1.0">

  <xs:element name="elem1">
    <xs:complexType>
      <xs:group ref="t:grp1" />
   </xs:complexType>
  </xs:element>

  <xs:group name="grp1">
    <xs:sequence>
      <xs:group ref="t:grp2" />
    </xs:sequence>
  </xs:group>

  <xs:group name="grp2">
    <xs:sequence>
      <xs:element minOccurs="0" name="elem2">
        <xs:complexType>
          <xs:group ref="t:grp1" />
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:group>

</xs:schema>

For this xml:

<elem1 xmlns="http://www.fixprotocol.org/ns/fast/t/1.0">
 <elem2>
   <elem2/>
 </elem2>
</elem1>

I have this schema, which seems to validate fine against w3 schema validation service, and the schema validates the above XML just fine. Sadly, xsd.exe and some other tools report it to be an error. Is that correct? Are circular group refs dissallowed by XML schema? Thanks!

Update: The schema is not mine, can't change it :(

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.fixprotocol.org/ns/fast/t/1.0" xmlns:t="http://www.fixprotocol.org/ns/fast/t/1.0">

  <xs:element name="elem1">
    <xs:complexType>
      <xs:group ref="t:grp1" />
   </xs:complexType>
  </xs:element>

  <xs:group name="grp1">
    <xs:sequence>
      <xs:group ref="t:grp2" />
    </xs:sequence>
  </xs:group>

  <xs:group name="grp2">
    <xs:sequence>
      <xs:element minOccurs="0" name="elem2">
        <xs:complexType>
          <xs:group ref="t:grp1" />
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:group>

</xs:schema>

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

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

发布评论

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

评论(4

情深缘浅 2024-10-06 20:47:12

这是一个合法的计划。问题是 xsd 正在尝试遍历所有依赖项。 MS版本预处理方案并扩展所有组。由于循环依赖性,这种扩展将是无限的,因此它会因错误而退出。对于 Mono 版本,有两种可能的情况:

  1. 它尝试遍历依赖树
    并最终陷入无限循环。
  2. 它试图扩大所有群体并
    最终陷入无限循环。

这只是我的猜测。我从未见过 Mono xsd 的实际源代码。

It's a legal scheme. Problem is that xsd is trying to traverse all dependencies. The MS version preprocesses scheme and expands all groups. Because of the cyclic dependency such expansion would be infinite so it quits with an error. With the Mono version there are two probable scenarios:

  1. It tries to traverse dependency tree
    and ends up in an infinite loop.
  2. It tries to expand all groups and
    ends up in an infinite loop.

That is just my guess. I never saw actual source codes of Mono xsd.

妞丶爷亲个 2024-10-06 20:47:12

这个问题与最近讨论同一问题的许多问题相关:循环组和微软的 xsd.exe,因此我认为应该回答它,即使它相当“旧”。

这种混乱是由循环组的资格引起的。根据 XSD 规范的 3.8.6 部分:

“不允许使用圆形群。即,在 a 的 {articles} 内
群中任何深度都不能存在其 {term} 为
组本身。”

根据上述内容,您的示例不是循环组,因为该组本身不依赖于自身作为粒子。您的架构是有效

这是一个循环组:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns="http://www.fixprotocol.org/ns/fast/t/1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.fixprotocol.org/ns/fast/t/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="elem1">
        <xsd:complexType>
            <xsd:group ref="grp1"/>
        </xsd:complexType>
    </xsd:element>
    <xsd:group name="grp1">
        <xsd:sequence>
            <xsd:choice>
                <xsd:group ref="grp1"/>
            </xsd:choice>                       
        </xsd:sequence>
    </xsd:group>
</xsd:schema>

人们无法重写真正的循环组但是,您的示例可以通过多种方式重写:下面的架构显示了基于的等效内容模型。 从

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema xmlns="http://www.fixprotocol.org/ns/fast/t/1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.fixprotocol.org/ns/fast/t/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:annotation>
        <xsd:documentation xmlns="">Generated from "Set1" under "Release2"</xsd:documentation>
    </xsd:annotation>

    <xsd:complexType name="grp1">
        <xsd:sequence>
            <xsd:element minOccurs="0" name="elem2" type="grp1"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:element name="elem1" type="grp1"/>
</xsd:schema> 

看到以下架构实际上适用于 xsd.exe 也是“有趣的”:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema xmlns="http://www.fixprotocol.org/ns/fast/t/1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.fixprotocol.org/ns/fast/t/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:annotation>
        <xsd:documentation xmlns="">Generated from "Set1" under "Release2"</xsd:documentation>
    </xsd:annotation>
    <xsd:element name="elem1">
        <xsd:complexType>
            <xsd:group ref="grp1"/>
        </xsd:complexType>
    </xsd:element>
    <xsd:group name="grp1">
        <xsd:sequence>
            <xsd:element minOccurs="0" name="elem2">
                <xsd:complexType>
                    <xsd:group ref="grp1"/>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:group>
</xsd:schema>

XML 实例的角度来看,所有三个有效架构都是等效的。

This question is being linked with many recent questions that talk about the same problem: circular groups, and Microsoft's xsd.exe, hence I think it should be answered, even though it is quite "old".

The confusion is caused by what qualifies as a circular group. According to section 3.8.6 of the XSD spec:

"Circular groups are disallowed. That is, within the {particles} of a
group there must not be at any depth a particle whose {term} is the
group itself."

Based on the above, your example is not a circular group, since the group itself does not rely on itself as a particle. Your schema is valid.

This is a circular group:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns="http://www.fixprotocol.org/ns/fast/t/1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.fixprotocol.org/ns/fast/t/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="elem1">
        <xsd:complexType>
            <xsd:group ref="grp1"/>
        </xsd:complexType>
    </xsd:element>
    <xsd:group name="grp1">
        <xsd:sequence>
            <xsd:choice>
                <xsd:group ref="grp1"/>
            </xsd:choice>                       
        </xsd:sequence>
    </xsd:group>
</xsd:schema>

One cannot rewrite a true circular group. However, your example can be rewritten in a couple of ways: the schema below shows an equivalent content model, based on recursive complex types.

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema xmlns="http://www.fixprotocol.org/ns/fast/t/1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.fixprotocol.org/ns/fast/t/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:annotation>
        <xsd:documentation xmlns="">Generated from "Set1" under "Release2"</xsd:documentation>
    </xsd:annotation>

    <xsd:complexType name="grp1">
        <xsd:sequence>
            <xsd:element minOccurs="0" name="elem2" type="grp1"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:element name="elem1" type="grp1"/>
</xsd:schema> 

It is also "entertaining" to see that the following schema actually works with xsd.exe:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema xmlns="http://www.fixprotocol.org/ns/fast/t/1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.fixprotocol.org/ns/fast/t/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:annotation>
        <xsd:documentation xmlns="">Generated from "Set1" under "Release2"</xsd:documentation>
    </xsd:annotation>
    <xsd:element name="elem1">
        <xsd:complexType>
            <xsd:group ref="grp1"/>
        </xsd:complexType>
    </xsd:element>
    <xsd:group name="grp1">
        <xsd:sequence>
            <xsd:element minOccurs="0" name="elem2">
                <xsd:complexType>
                    <xsd:group ref="grp1"/>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:group>
</xsd:schema>

From an XML instance perspective, all three valid schemas are equivalent.

夜访吸血鬼 2024-10-06 20:47:12

问题可能是您使用的工具不支持 XML 模式规范支持的所有可能性。当然,xsd.exe 并不支持所有内容。该规范非常庞大,不值得将其支持的所有内容提供到编程语言的映射,特别是当某些内容无法很好地映射时。

要解决此问题,您可以尝试创建一组模仿您要生成的 xml 的 C# 类,然后在这些类上运行 xsd.exe 以生成 xsd。可能还有一些其他 XML 模式构造可以支持您想要的内容。

The issue is probably that the tools you are using don't support all possibilities supported by the XML schema spec. Certainly xsd.exe doesn't support everything. The spec is gigantic and it isn't worth providing mappings from everything it supports into a programming language, particularly when some things just don't map very well.

To work around this, you could try to create a set of C# classes that mimic the xml you want to generate and then run xsd.exe on those classes to generate an xsd. There is probably some other XML schema construct that supports what you want.

过潦 2024-10-06 20:47:12

我不知道组,但 XSD.exe 支持圆形元素:

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Class1" nillable="true" type="Class1" />
  <xs:complexType name="Class1">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="Name" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" name="child" type="Class1" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

I don't know about groups but XSD.exe supports circular elements:

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Class1" nillable="true" type="Class1" />
  <xs:complexType name="Class1">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="Name" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" name="child" type="Class1" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文