【问题标题】:xml:all wants all or nothing?xml:all 想要全部还是什么都不想要?
【发布时间】:2016-10-15 08:28:26
【问题描述】:

寻找 XML 专家来回答异常。我检查了多个网页,但运气不佳。

我有一个复杂的数据类型,其中 xs:all 设置为 minOccurs=0,如下所示:

<xs:complexType name="EffectRecord">
        <xs:complexContent>
            <xs:restriction base="xs:anyType">
                <xs:all minOccurs="0" maxOccurs="1">
                    <xs:element name="P1Name" type="xs:string"/>
                    <xs:element name="P2Name" type="xs:string"/>
                    <xs:element name="P3Name" type="xs:string"/>
                    <xs:element name="BasedOnStat" type="xs:string"/>
                </xs:all>
                <xs:attribute name="EffectName" type="xs:string" use="required"/>
                <xs:attribute name="Description" type="xs:string" use="required"/>
                <xs:attribute name="CostFormula" type="xs:string" use="required"/>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>

我稍后在我的架构中使用该类型,如下所示:

xs:element name="World">
    <xs:complexType>
        <xs:all>
            <xs:element name="Statistics">
                <xs:complexType>
                    <xs:sequence minOccurs="1" maxOccurs="unbounded">
                        <xs:element name="Statistic" type="StatRecord"/>
                    </xs:sequence>
                </xs:complexType>
                <xs:key name="StatKey">
                    <xs:selector xpath="World/Statistics"/>
                    <xs:field xpath="@Name"/>
                </xs:key>
                <xs:keyref refer="StatKey" name="StatRef">
                    <xs:selector xpath="EffectRecord"/>
                    <xs:field xpath="BasedOnStat"/>
                </xs:keyref>
            </xs:element>
            <xs:element name="Effects">
                <xs:complexType>
                    <xs:sequence maxOccurs="unbounded" minOccurs="0">
                        <xs:element name="Effect" type="EffectRecord"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:all>
    </xs:complexType>
</xs:element>

当我尝试在我的模式驱动的 XML 编辑器中使用此模式编写文档时,可以这样:

<?xml version="1.0" encoding="UTF-8"?>

<Statistics>
    <Statistic Name="Strength" Abbreviation="STR" Cost="3"/>
    <Statistic Name="Toughness" Abbreviation="TOU" Cost="2"/>
</Statistics>
<Effects>
    <Effect EffectName="foo" Description="A foo" CostFormula="P1*2">

    </Effect>
</Effects>

但这会产生错误元素“效果”的内容不完整。应为“{P2Name, P3Name, BasedOnStat}”之一。

<?xml version="1.0" encoding="UTF-8"?>

<Statistics>
    <Statistic Name="Strength" Abbreviation="STR" Cost="3"/>
    <Statistic Name="Toughness" Abbreviation="TOU" Cost="2"/>
</Statistics>
<Effects>
    <Effect EffectName="foo" Description="A foo" CostFormula="P1*2">
        <P1Name></P1Name>
    </Effect>
</Effects>

为什么包含这些元素之一意味着它们都必须包含在内?这与我找到的 xs:all 的任何定义都不匹配。我想要的是所有的子元素都是可选的,但最多显示一次。

谢谢,真的让我的头撞到屏幕上。

【问题讨论】:

    标签: xml schema


    【解决方案1】:

    好的,我回答了我自己的问题。我的 minOccurs 级别错误。对于我想要的效果,它属于每个元素,而不是全部。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      • 2011-07-06
      • 2021-02-07
      • 1970-01-01
      • 2011-02-11
      • 2021-06-14
      • 2021-03-18
      相关资源
      最近更新 更多