限制一个 XML 请求属性值和少数元素值 [英] Restrict the a XML request attribute value and few of the element value

查看:18
本文介绍了限制一个 XML 请求属性值和少数元素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个帖子是帖子的扩展:使用XSD限制属性值和元素的值

This post is an extension of the post :Restrict the attribute value and the value of the element using XSD

现在我可以限制所有的属性 &使用 XSD 的元素值.但我现在希望只限制一两个元素值,而不是全部.

Now I am able to restrict all the attribute & element value using the XSD. But I'm now looking to restrict only one or two of the element value and not all of them.

XML:

<response src="XML">
  <resp name="JSON">letter.c</resp>
  <resp name="SWAGGER">di.js</resp>
  <resp name="BI">bi.j</resp>
</response>

XSD:

<xs:schema attributeFormDefault="unqualified"
          elementFormDefault="qualified"
          xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="response">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="resp" maxOccurs="14" minOccurs="1">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="respFilter">
                                <xs:attribute name="name" use="required" 
                                              type="Enum" />
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="src" use="required">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="XML"></xs:enumeration>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
    </xs:element>
    <xs:simpleType name="Enum">
        <xs:restriction base="xs:string">
            <xs:enumeration value="JSON">
            </xs:enumeration>
            <xs:enumeration value="SWAGGER">
            </xs:enumeration>
            <xs:enumeration value="BI">
            </xs:enumeration>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="respFilter">
        <xs:restriction base="xs:string">
            <xs:minLength value="0"></xs:minLength>
            <xs:maxLength value="1064"></xs:maxLength>
            <xs:enumeration value="letter.c"/>
            <xs:enumeration value="di.js"/>
            <xs:enumeration value="bi.j"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

如果其中包含以下值,则此 XSD 仅允许上述 XML 请求:

This XSD allows only the above XML request if it has the below value in it:

letter.c
di.js
bi.j

但我现在要做的是动态地允许很少的元素值,以便 XSD 验证以下所有请求.

But what I'm looking to do now is to dynamically allow few element values so that the XSD validates all the below requests.

1.

<response src="XML">
  <resp name="JSON">letter.c</resp>
  <resp name="SWAGGER"></resp>
  <resp name="BI">bi.j</resp>
</response>

2.

<response src="XML">
  <resp name="JSON">letter.c</resp>
  <resp name="SWAGGER">23.x</resp>
  <resp name="BI">bi.j</resp>
</response>

3.

<response src="XML">
  <resp name="JSON">letter.c</resp>
  <resp name="SWAGGER">di.js</resp>
  <resp name="BI">dj.c</resp>
</response>

所以基本上,属性 JSON 将始终具有 letter.c,XSD 应丢弃 JSON 中除 letter.c 之外的任何内容.另一方面,属性 SWAGGER &BI 中可以有任何值或空值.

So basically, attribute JSON will always have letter.c, anything other than letter.c in JSON should be discarded by the XSD. On the other hand attribute SWAGGER & BI can have any or empty value in it.

推荐答案

为了让验证根据 @name 的值而变化,您需要来自 XSD 1.1 的断言或条件类型分配.如果您仅限于 XSD 1.0,则必须针对 XSD 测试带外约束,或者必须重新设计 XML 以减少元数据:

In order to have validation vary per the value of @name, you need assertions or conditional type assignment from XSD 1.1. If you're limited to XSD 1.0, you'll have to test your constraint out-of-band with respect to XSD, or you'll have to redesign your XML to make it less meta:

<response src="XML">
  <JSON>letter.c</JSON>
  <SWAGGER>di.js</SWAGGER>
  <BI>bi.j</BI>
</response>

此 XML 可以在 XSD 1.0 中使用 JSON 的固定值和 SWAGGERBI 的枚举.

This XML could use a fixed value for JSON and enumerations for SWAGGER and BI in XSD 1.0.

这篇关于限制一个 XML 请求属性值和少数元素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆