我正在尝试使用xsd文件验证xml文件,以提交加拿大税务局的T2202 TAX SLIP
以下是我的xml文件,可以对其稍作更改以通过xsd模式文件进行验证。
<?xml version="1.0" encoding="UTF-8"?>
<Return>
<T2202>
<T2202Slip>
<SlipReportTypeCode>O</SlipReportTypeCode>
<FilerAccountNumber>000000000RZ0000</FilerAccountNumber>
<PostSecondaryEducationalSchoolProgramName>Webdevelopment</PostSecondaryEducationalSchoolProgramName>
<PostSecondaryEducationalSchoolTypeCode>1</PostSecondaryEducationalSchoolTypeCode>
<FlyingSchoolClubCourseTypeCode>1</FlyingSchoolClubCourseTypeCode>
<StudentName>
<FamilyName>Jacob</FamilyName>
<GivenName>peter</GivenName>
<NameInitialText>k</NameInitialText>
</StudentName>
<SocialInsuranceNumber>000000000</SocialInsuranceNumber>
<StudentNumber>501058988</StudentNumber>
<StudentAddress>
<AddressLine1Text>202-2526 LAKEVIEW</AddressLine1Text>
<AddressLine2Text>CRES</AddressLine2Text>
<CityName>ABBOTSFORD</CityName>
<ProvinceStateCode>BC</ProvinceStateCode>
<CountryCode>CAN</CountryCode>
<PostalZipCode>V2W3A9</PostalZipCode>
</StudentAddress>
<SchoolSession>
<StartYearMonth>1901</StartYearMonth>
<EndYearMonth>1909</EndYearMonth>
<EligibleTuitionFeeAmount>153.64</EligibleTuitionFeeAmount>
<PartTimeStudentMonthCount>9</PartTimeStudentMonthCount>
<FullTimeStudentMonthCount>0</FullTimeStudentMonthCount>
</SchoolSession>
<TotalEligibleTuitionFeeAmount>0</TotalEligibleTuitionFeeAmount>
<TotalPartTimeStudentMonthCount>0</TotalPartTimeStudentMonthCount>
<TotalFullTimeStudentMonthCount>0</TotalFullTimeStudentMonthCount>
</T2202Slip>
<T2202Summary>
<FilerAccountNumber>000000000RZ0000</FilerAccountNumber>
<SummaryReportTypeCode>O</SummaryReportTypeCode>
<TaxationYear>2019</TaxationYear>
<TotalSlipCount>1</TotalSlipCount>
<PostSecondaryEducationalInstitutionName>
<NameLine1Text>CAREER COLLEGE</NameLine1Text>
</PostSecondaryEducationalInstitutionName>
<PostSecondaryEducationalInstitutionMailingAddress>
<CityName>ABBOTSFORD</CityName>
<ProvinceStateCode>BC</ProvinceStateCode>
<CountryCode>CAN</CountryCode>
<PostalZipCode>V2W3A9</PostalZipCode>
</PostSecondaryEducationalInstitutionMailingAddress>
<ContactInformation>
<ContactName>JOHN</ContactName>
<ContactAreaCode>604</ContactAreaCode>
<ContactPhoneNumber>720-4037</ContactPhoneNumber>
<ContactExtensionNumber>6789</ContactExtensionNumber>
</ContactInformation>
<TotalEligibleTuitionFeeAmount>153.64</TotalEligibleTuitionFeeAmount>
</T2202Summary>
</T2202>
</Return>
请在我的xsd模式文件下面找到该文件,该文件来自政府网站。
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Description T2202 Return Complex and Element Types
Last updated May 2019
Version#: 1.19 (version #.yy)
-->
<xsd:schema xmlns:sdt="http://www.cra-arc.gc.ca/xmlns/sdt/2-2-0"
xmlns:ccms="http://www.cra-arc.gc.ca/xmlns/ccms/1-0-0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://www.cra-arc.gc.ca/xmlns/sdt/2-2-0" schemaLocation="standarddatatypes.xsd"/>
<xsd:import namespace="http://www.cra-arc.gc.ca/xmlns/ccms/1-0-0" schemaLocation="cracommonstructures.xsd"/>
<xsd:include schemaLocation="lemmcommontypes.xsd"/>
<xsd:complexType name="T2202SlipType">
<xsd:sequence>
<xsd:element ref="SlipReportTypeCode"/>
<xsd:element name="FilerAccountNumber" type="BNRZType"/>
<xsd:element name="PostSecondaryEducationalSchoolProgramName" type="Length1to30TextType"/>
<!-- School type code:
1. University
2. College
3. Other education institution providing courses at a post-secondary school level
4. Certified by the Minister of Employment and Social Development Canada
5. Flying school or club
-->
<xsd:element name="PostSecondaryEducationalSchoolTypeCode" type="OneToFiveCodeType"/>
<!-- Flying school or club course type code. Must have a value if School type code is 5,
should be blank if school type code is 1-4. Valid values:
1. Private pilots license
2. Commercial pilots licence
3. Civil flying instructor rating
4. Helicopter category rating
5. Instrument rating
6. Other
-->
<xsd:element name="FlyingSchoolClubCourseTypeCode" type="OneToSixCodeType" minOccurs="0"/>
<xsd:element name="StudentName" type="IndividualNameType"/>
<xsd:element name="SocialInsuranceNumber" type="ccms:SINType"/>
<xsd:element name="StudentNumber" type="Length1to20TextType" minOccurs="0"/>
<xsd:element name="StudentAddress" type="RequiredLine1AddressType"/>
<xsd:element name="SchoolSession" type="SchoolSessionType" minOccurs="1" maxOccurs="4"/>
<xsd:element name="TotalEligibleTuitionFeeAmount" type="Decimal13AmountType" minOccurs="0"/>
<xsd:element name="TotalPartTimeStudentMonthCount" type="ZeroToTwelveCountType"/>
<xsd:element name="TotalFullTimeStudentMonthCount" type="ZeroToTwelveCountType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="SchoolSessionType">
<xsd:all>
<xsd:element name="StartYearMonth" type="YYMMType"/>
<xsd:element name="EndYearMonth" type="YYMMType"/>
<xsd:element name="EligibleTuitionFeeAmount" type="Decimal11AmountType" minOccurs="0"/>
<xsd:element name="PartTimeStudentMonthCount" type="ZeroToTwelveCountType"/>
<xsd:element name="FullTimeStudentMonthCount" type="ZeroToTwelveCountType"/>
</xsd:all>
</xsd:complexType>
<xsd:complexType name="T2202SummaryType">
<xsd:all>
<xsd:element name="FilerAccountNumber" type="BNRZType"/>
<xsd:element ref="SummaryReportTypeCode"/>
<xsd:element name="FilerAmendmentNote" type="Length1to1309TextType" minOccurs="0"/>
<xsd:element ref="TaxationYear"/>
<xsd:element ref="TotalSlipCount"/>
<xsd:element name="PostSecondaryEducationalInstitutionName" type="ThreeLinedLength30NameType"/>
<xsd:element name="PostSecondaryEducationalInstitutionMailingAddress" type="RequiredLine1AddressType"/>
<xsd:element name="ContactInformation" type="ContactType3" minOccurs="0"/>
<xsd:element name="TotalEligibleTuitionFeeAmount" type="Decimal15AmountType"/>
</xsd:all>
</xsd:complexType>
</xsd:schema>
我需要验证xml文件。可以对xml文件进行少量更改。我收到以下错误
错误:元素“返回”:没有匹配的全局声明可用于验证根
提前致谢
我调试了您的XSD集,并最终隔离了关键错误:
因此,可以将minOccurs="0"
属性添加到您lemmcommontypes.xsd
喜欢的以下元素中
<xsd:element ref="AddressLine1Text" minOccurs="0" /> <!-- Added by edit -->
这可能是不可能的,因为您不能更改XSD。
或者,否则,将以下元素添加为XML的一部分
<PostSecondaryEducationalInstitutionMailingAddress>
<AddressLine1Text>WHATEVER!!!</AddressLine1Text> <!-- Added by edit -->
<CityName>ABBOTSFORD</CityName>
<ProvinceStateCode>BC</ProvinceStateCode>
<CountryCode>CAN</CountryCode>
<PostalZipCode>V2W3A9</PostalZipCode>
</PostSecondaryEducationalInstitutionMailingAddress>
另外,将Decimal13AmountType
in 的定义更改lemmcommontypes.xsd
为
<xsd:simpleType name="Decimal13AmountType">
<xsd:restriction base="sdt:AmountType">
<xsd:totalDigits value="13"></xsd:totalDigits>
<xsd:pattern value="[+]?\d{0,11}(\.\d{2})?"/>
<xsd:minInclusive value="0"/>
</xsd:restriction>
</xsd:simpleType>
这可能是不可能的,因为您不能更改XSD。
或者,如果您不能执行此操作,请将值TotalEligibleTuitionFeeAmount
从更改为0
,0.00
以使RegEx匹配。
使XML与您的XSD集匹配。
在这两种情况下,您都必须在上述XSD文件(从中复制frms.xsd
)中添加以下定义:
<xsd:complexType name="T2202ReturnType"> <!-- From frms.xsd -->
<xsd:sequence>
<xsd:element name="T2202Slip" type="T2202SlipType" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="T2202Summary" type="T2202SummaryType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ReturnChoiceType"> <!-- From frms.xsd -->
<xsd:choice>
<xsd:element name="T2202" type="T2202ReturnType" minOccurs="0"/>
</xsd:choice>
</xsd:complexType>
<xsd:element name="Return" type="ReturnChoiceType"/>
使上述XML匹配。
现在,XML应该验证了。
谢谢您的宝贵时间。通过添加此代码,XML现在有效。但是,可以将某些内容添加到xml文件中,而不用更改xsd文件吗?这是因为在报税时,我们需要使用适当的xml。不能更改xsd。
正如我在上面的答案中所写:选项1和3更改XSD,选项2和4更改XML。因此,如果您不能更改XSD,请使用选项2和4。这意味着添加行
<AddressLine1Text>WHATEVER!!!</AddressLine1Text>
并在XML中更改0
为0.00
。然后将答案最后一部分(在列表之后)中提到的定义添加到XSD。我对xml进行了更改。但是我说的是“将答案的最后一部分(在列表之后)添加到您的XSD中”。这是更改xsd文件的一种吗?
我必须添加最后一部分(我只是从
frms.xsd
文件中复制了这部分),以使您在问题中描述的整个情况都起作用。在一个完整的场景中,您可能已经frms.xsd
包括了,因此不需要添加。但是要使这个答案完整,是的。这就是为什么我提到MCVE:要使MCVE正常工作,必须添加最后一部分。“要验证您的文件,请使用验证解析器对照我们的模式文件检查.xml文件的格式和内容。” -我希望可以将代码从一个文件添加到另一个文件中。