gpt4 book ai didi

xml - 使用 XSD 验证 XML 元素属性值

转载 作者:数据小太阳 更新时间:2023-10-29 02:51:26 27 4
gpt4 key购买 nike

我有以下 XML 元素。我需要验证 Parameter 属性是否仅在以下 XML 元素中包含 Y 或 N

<Test Script="abc.sh" Parameter="Y"/>
**OR**
<Test Script="abc.sh" Parameter="N"/>

我的 XSD 是:

    <xs:element name="Test" minOccurs="0">
<xs:complexType>
<xs:attribute name="Script" type="xs:string" use="required"/>
<xs:attribute name="Parameter" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>

当前此 XSD 未验证参数是否持有 YN

最佳答案

您需要使用表示限制simpleType 来定义属性,以便强制属性值成为已定义集合的成员值(value)观。

假设您有以下 xml:

<?xml version="1.0"?>
<Test Script="path/to/script" Parameter="Y" xmlns="http://www.example.org" />

您可以使用此 xsd 强制它们的 type 属性具有 foobar 的值:

<?xml version="1.0"?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org"
targetNamespace="http://www.example.org"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
>
<xsd:element name="Test">
<xsd:complexType>
<xsd:attribute name="Script" type="xsd:string" use="required"/>
<!-- the following has no type attribute. It's type is
defined in the simpleType child -->
<xsd:attribute name="Parameter" use="optional">
<xsd:simpleType>
<!-- define a set of xsd:strings
as possible values -->
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Y"/>
<xsd:enumeration value="N"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:schema>

关于xml - 使用 XSD 验证 XML 元素属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21183493/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com