验证元素(正则表达式)和属性(字符串)
Validate element (regex) and attribute (string)
我的问题如下:
在我的 xml 文件中,我有以下位:
<duration notation="mm:ss">03:14</duration>
现在,我需要在我的 xml 模式中验证这一点。如果这个元素的值是一个简单的字符串,不需要进一步关注,我就可以做到:
<xs:element name="duration" type="durationType" maxOccurs="1"/>
<xs:complexType name="durationType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="notation" type="xs:string" fixed="mm:ss"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
很遗憾,我想使用 Regex 验证此元素中的值。如果这是我唯一需要验证的东西,我会使用:
<xs:simpleType name="durationType">
<xs:restriction base="xs:string">
<xs:pattern value="\d{1,2}:\d{2}"/>
</xs:restriction>
</xs:simpleType>
我的问题是我似乎无法弄清楚如何同时验证两者。浏览互联网几个小时(您可能会说,complexType 代码直接来自 Whosebug 上的答案;)),但无济于事。
用不同的名称重命名验证标签内容的简单类型(例如 durationContent
):
<xs:simpleType name="durationContent">
<xs:restriction base="xs:string">
<xs:pattern value="\d{1,2}:\d{2}"/>
</xs:restriction>
</xs:simpleType>
现在只需使用 that 类型作为扩展的基础,它会添加一个属性:
<xs:complexType name="durationType">
<xs:simpleContent>
<xs:extension base="durationContent">
<xs:attribute name="notation" type="xs:string" fixed="mm:ss"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
现在它验证属性和标签的内容。
我的问题如下: 在我的 xml 文件中,我有以下位:
<duration notation="mm:ss">03:14</duration>
现在,我需要在我的 xml 模式中验证这一点。如果这个元素的值是一个简单的字符串,不需要进一步关注,我就可以做到:
<xs:element name="duration" type="durationType" maxOccurs="1"/>
<xs:complexType name="durationType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="notation" type="xs:string" fixed="mm:ss"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
很遗憾,我想使用 Regex 验证此元素中的值。如果这是我唯一需要验证的东西,我会使用:
<xs:simpleType name="durationType">
<xs:restriction base="xs:string">
<xs:pattern value="\d{1,2}:\d{2}"/>
</xs:restriction>
</xs:simpleType>
我的问题是我似乎无法弄清楚如何同时验证两者。浏览互联网几个小时(您可能会说,complexType 代码直接来自 Whosebug 上的答案;)),但无济于事。
用不同的名称重命名验证标签内容的简单类型(例如 durationContent
):
<xs:simpleType name="durationContent">
<xs:restriction base="xs:string">
<xs:pattern value="\d{1,2}:\d{2}"/>
</xs:restriction>
</xs:simpleType>
现在只需使用 that 类型作为扩展的基础,它会添加一个属性:
<xs:complexType name="durationType">
<xs:simpleContent>
<xs:extension base="durationContent">
<xs:attribute name="notation" type="xs:string" fixed="mm:ss"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
现在它验证属性和标签的内容。