LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   XML Schema complexType (https://www.linuxquestions.org/questions/programming-9/xml-schema-complextype-129411/)

eric.r.turner 12-27-2003 10:36 PM

XML Schema complexType
 
I want to be able to do the following in XML:

Code:

<transaction>
  <amount currency="USD">100.00</amount>
</transaction>

How would I write the XML schema for this? My first attempt at a solution was:

Code:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:complexType name="amountType" type="xsd:float">
      <xsd:attribute name="currency" type="xsd:string"/>
  </xsd:complexType>

  <xsd:element name="transaction">
      <xsd:sequence>
        <xsd:element name="amount" type="amountType"/>
      </xsd:sequence>
  </xsd:element>

</xsd:schema>

That's obviously wrong because complexType cannot have a type attribute. How do I specify that <amount> has a currency attribute, and can have a positive floating point value as content?

Looking_Lost 12-28-2003 04:01 AM

I'm on the very early stages of XML but it could be done something like this I think. Basically define your own simple type for a currency value.. You can also use xsd:presicion value="10" or whatever to define the total number of digits used including decimal and xsd:scale for the number of decimal places rather than fractionDigits. There is probably a better, convaluted way,as thisvery much a beginner here but all practice is good :)

Code:

<?xml version="1.0"?>

<xsd:schema xlmns="http://www.w3.org/2000/10/XMLSchema">
 
  <xsd:element name="currencyTransaction">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="transaction" type="transactionType" minOccurs="0" maxOccurs="unbounded" / > 
    </xsd:sequence>
  </xsd:complexType>
  </xsd:element>

<xsd:complexType name="transactionType">
 <xsd:sequence>
  <xsd:element name="transactionCurrency" type="xsd:string"/>
 </xsd:sequence>

 <xsd:attribute name="transactionValue" type="currencyValue" use="required"/>

</xsd:complexType>

<xsd:simpleType="currencyValue">
 <xsd:restriction base="xsd:decimal">
  <xsd:minInclusive value="0"/>
  <xsd:fractionDigits value="2"/>
 </xsd:restriction>
</xsd:simpleType>

</xsd:schema>



All times are GMT -5. The time now is 10:50 AM.