我在一个文件夹中有三个 XML 文件,并且还有一个用于这些的 XSD 文件。用户将许多 XML 文件放入文件夹中,我需要使用模式文档检查它们是否正确。XML 文件本身不包含指向任何 XSD 文件的链接。
- AwardsXSDPath 变量是架构文件的路径
- strFileName 变量是正在处理的 XML 文件的路径
- StrFileList 变量是一个数组,列出了要检查的每个 XML 文件
我有一个循环,它依次获取每个文件并尝试根据 XSD 验证它们;-
'verify the schemas of the three XML files using the XSD file
For intFile = 1 To UBound(strFileList)
strFileName = ImportFolder & strFileList(intFile)
'Debug.Print strFileName
objSchemaCache.Add "http://somewhere.com/root", LoadXmlFile(AwardsXSDPath)
Set xmlDoc = LoadXmlFile(strFileName)
Set xmlDoc.schemas = objSchemaCache
Set objErr = xmlDoc.validate()
If objErr.errorCode = 0 Then
Debug.Print "No errors found"
Else
Debug.Print "Error parser: " & objErr.errorCode & "; " & objErr.reason
End If
Set xmlDoc = Nothing
Set objErr = Nothing
Next intFile
我有一个名为 LoadXmlFile 的函数
Function LoadXmlFile(Path As String) As MSXML2.DOMDocument60
Set LoadXmlFile = New MSXML2.DOMDocument60
With LoadXmlFile
.async = False
.validateOnParse = False
.resolveExternals = False
.Load Path
End With
End Function
即时窗口显示错误
错误解析器:-1072897500;该节点既无效也不无效,因为没有找到 DTD/Schema 声明。
我认为这意味着有问题的 XML 文件没有方案声明。我在 XML 文件本身中没有模式声明,因为它们没有附带。难道没有办法在文件本身没有 shcema 声明的情况下验证 XML 文件到 XSD 吗?
XSD 文件;-
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Awards">
<xs:complexType>
<xs:sequence>
<xs:element name="Header">
<xs:complexType>
<xs:sequence>
<xs:element name="FinAssType" type="xs:string" />
<xs:element name="FileType" type="xs:string" />
<xs:element name="ExtractDateTime" type="xs:dateTime" />
<xs:element name="AwardsFrom" type="xs:date" />
<xs:element name="AwardsTo" type="xs:date" minOccurs="0" />
<xs:element name="LACode" type="xs:int" minOccurs="0" />
<xs:element name="OrganisationName" type="xs:string" />
<xs:element name="SupplierDetails">
<xs:complexType>
<xs:sequence>
<xs:element name="SupplierID" type="xs:int" />
<xs:element name="SupplierName" type="xs:string" />
<xs:element name="SupplierAddress" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Records" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="CaseIdentification">
<xs:complexType>
<xs:sequence>
<xs:element name="CaseReference" type="xs:string" />
<xs:element name="DateOfApplication" type="xs:date" />
<xs:element name="ReasonForApplication" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ApplicantDetails">
<xs:complexType>
<xs:sequence>
<xs:element name="ApplicantTitle" type="xs:string" />
<xs:element name="ApplicantForename" type="xs:string" />
<xs:element name="ApplicantSurname" type="xs:string" />
<xs:element name="ApplicantNINO" type="xs:string" />
<xs:element name="DateOfBirth" type="xs:date" />
<xs:element name="ApplicantAddress1" type="xs:string" />
<xs:element name="ApplicantAddress2" type="xs:string" />
<xs:element name="ApplicantAddress3" type="xs:string" minOccurs="0" />
<xs:element name="ApplicantAddress4" type="xs:string" minOccurs="0" />
<xs:element name="ApplicantPostcode" type="xs:string" minOccurs="0" />
<xs:element name="ApplicantTelNo" type="xs:string" minOccurs="0" />
<xs:element name="ApplicantEmail" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewAddressDetails" minOccurs="0" >
<xs:complexType>
<xs:sequence>
<xs:element name="NewAddressMoveDate" type="xs:date" />
<xs:element name="NewAddress1" type="xs:string" />
<xs:element name="NewAddress2" type="xs:string" />
<xs:element name="NewAddress3" type="xs:string" minOccurs="0" />
<xs:element name="NewAddress4" type="xs:string" minOccurs="0" />
<xs:element name="NewAddressPostcode" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ThirdPartyContact">
<xs:complexType>
<xs:sequence>
<xs:element name="ThirdPartyRelationship" type="xs:string" />
<xs:element name="ThirdPartyName" type="xs:string" />
<xs:element name="ThirdPartyAddressLine1" type="xs:string" />
<xs:element name="ThirdPartyAddressLine2" type="xs:string" />
<xs:element name="ThirdPartyAddressLine3" type="xs:string" minOccurs="0" />
<xs:element name="ThirdPartyAddressLine4" type="xs:string" minOccurs="0" />
<xs:element name="ThirdPartyPostCode" type="xs:string" minOccurs="0" />
<xs:element name="ThirdPartyTelNo" type="xs:string" minOccurs="0" />
<xs:element name="ThirdPartyEmail" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ClaimDetails">
<xs:complexType>
<xs:sequence>
<xs:element name="ItemRequested">
<xs:complexType>
<xs:sequence>
<xs:element name="ClaimID" type="xs:int" />
<xs:element name="AssistanceType" type="xs:string" />
<xs:element name="GoodsType" type="xs:string" />
<xs:element name="AmountRequested" type="xs:decimal" minOccurs="0" />
<xs:element name="ItemQuantity" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="AwardDetail">
<xs:complexType>
<xs:sequence>
<xs:element name="AwardDate" type="xs:date" />
<xs:element name="AwardValue" type="xs:decimal" />
<xs:element name="QuantityAwarded" type="xs:int" />
<xs:element name="TotalAwardValue" type="xs:decimal" />
<xs:element name="PaymentType" type="xs:string" />
<xs:element name="Recoverable" type="xs:boolean" />
<xs:element name="Reference1Name" type="xs:string" minOccurs="0" />
<xs:element name="Reference1Value" type="xs:string" minOccurs="0" />
<xs:element name="Reference2Name" type="xs:string" minOccurs="0" />
<xs:element name="Reference2Value" type="xs:string" minOccurs="0" />
<xs:element name="Reference3Name" type="xs:string" minOccurs="0" />
<xs:element name="Reference3Value" type="xs:string" minOccurs="0" />
<xs:element name="Reference4Name" type="xs:string" minOccurs="0" />
<xs:element name="Reference4Value" type="xs:string" minOccurs="0" />
<xs:element name="Reference5Name" type="xs:string" minOccurs="0" />
<xs:element name="Reference5Value" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Trailer">
<xs:complexType>
<xs:sequence>
<xs:element name="NoOfRecords" type="xs:int" />
<xs:element name="TotalOfAwardValues" type="xs:decimal" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML文件之一如下
<?xml version="1.0" encoding="UTF-8"?>
<Awards>
<Header>
<FinAssType>XXX</FinAssType>
<FileType>Awards</FileType>
<ExtractDateTime>2013-04-03T16:50:15.483</ExtractDateTime>
<LACode />
<OrganisationName>Org</OrganisationName>
<SupplierDetails>
<SupplierID>3</SupplierID>
<SupplierName>xxx</SupplierName>
<SupplierAddress>xxx</SupplierAddress>
</SupplierDetails>
</Header>
<AwardRecords>
<CaseIdentification>
<CaseReference>XXX1156</CaseReference>
<DateOfApplication>2013-03-30</DateOfApplication>
<ReasonForApplication>Moving Home/Resettlement</ReasonForApplication>
</CaseIdentification>
<ApplicantDetails>
<ApplicantTitle>Mr</ApplicantTitle>
<ApplicantForename>test</ApplicantForename>
<ApplicantSurname>test</ApplicantSurname>
<ApplicantNINO>XX999999X</ApplicantNINO>
<DateOfBirth>1984-03-03</DateOfBirth>
<ApplicantAddress1>12</ApplicantAddress1>
<ApplicantAddress2>1</ApplicantAddress2>
<ApplicantAddress3>3</ApplicantAddress3>
<ApplicantAddress4>3</ApplicantAddress4>
<ApplicantPostcode>m16 7ha</ApplicantPostcode>
<ApplicantTelNo>01611234567</ApplicantTelNo>
<ApplicantEmail>test@test.com</ApplicantEmail>
<DateMovedIn>1991-11-28</DateMovedIn>
</ApplicantDetails>
<ClaimDetails>
<Claim>
<ItemRequested>
<ClaimID>20</ClaimID>
<AssistanceType>Beds and mattresses</AssistanceType>
<GoodsType>Double bed (standard mattress)</GoodsType>
<ItemQuantity>1</ItemQuantity>
</ItemRequested>
<AwardDetail>
<AwardDate>2013-03-30T11:33:27.307</AwardDate>
<AwardValue>134.03</AwardValue>
<QuantityAwarded>1</QuantityAwarded>
<TotalAwardValue>134.03</TotalAwardValue>
<PaymentType>Delivery to customer</PaymentType>
<Recoverable>0</Recoverable>
<Reference1Name>Delivery/accessibility info</Reference1Name>
<Reference1Value />
<Reference2Name>Notes</Reference2Name>
<Reference2Value />
<Reference3Value />
<Reference4Value />
<Reference5Value />
</AwardDetail>
</Claim>
</ClaimDetails>
</AwardRecords>
<AwardRecords>
<CaseIdentification>
<CaseReference>XXX1483</CaseReference>
<DateOfApplication>2013-04-03</DateOfApplication>
<ReasonForApplication>xxx</ReasonForApplication>
</CaseIdentification>
<ApplicantDetails>
<ApplicantTitle>Ms</ApplicantTitle>
<ApplicantForename>xxx</ApplicantForename>
<ApplicantSurname>xxx</ApplicantSurname>
<ApplicantNINO>xxx</ApplicantNINO>
<DateOfBirth>1959-04-25</DateOfBirth>
<ApplicantAddress1>xxx</ApplicantAddress1>
<ApplicantAddress2>xxx</ApplicantAddress2>
<ApplicantAddress3>xxx</ApplicantAddress3>
<ApplicantAddress4 />
<ApplicantPostcode>xxx</ApplicantPostcode>
<ApplicantTelNo>xxx</ApplicantTelNo>
<ApplicantEmail />
<DateMovedIn>2013-04-02</DateMovedIn>
</ApplicantDetails>
<ClaimDetails>
<Claim>
<ItemRequested>
<ClaimID>88</ClaimID>
<AssistanceType>xxx</AssistanceType>
<GoodsType>xxx</GoodsType>
<ItemQuantity>1</ItemQuantity>
</ItemRequested>
<AwardDetail>
<AwardDate>2013-04-03T15:54:55.100</AwardDate>
<AwardValue>134.03</AwardValue>
<QuantityAwarded>1</QuantityAwarded>
<TotalAwardValue>134.03</TotalAwardValue>
<PaymentType>Delivery to customer</PaymentType>
<Recoverable>0</Recoverable>
<Reference1Name>xxx</Reference1Name>
<Reference1Value />
<Reference2Name>Notes</Reference2Name>
<Reference2Value>xxx</Reference2Value>
<Reference3Value />
<Reference4Value />
<Reference5Value />
</AwardDetail>
</Claim>
</ClaimDetails>
</AwardRecords>
<Trailer>
<NoOfRecords>2</NoOfRecords>
<TotalOfAwardValues>268</TotalOfAwardValues>
</Trailer>
</Awards>
此外,在我使用架构验证后,我将需要一个进一步的循环来遍历文件并存储“Awards/Header/SupplierDetails/SupplierName”节点的内容 - 我该怎么做?
谢谢。