【问题标题】:How to validate empty string value tag in xsd如何验证 xsd 中的空字符串值标记
【发布时间】:2010-02-14 10:15:13
【问题描述】:

我有一个 xml 文件,其中包含一些日期值和其他数据类型。

<Purchasedate Name="purcaseDate" value=""/>

我正在使用 xsd 文件验证这些 xml 文件。 在 xsd shcema 中,我为dd/mm/yyyy 格式编写了一个正则表达式模式。

如果 value 属性有一个值,这可以正常工作。 我的模式正在验证 value 属性。

该字段 (purchasedate) 不是强制性的。 如果 value="",这意味着我的模式也在验证空字符串,这不是强制性的。

我需要验证可选字段 我也在使用&lt;xs:attribute name="PurchaseDate" use="optional"&gt;

当值标签不为空时,我需要验证该字段。

【问题讨论】:

    标签: xsd


    【解决方案1】:

    这太容易了..

    您所要做的就是在您的 pattern

    中包含 空字符串规范

    这是做到这一点的方法.. &lt;xs:pattern value="|(Regular_pattern_goes_here)"/&gt;

    为了您的参考,我已经编写了一个示例代码块 .. 只需通过它们 ..

    示例 XML:

    <?xml version="1.0" encoding="utf-8"?>
    <xmln xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com XMLFile1.xsd" xmlns="http://www.xsdef.com/xml/123">
      <Purchasedate Name="purcaseDate" value=""/>
    </xmln>
    

    示例 XSD:(包括自定义类型 def)

    <xs:schema xmlns:xsLocal="http://www.xsdef.com/xml/123" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.xsdef.com/xml/123" elementFormDefault="qualified" attributeFormDefault="unqualified">
      <xs:element name="xmln">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Purchasedate">
              <xs:complexType>
                <xs:simpleContent>
                  <xs:extension base="xs:string">
                    <xs:attribute name="Name" type="xs:string" use="required" />
                    <xs:attribute name="value" type="xsLocal:CUSTOM_DATE" use="required" />
                  </xs:extension>
                </xs:simpleContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:simpleType name="CUSTOM_DATE">
        <xs:restriction base="xs:string">
          <xs:pattern value="|((01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)/(01|02|03|04|05|06|07|08|09|10|11|12)/[1-2][0-9][0-9][0-9])"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:schema>
    

    【讨论】:

      【解决方案2】:

      尝试将此属性 nillable="true" 添加到 xml 标记定义中 你也可以看看他的链接http://www.zvon.org/xxl/XMLSchemaTutorial/Output/ser_over_st0.html
      Best Reagds,
      约旦

      【讨论】:

        【解决方案3】:

        “?”正则表达式中的字符表示它之前的字符必须出现 0 次或 1 次。

        因此,为了解决您的问题,您需要将正则表达式括在括号中并在末尾添加一个问号:

          <xs:simpleType name="PurchaseDateType">
            <xs:restriction base="xs:string">
              <xs:pattern value="(Regular_pattern_goes_here)?"/>
            </xs:restriction>
          </xs:simpleType>
        

        在你的领域使用这种类型,你应该没问题

        【讨论】:

          【解决方案4】:

          如果您控制 XML 的语法,则应考虑按如下方式定义元素。由于 XML-Schema 已经提供了一个日期类型,除非你有充分的理由,否则你应该使用它。我这样说是因为它会让其他人更容易使用 xml,并且让您以后使用更好的代码框架。我没有包含“name”属性,因为它似乎对元素名称是多余的。

          <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
          
          <xs:element name="Purchasedate" nillable="true" type="xs:date"/>
          <xs:element name="Purchasedate2">
              <xs:complexType>
                  <xs:attribute name="value" type="xs:date"/>
              </xs:complexType>
          </xs:element>
          <xs:element name="root">
              <xs:complexType>
                  <xs:sequence>
                      <xs:element ref="Purchasedate"/>
                      <xs:element minOccurs="0" ref="Purchasedate2"/>
                  </xs:sequence>
              </xs:complexType>
          </xs:element>
          

          【讨论】:

            猜你喜欢
            • 2019-11-03
            • 1970-01-01
            • 2013-07-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-05-08
            • 1970-01-01
            • 2012-12-13
            相关资源
            最近更新 更多