我有一个XML
文件例如如下所示。我想确保menonic
始终存在且不是空字符串。
我设置use="required"
在我的 XSD 中像这样
<xs:attribute type="xs:string" name="mnemonic" use="required"/>
它确保该字段存在。
但是当字符串为空时,如下所示
<recipeStructureDef mnemonic="" title="Recipe.Bread">
它通过了验证。
有什么方法可以检查字符串是否不是空字符串吗?
谢谢,请找到下面的 xml 和 xsd 文件。
<recipeStructureDef mnemonic="Rice Bag" title="Recipe.Bread">
<description>
</description>
<parametersTab>
<parameterTabDef title="Main Parameters">
<parameterGroup title="Product Rice">
</parameterGroup>
</parameterTabDef>
</parametersTab>
</recipeStructureDef>
需要OP:
<xs:complexType name="recipeStructureDefType">
<xs:sequence>
<xs:element type="xs:string" name="description"/>
<xs:element type="parametersTabType" name="parametersTab"/>
</xs:sequence>
<xs:attribute type="xs:string" name="mnemonic" use="required"/>
<xs:attribute type="xs:string" name="title"/>
</xs:complexType>
最佳答案
属性@mnemonic
必须存在:
<xs:attribute name="mnemonic" use="required" type="nonEmptyString"/>
^^^^^^^^^^^^^^
@mnemonic
的属性值不能为空:
<xs:simpleType name="nonEmptyString">
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
关于xml - 使用 XSD 确保 XML 验证中的字符串不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59885734/