我試圖從2個xml文檔生成xsd。在這2個xml文檔中,有一些簡單元素(<SubTotal>
和<Tax>
)和複雜類型元素(<SubTotals>
)是可選的;這意味着它們可能存在或不存在於XML文檔中。以下是2個示例XML文檔和1個破解的xsd文檔。從XML中的可選元素生成xsd
我生成的xsd文件,但它不工作。通過使用生成的xsd文檔,當我驗證XML文檔時,第一個XML文檔是有效的,但第二個xml文檔不是。 我希望xsd能夠同時處理XML文檔。這2個xml文檔中的xml數據是有效的。
在第一個XML文檔,可選簡單類型<SubTotal>
和<Tax>
元件本,但可選的複雜<SubTotals>
型元件是缺席。
在第二XML文檔中,可選的複雜類型<SubTotals>
元件是本,但2個可選的簡單類型<SubTotal>
和<Tax>
元件缺席。
以下是第1個xml文檔。已成功驗證成功。
<ReceiptMessage>
<DeviceId>AA-BB-CC-DD-EE-FF</DeviceId>
<From>[email protected]</From>
<To>[email protected]</To>
<Subject>Your Receipt - Version 1</Subject>
<OptIn>255</OptIn>
<Receipt>
<CheckNo>13254</CheckNo>
<TableId>1</TableId>
<ReceiptDate>2015-09-23T11:20:00</ReceiptDate>
<Server>Joy Server</Server>
<CardNo>48757-Loyalty</CardNo>
<PaymentMode>Credit Card</PaymentMode>
<ReceiptHeader>
<string>Some Header 1</string>
<string>Some header 2</string>
</ReceiptHeader>
<SubTotal>35.00</SubTotal>
<Tax>1.00</Tax>
<Total>36.00</Total>
<Gratuity>2.00</Gratuity>
<SplitCheckTotal>38.00</SplitCheckTotal>
<Tip>1.50</Tip>
<AmountPaid>39.50</AmountPaid>
<ReceiptItems>
<ReceiptItem>
<ItemName>Pizza Hut ABC</ItemName>
<Qty>2</Qty>
<Price>4.00</Price>
</ReceiptItem>
<ReceiptItem>
<ItemName>Burito 289</ItemName>
<Qty>1</Qty>
<Price>8.35</Price>
</ReceiptItem>
</ReceiptItems>
<ReceiptFooter>
<string>Thank you for your shopping at our site</string>
<string>Please come back</string>
</ReceiptFooter>
<ReceiptSurvey>
<string>Survey 1 content</string>
<string>Survey - go to our site and register for sweeptakes</string>
</ReceiptSurvey>
</Receipt>
</ReceiptMessage>
以下是第2個xml文檔。是無效。
<ReceiptMessage>
<DeviceId>AA-BB-CC-DD-EE-FF</DeviceId>
<From>[email protected]</From>
<To>[email protected]</To>
<Subject>Your Receipt from XYZ- Version 2</Subject>
<OptIn>255</OptIn>
<Receipt>
<CheckNo>17282</CheckNo>
<TableId>Table ABC</TableId>
<ReceiptDate>2015-09-23T16:32:59.4561339-05:00</ReceiptDate>
<Server>John Doe</Server>
<CardNo>2920202</CardNo>
<PaymentMode>Credit Card</PaymentMode>
<ReceiptHeader>
<string>Header 1</string>
<string>Header 2</string>
</ReceiptHeader>
<SubTotals>
<SubtotalItem Label="Subtotal">25.00</SubtotalItem>
<SubtotalItem Label="Sales Tax">3.00</SubtotalItem>
<SubtotalItem Label="City Tax">1.15</SubtotalItem>
<SubtotalItem Label="County Tax">2.25</SubtotalItem>
<SubtotalItem Label="State Tax">1.25</SubtotalItem>
</SubTotals>
<Total>32.65</Total>
<Gratuity>2.00</Gratuity>
<SplitCheckTotal>0.5</SplitCheckTotal>
<Tip>3.00</Tip>
<AmountPaid>38.15</AmountPaid>
<ReceiptItems>
<ReceiptItem>
<ItemName>Pizza</ItemName>
<Qty>1</Qty>
<Price>5.32</Price>
</ReceiptItem>
<ReceiptItem>
<ItemName>Burito</ItemName>
<Qty>2</Qty>
<Price>10.99</Price>
</ReceiptItem>
</ReceiptItems>
<ReceiptFooter>
<string>Footer 1</string>
<string>Footer 2</string>
</ReceiptFooter>
<ReceiptSurvey>
<string>Go to our site to register and win awards to</string>
</ReceiptSurvey>
</Receipt>
</ReceiptMessage>
我想爲以下生成的xsd擁有正確的版本。它應該與這2個XML文檔一起工作。
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ReceiptMessage" nillable="true" type="ReceiptMessage" />
<xs:complexType name="ReceiptMessage">
<xs:complexContent mixed="false">
<xs:extension base="EmailParams">
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="OptIn" type="xs:unsignedByte" />
<xs:element minOccurs="0" maxOccurs="1" name="Receipt" type="ReceiptData" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="EmailParams">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="DeviceId" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="From" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="To" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="Subject" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ReceiptData">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="CheckNo" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="TableId" type="xs:string" />
<xs:element minOccurs="1" maxOccurs="1" name="ReceiptDate" type="xs:dateTime" />
<xs:element minOccurs="0" maxOccurs="1" name="Server" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="CardNo" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="PaymentMode" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="ReceiptHeader" type="ArrayOfString" />
<xs:element minOccurs="1" maxOccurs="1" name="SubTotal" nillable="true" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="Tax" nillable="true" type="xs:double" />
<xs:element minOccurs="0" maxOccurs="1" name="SubTotals" type="ArrayOfSubtotalItem" />
<xs:element minOccurs="1" maxOccurs="1" name="Total" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="Gratuity" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="SplitCheckTotal" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="Tip" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="AmountPaid" type="xs:double" />
<xs:element minOccurs="0" maxOccurs="1" name="ReceiptItems" type="ArrayOfReceiptItem" />
<xs:element minOccurs="0" maxOccurs="1" name="ReceiptFooter" type="ArrayOfString" />
<xs:element minOccurs="0" maxOccurs="1" name="ReceiptSurvey" type="ArrayOfString" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ArrayOfString">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="string" nillable="true" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ArrayOfSubtotalItem">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="SubtotalItem" nillable="true" type="SubtotalItem" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="SubtotalItem">
<xs:simpleContent>
<xs:extension base="xs:double">
<xs:attribute name="Label" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="ArrayOfReceiptItem">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="ReceiptItem" nillable="true" type="ReceiptItem" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ReceiptItem">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="ItemName" type="xs:string" />
<xs:element minOccurs="1" maxOccurs="1" name="Qty" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="Price" type="xs:double" />
</xs:sequence>
</xs:complexType>
</xs:schema>
我用下面的免費在線工具,基於生成的XSD來驗證我的XML文檔:
http://www.freeformatter.com/xml-validator-xsd.html
感謝您的回覆。我可以使用作爲這些可選元素嗎?如果是這樣,我該如何使用來編寫它的XSD? –
@ Thomas.Benz,是的,你可以。一些簡單的例子在這裏:http://www.w3schools.com/schema/el_choice.asp。但是,我可以建議使用一些帶上下文相關幫助(免費或商業)的XSD編輯器嗎?那麼你甚至不需要查看它,只需開始輸入查看建議即可。 – Abel