【问题标题】:XSD Use single restriction for multiple elementsXSD 对多个元素使用单一限制
【发布时间】:2016-10-16 20:09:51
【问题描述】:

我有一个从客户系统生成的 XML 文件,其中十进制值使用逗号而不是小数点输出,例如 10,5 或 1250,50 而不是 10.5 或 1250.50。 我需要验证和反序列化这个 XML 文件,所以我试图创建一个 XSD 模式来进行验证,并在验证后反序列化文件。 一切正常,除了因为逗号而验证数字。 我对 XSD 有以下限制,但我无法弄清楚如何添加一次限制并将其用于多个不同的元素,因为其中可能有很多带有数字的项目:

<xs:restriction base="xs:string">
   <xs:pattern value="^\d+(\,\d{1,2})?$"/>
</xs:restriction>

我尝试在下面的 XSD 示例中的 InvoiceRow 元素中添加以下块,但它似乎破坏了 XSD 格式:

<xs:simpleType name="DecimalValidation">
  <xs:restriction base="xs:string">
    <xs:pattern value="^\d+(\,\d{1,2})?$"/>
  </xs:restriction>
</xs:simpleType>

这是我的 XSD,设置了“xs:decimal”的地方是其中包含带有逗号的数值的项目,我需要使用限制来验证它们。

<xs:element name="InvoiceRow" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="ArticleIdentifier"/>
              <xs:element type="xs:string" name="ArticleName"/>
              <xs:element name="OrderedQuantity">
                <xs:complexType>
                  <xs:simpleContent>
                    <xs:extension base="xs:short">
                      <xs:attribute type="xs:string" name="QuantityUnitCode" use="optional"/>
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
              <xs:element name="UnitPriceAmount">
                <xs:complexType>
                  <xs:simpleContent>
                    <xs:extension base="xs:decimal">
                      <xs:attribute type="xs:string" name="AmountCurrencyIdentifier" use="optional"/>
                      <xs:attribute type="xs:string" name="UnitPriceUnitCode" use="optional"/>
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
              <xs:element type="xs:short" name="RowVatRatePercent"/>
              <xs:element name="RowVatAmount">
                <xs:complexType>
                  <xs:simpleContent>
                    <xs:extension base="xs:decimal">
                      <xs:attribute type="xs:string" name="AmountCurrencyIdentifier" use="optional"/>
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
              <xs:element name="RowVatExcludedAmount">
                <xs:complexType>
                  <xs:simpleContent>
                    <xs:extension base="xs:decimal">
                      <xs:attribute type="xs:string" name="AmountCurrencyIdentifier" use="optional"/>
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
              <xs:element name="RowAmount">
                <xs:complexType>
                  <xs:simpleContent>
                    <xs:extension base="xs:decimal">
                      <xs:attribute type="xs:string" name="AmountCurrencyIdentifier" use="optional"/>
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>

感谢您提供有关此问题的任何信息或建议。

【问题讨论】:

    标签: c# xml xsd xml-validation


    【解决方案1】:

    因为您的简单类型 DecimalValidation 已命名,它应该作为全局声明出现在架构的顶层(即,作为 xs:schema 的子级),然后您应该能够替换所有引用通过引用DecimalValidationxs:decimal。 (看起来没有涉及命名空间,但如果有,您可能需要添加合适的命名空间前缀。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      • 2019-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多