Anto
Anto

Reputation: 419

XSD Use single restriction for multiple elements

I have an XML file that is produced from a customer system where decimal values are output with commas instead of decimal points so like 10,5 or 1250,50 instead of 10.5 or 1250.50. I need to validate and deserialise this XML file so I am trying to get a XSD schema created to do the validation and once validated deserialise the file. Everything works ok except for validating the numbers because of the comma. I have the following restriction for the XSD but I cannot figure out how to add the restriction once and use it for multiple different elements since there can be a lot of items with numbers in them:

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

I've tried to add the following block inside the InvoiceRow element in the XSD sample below but it just seems to break the XSD format:

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

This is my XSD and the places where "xs:decimal" have been set are the items that have the number values with the commas in it and I need to use the restriction to validate them.

<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>

Thanks for any info or suggestions around this.

Upvotes: 0

Views: 516

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

Because your simple type DecimalValidation is named, it should appear at the top level of the schema as a global declaration (that is, as a child of xs:schema), and you should then be able to replace all references to xs:decimal by references to DecimalValidation. (It looks like there are no namespaces involved, but if there are, you might need to add a suitable namespace prefix.)

Upvotes: 1

Related Questions