【问题标题】:XSD - ComplexType mixed with Content RestrictionXSD - ComplexType 与内容限制混合
【发布时间】:2015-10-20 20:37:45
【问题描述】:

我需要用一个 XSD 文件验证多个 XML 文件... XML 文件可以以多种形式出现......我需要抓住 所有情况。

XML1:

<BusinessRule>
   ABC
</BusinessRule>

XML2:

<BusinessRule>
<Case number="1">
  <Lookup class="type">
     <LkpColumn>column1</LkpColumn>
     <LkpText>someText1</LkpText>
  </Lookup>
</Case>
<Case number="2">
  <Lookup class="type">
     <LkpColumn>column2</LkpColumn>
     <LkpText>someText2</LkpText>
  </Lookup>
</Case>
<Case number="3">
  <Lookup class="type">
     <LkpColumn>column3</LkpColumn>
     <LkpText>someText3</LkpText>
  </Lookup>
</Case>
</BusinessRule>

XML3:

<BusinessRule>
   <Lookup class="type">    
      <LkpColumn>column</LkpColumn>
      <LkpText>someText</LkpText>
   </Lookup>
</BusinessRule>

XML4:

<BusinessRule>
   <If>condition1</If>
   <Then>then_bough1</Then>
   <Else>
       <If>condition2</If>
       <Then>then_bough2</Then>
       <Else>else1</Else>
   </Else>
</BusinessRule>

我的 XSD:

<!-- ... -->
<!-- BusinessRule -->
<xs:complexType name="BusinessRuleType" mixed="true">
    <!-- I NEED THIS PART, BUT IT DOESN'T WORK! -->
    <!--
    <xs:simpleContent>
        <xs:restriction base="xs:string">
            <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
        </xs:restriction>
    </xs:simpleContent> 
    -->
    <xs:choice>
        <xs:element name="Case" type="CaseType" minOccurs="0" maxOccurs="unbounded" />
        <xs:element name="Lookup" type="LookupType"/>
        <xs:sequence>
            <xs:element name="If"/>
            <xs:element name="Then"/>
            <xs:element name="Else"/>
        </xs:sequence>
    </xs:choice>
</xs:complexType>

<!-- case -->
<xs:complexType name="CaseType" mixed="true">
    <xs:sequence>
        <xs:element name="Lookup" type="LookupType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="number" type="xs:positiveInteger" use="required"/>
</xs:complexType>

<!-- LookUp -->
<!-- ... -->

我想我理解涵盖 XML2 - XML4... 但我还需要用一些“限制”标签检查 XML1 的内容...... 这可能吗?

感谢您的帮助!

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

    实际上,由于使用mixed="true",您的XML1 案例已经有效,但请注意,您的每个案例还允许在BusinessRule 内容的元素之间散布文本。我猜你可能不想要这个。如果不是,请删除 mixed="true" 并重新设计您的 XML1 案例以使用包装元素,例如 Symbol

    <xs:choice>
      <xs:element name="Symbol">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="Case" type="CaseType" minOccurs="0" maxOccurs="unbounded" />
      <xs:element name="Lookup" type="LookupType"/>
      <xs:sequence>
        <xs:element name="If"/>
        <xs:element name="Then"/>
        <xs:element name="Else"/>
      </xs:sequence>
    </xs:choice>
    

    然后,您将能够限制 XML1 案例的内容(前提是内容包装在专用元素中)。 使用mixed="true",无法使用 XSD 1.0 进一步限制文本。(Schematron 或 XSD 1.1 的xs:assert 可以对混合内容施加额外的限制——感谢@Abel提醒。)

    【讨论】:

    • “使用mixed="true",无法进一步限制文本。” >> 除非使用XSD 1.1 和断言,或者Schematron(不确定RelaxNG ),如果其中任何一个是一个选项。
    • 所以我需要重新设计我的 xslt 以获得类似: ABC 以便能够限制内容?我想如果我这样做,我就不再需要 mixed="true" ...
    • 对,如果没有mixed="true",您将防止文本出现在整个BusinessRule 内容中:例如:&lt;If&gt;condition1&lt;/If&gt;Probably don't want text here&lt;Then&gt;action1&lt;/Then&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 2013-10-10
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多