【问题标题】:Error: Element 'assert' Is Invalid, Misplaced, Or Occurs Too Often错误:元素“断言”无效、放错位置或过于频繁地出现
【发布时间】:2018-08-01 19:01:24
【问题描述】:

我在 XSD 中尝试过断言,但出现错误

元素“断言”无效、放错位置或过于频繁地出现。

我的例子是下面的 XML。

`<?xml version="1.0" encoding="utf-8"?>
<p:CustomerElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <recordCount>1234</recordCount>
  <Customer>
      <indicator>A</indicator>
      <test1>hdjfs</test1>
      <test2>idsfh</test2>
	  <test3>idsfh</test3>
	  <test4>idsfh</test4>
	  <test5>idsfh</test5>
	  <test6>idsfh</test6>
	  <test7>idsfh</test7>
	</Customer>
    <Customer>
      <indicator>D</indicator>
      <test1>abcd</test1>
	  <test3>jhf</test3>
    </Customer>
</p:CustomerElement>`

我为此创建的 XSD 是

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns="http://My.Schema.Namespace" 
           targetNamespace="http://My.Schema.Namespace"
		   xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           elementFormDefault="qualified"
           vc:minVersion="1.1">>

  <xs:element name="customer">
    <xs:complexType>
	<xs:sequence>
      <xs:element name="indicator">
	<xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:enumeration value="A" />
            <xs:enumeration value="B" />
          </xs:restriction>
    </xs:simpleType>
	  </xs:element>
      <xs:choice>
        <xs:element name="test1" />
        <xs:element name="test2" />
		<xs:element name="test3" />
		<xs:element name="test4" />
		<xs:element name="test5" />
		<xs:element name="test6" />
		<xs:element name="test7" />
      </xs:choice>
	  </xs:sequence>
    <xs:assert test="if(indicator eq 'A') then test1 and test2 and test3 and test4 
						and test5 and test6 and test7
						else if(indicator eq 'B') then test1 and test3"/>
	</xs:complexType>
  </xs:element>
 </xs:schema>

我在freeformat validator 中使用 XSD 验证上述 xml。

我猜断言的语法中几乎没有错误。任何人都可以就我正在寻找的条件提供帮助并在语法方面帮助我,如果我添加了支持 XSD 1.1 架构的正确链接,请告诉我。

【问题讨论】:

  • @kjhughes 你能帮我这个案子吗?

标签: xml xsd xml-parsing xsd-validation xsd-1.1


【解决方案1】:

要解决 XSD 的问题:

  1. 改变

       elementFormDefault="qualified"
       vc:minVersion="1.1">>
    

       vc:minVersion="1.1">
    

    消除虚假的&gt; 和重复的elementFormDefault 属性。

  2. else false() 添加到您的断言测试中,使其格式正确。更好的是,重写为没有复合 if-else 构造的逻辑等价物。

  3. 如果您希望使用 xs:assert,请使用 XSD 1.1 处理器。 Freeformat.com 仅支持 XSD 1.0

格式良好的 XSD

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns="http://My.Schema.Namespace" 
  targetNamespace="http://My.Schema.Namespace"
  xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
  vc:minVersion="1.1">

  <xs:element name="customer">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="indicator">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="A" />
              <xs:enumeration value="B" />
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
        <xs:choice>
          <xs:element name="test1" />
          <xs:element name="test2" />
          <xs:element name="test3" />
          <xs:element name="test4" />
          <xs:element name="test5" />
          <xs:element name="test6" />
          <xs:element name="test7" />
        </xs:choice>
      </xs:sequence>
      <xs:assert test="if (indicator eq 'A') 
        then test1 and test2 and test3 and test4 and test5 and test6 and test7
        else if (indicator eq 'B') then test1 and test3 else false()"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

假设上述障碍可以克服,那么针对此 XSD 实现 XML 的有效性留给提问者完成。

【讨论】:

  • 您可能打算使用else false() 而不是else false。效果可能是一样的,但是使用false 代替false() 会诱使人们使用true 来表示true()...
  • @MichaelKay:很好。固定的。谢谢!
  • @kjhughes 非常感谢您的解决方案!有什么办法可以优化吗? 喜欢而不是提到所有的列,我们可以把它变小,因为在我的真实情况下,我有大约 100 列?
  • 逻辑可以重写:隐含可以转换为否定和析取,布尔代数可以简化等。但是,您还没有说明您的高级目标,所以我不打算追逐 XY 问题的错误结束。由于我已经帮助您解决了您发布的问题,请先accept这个答案。然后,问一个关于如何处理 100 列问题的新问题,并确保在新问题中明确你的高层次目标。谢谢。
  • @kjhughes 嘿,你能帮我解决这个问题吗? Question 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多