2013-04-30 93 views
2

我正在编写一个XSD模式以验证存储(除其他外)若干个result元素的文档。此元素的两个属性clickIndextimeViewed是可选的,并使用内置数据类型nonNegativeIntegerdecimal。而不是删除这些属性时,它们的值未设置的,我想将它们设置为空字符串,像这样:验证属性中的空字符串

<Page resultCount="10" visited="true"> 
    <Result index="1" clickIndex="1" timeViewed="45.21" pid="56118" title="alpha"/> 
    <Result index="2" clickIndex="" timeViewed="" pid="75841" title="beta"/> 
    <Result index="3" clickIndex="2" timeViewed="21.45" pid="4563" title="gamma"/> 
    <Result index="4" clickIndex="" timeViewed="" pid="44546" title="delta"/> 
    <Result index="5" clickIndex="" timeViewed="" pid="1651" title="epsilo"/> 
    <Result index="6" clickIndex="" timeViewed="" pid="551651" title="zeta"/> 
    <Result index="7" clickIndex="" timeViewed="" pid="20358" title="eta"/> 
    <Result index="8" clickIndex="" timeViewed="" pid="61621" title="theta"/> 
    <Result index="9" clickIndex="" timeViewed="" pid="135154" title="iota"/> 
    <Result index="10" clickIndex="" timeViewed="" pid="95821" title="kappa"/> 
</Page> 

不幸的是这不会验证对以下XSD:

<xs:element name="Result"> 
    <xs:complexType> 
     <xs:attribute name="index" type="xs:nonNegativeInteger" use="required"/> 
     <xs:attribute name="clickIndex" type="xs:nonNegativeInteger" use="optional"/> 
     <xs:attribute name="timeViewed" type="xs:decimal" use="optional"/> 
     <xs:attribute name="pid" type="xs:positiveInteger" use="required"/> 
     <xs:attribute name="title" type="xs:string" use="required"/> 
    </xs:complexType> 
</xs:element> 

是否无法验证具有已定义类型的属性上的空字符串?有没有办法做到这一点,而不需要定义自定义类型?我是否仅仅忘记了XSD中的一个选项?

我将处理这些XML文件以生成统计信息,其中缺少的属性可能会引入不必要的复杂性。如果没有解决方法,我只需用零替换空值。我宁愿保留空字符串,因为它看起来更干净,但零也可能更有效。

回答

2

如果所讨论的属性是整数类型,则不能分配空字符串。引号内的值应该可以解析为整数。你必须或者像你说的那样用零值更新你的xml,或者删除保存空字符串值的属性。如果您不喜欢这些解决方案中的任何一种,那么您必须定义自己的自定义类型。您可以参考此了解更多详情

XSD make attribute nullable

+0

简洁,重点突出,我应该意识到解析问题。 – Lilienthal 2013-05-02 06:51:34