XSD - ComplexType 与内容限制混合
XSD - ComplexType mixed with Content Restriction
我需要用一个 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...
但我还需要用一些 "Restriction"-Tags 检查 XML1 的内容...
这可能吗?
感谢您的帮助!
实际上,由于使用 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 的提醒。)
我需要用一个 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... 但我还需要用一些 "Restriction"-Tags 检查 XML1 的内容... 这可能吗?
感谢您的帮助!
实际上,由于使用 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 的提醒。)