简单类型字符串的 XML 模式不区分大小写枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/359359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:15:09  来源:igfitidea点击:

XML Schema Case Insensitive Enumeration of Simple Type String

xmlschemasimpletypecase-sensitive

提问by Bobby Cannon

I am in need of a case insensitive string enumeration type in my XML schema (.xsd) file. I can get case insensitive by doing the following.

我的 XML 架构 (.xsd) 文件中需要一个不区分大小写的字符串枚举类型。通过执行以下操作,我可以不区分大小写。

<xs:simpleType name="setDigitalPointType">
    <xs:restriction base="xs:string">
        <xs:pattern value="[Oo][Nn]" />
        <xs:pattern value="[Oo][Ff][Ff]" />
    </xs:restriction>
</xs:simpleType>

The only problem is that I get no enumeration values. I will not get the nice intellesense when using Visual Studio to write my XML. The following will give me enumerations but it is case sensitive.

唯一的问题是我没有得到枚举值。使用 Visual Studio 编写我的 XML 时,我不会得到很好的智能感知。以下将为我提供枚举,但区分大小写。

<xs:simpleType name="setDigitalPointType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="on" />
        <xs:enumeration value="off" />
    </xs:restriction>
</xs:simpleType>

This will give me my enumerations but if I ever receive a value of "On", "ON", or "oN" it will fail verification.

这将为我提供我的枚举,但如果我收到“On”、“ON”或“oN”的值,它将无法通过验证。

I want enumeration of "on", "off" and allow entry of case insensitive versions.

我想要枚举“on”、“off”并允许输入不区分大小写的版本。

采纳答案by joel.neely

IBM developerWorks has an articleon how to use XSLT to perform the construction of the full set of enumeration alternatives in an automated fashion. It is presented as a workaround to the lack of case-insensitive enumerations.

IBM developerWorks 有一篇关于如何使用 XSLT 以自动化方式构建全套枚举替代方案的文章。它是作为缺少不区分大小写的枚举的一种变通方法。

回答by Orvid King

If you want to both keep the case-insensitive validation, while still getting Intellisense in Visual Studio 2010, you can use a union:

如果您想同时保持不区分大小写的验证,同时仍然在 Visual Studio 2010 中获得 Intellisense,您可以使用联合:

<xs:simpleType name="setDigitalPointType">
    <xs:union>
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="on" />
                <xs:enumeration value="off" />
            </xs:restriction>
        </xs:simpleType>
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:pattern value="[Oo][Nn]" />
                <xs:pattern value="[Oo][Ff][Ff]" />
            </xs:restriction>
        </xs:simpleType>
    </xs:union>
</xs:simpleType>

This works because it exploits the fact that Visual Studio 2010 only processes the first simple type in a union when it builds it's Intellisense data. However when it validates a document, it processes both, which means "On" is still determined to be valid even though it isn't in the list of enumeration values.

之所以有效,是因为它利用了 Visual Studio 2010 在构建智能感知数据时仅处理联合中的第一个简单类型这一事实。然而,当它验证一个文档时,它会同时处理这两个,这意味着“On”仍然被确定为有效,即使它不在枚举值列表中。

回答by leppie

Well, you could just list all the permutations as patterns :)

好吧,您可以将所有排列列为模式:)