gpt4 book ai didi

delphi - 使用 Windows DOM 和 TXMLDocument 验证 XML : doesn't work on some computers

转载 作者:行者123 更新时间:2023-12-02 00:13:05 25 4
gpt4 key购买 nike

我有一些 Delphi 代码来读取和验证基于 XSD 文档的 XML 文件。我正在使用 Windows DOM (TMXLDocument)。 This Article 解释了底层逻辑。

它适用于某些计算机(即针对违规标签抛出异常)。但在较新的计算机上,它不会抛出任何异常。

我需要更改 Windows 中的设置才能使其正常工作吗?或者有人知道用于验证 XML 的 native Delphi 组件吗?

XSD 文件:http://www.nemsis.org/media/XSD/EMSDataSet.xsd

示例 XML(注意 E02_02 需要具有基于 xsd xyz.com/DataSet.xsd 的正值

<EMSDataSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.nemsis.org" xsi:schemaLocation="http://myfakedomain.com/DataSet.xsd">
<Header>
<Record>
<E02>
<E02_01>123</E02_01>
<E02_02>0</E02_02>
</E02>
</Record>
</Header>
</EMSDataSet>

德尔福代码:

XMLDoc:= TXMLDocument.Create(nil);
try
XMLDoc.ParseOptions:= [poResolveExternals, poValidateOnParse];
XMLDoc.LoadFromFile(filetocheck);
XMLDoc.Active:= True;
except
on E:EDOMParseError do begin
showMessage(e.Message);
end;
end;

异常(exception):

The element: '{http://www.nemsis.org}E02_02'  has an invalid value according to its data type.  Line: 20  <E02_02>0</E02_02>

最佳答案

TXMLDocument 在使用 MSXML 时不直接支持启用 XSD 验证,因此管理它是 MSXML 的责任。启用 poResolveExternalspoValidateOnParse 标志对此很重要,但还有一些其他因素需要考虑。最重要的是,虽然 MSXML 确实支持从 XML 内部引用 XSD,但它对于在加载 XML 时是否实际使用引用的 XSD 有一些限制:

Referencing XSD Schemas in Documents

To reference an XML Schema (XSD) schema from an XML document in MSXML 6.0, you can use any one of the following means to link a schema to an XML document so that MSXML will use the schema to validate the document contents.

  • Reference the XSD schema in the XML document using XML schema instance attributes such as either xsi:schemaLocation or xsi:noNamespaceSchemaLocation.

  • Add the XSD schema file to a schema cache and then connect that cache to the DOM document or SAX reader, prior to loading or parsing the XML document.

...

The xsi:schemaLocation attribute works well in situations where namespace prefixes are explicitly declared and used in the XML document you want to validate.

The following example shows an XML document that references an external XSD schema, MyData.xsd for us in validating nodes that are in the 'urn:MyData' namespace URI , which is mapped to the "MyData:" namespace prefix.

<catalog xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation="urn:MyData http://www.example.com/MyData.xsd"
<MyData:book xmlns:MyData="urn:MyData">
<MyData:title>Presenting XML</MyData:title>
<MyData:author>Richard Light</MyData:author>
</MyData:book>

In order for the MyData.xsd file to be paired with and used you to validate elements and attribute nodes that start with the "MyData:", the schema needs to use and contain the following schema attributes:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:MyData="urn:MyData"
targetNamespace="urn:MyData"
elementFormDefault="qualified">

These attributes declare the 'urn:MyData' namespace URI and the "MyData:" namespace prefix so that they correspond identically to how these declarations were made in the XML file. If they do not match, the schema at the specified location would never be invoked during validation.

您尚未显示您的 XSD,但您显示的 XML 不符合上述文档中提到的规则。特别是,您缺少使用 urn 命名空间映射以及要验证的 XML 节点上的前缀。某些版本的 MSXML 可能比其他版本更好地处理此问题,这可以解释为什么验证在某些计算机上有效而在其他计算机上被忽略,具体取决于安装的 MSXML 版本。

话虽这么说,您可能必须求助于文档中提到的第二种方法:

  • Add the XSD schema file to a schema cache and then connect that cache to the DOM document or SAX reader, prior to loading or parsing the XML document.

这需要直接使用 MSXML,您不能使用 TXMLDocument 来做到这一点:

MSXML also provides a means to connect and use a schema cache to store, load and connect a schema to an XML document, such as in the following VBScript code excerpt:

'Create the schema cache and add the XSD schema to it.
set oSC = CreateObject("MSXML2.XMLSchemaCache.6.0")
oSC.Add "urn:MyData", "http://www.example.com/MyData.xsd"
'Create the DOM document assign the cache to its schemas property.
set oXD = CreateObject("MSXML2.DOMDocument.6.0")
oXD.schemas = oSC
'Set properties, load and validate it in the XML DOM.

问题是您必须知道 XSD 的位置才能将其连接到解析器。因此,您必须加载 XML 一次才能提取 XSD 位置,然后将 XSD 加载到架构缓存中,然后重新加载附加了 XSD 的 XML。以下是一些 Delphi 示例:

schema validation with msxml in delphi

function TForm1.ValidXML2(const xmlFile: String;
out err: IXMLDOMParseError): Boolean;
var
xml, xml2, xsd: IXMLDOMDocument2;
schemas, cache: IXMLDOMSchemaCollection;
begin
xml := CoDOMDocument.Create;
if xml.load(xmlFile) then
begin
schemas := xml.namespaces;
if schemas.length > 0 then
begin
xsd := CoDOMDocument40.Create;
xsd.Async := False;
xsd.load(schemas.namespaceURI[0]);
cache := CoXMLSchemaCache40.Create;
cache.add(schemas.namespaceURI[1], xsd);
xml2 := CoDOMDocument40.Create;
xml2.async := False;
xml2.schemas := cache;
Result := xml2.load(xmlFile);
//err := xml.validate;
if not Result then
err := xml2.parseError
else
err := nil;
end;
end;
end;

How to validate a IXMLDocument against a XML Schema?

unit XMLValidate;

// Requirements ----------------------------------------------------------------
//
// MSXML 4.0 Service Pack 1
// http://www.microsoft.com/downloads/release.asp?releaseid=37176
//
// -----------------------------------------------------------------------------

interface

uses
SysUtils, XMLIntf, xmldom, XMLSchema;

type
EValidateXMLError = class(Exception)
private
FErrorCode: Integer;
FReason: string;
public
constructor Create(AErrorCode: Integer; const AReason: string);
property ErrorCode: Integer read FErrorCode;
property Reason: string read FReason;
end;

procedure ValidateXMLDoc(const Doc: IDOMDocument; const SchemaLocation, SchemaNS: WideString); overload;
procedure ValidateXMLDoc(const Doc: XMLIntf.IXMLDocument; const SchemaLocation, SchemaNS: WideString); overload;
procedure ValidateXMLDoc(const Doc: IDOMDocument; const Schema: IXMLSchemaDoc); overload;
procedure ValidateXMLDoc(const Doc: XMLIntf.IXMLDocument; const Schema: IXMLSchemaDoc); overload;

implementation

uses
Windows, ComObj, msxmldom, MSXML2_TLB;

resourcestring
RsValidateError = 'Validate XML Error (%.8x), Reason: %s';

{ EValidateXMLError }

constructor EValidateXMLError.Create(AErrorCode: Integer; const AReason: string);
begin
inherited CreateResFmt(@RsValidateError, [AErrorCode, AReason]);
FErrorCode := AErrorCode;
FReason := AReason;
end;

{ Utility routines }

function DOMToMSDom(const Doc: IDOMDocument): IXMLDOMDocument2;
begin
Result := ((Doc as IXMLDOMNodeRef).GetXMLDOMNode as IXMLDOMDocument2);
end;

function LoadMSDom(const FileName: WideString): IXMLDOMDocument2;
begin
Result := CoDOMDocument40.Create;
Result.async := False;
Result.resolveExternals := True; //False;
Result.validateOnParse := True;
Result.load(FileName);
end;

{ Validate }

procedure InternalValidateXMLDoc(const Doc: IDOMDocument; const SchemaDoc: IXMLDOMDocument2; const SchemaNS: WideString);
var
MsxmlDoc: IXMLDOMDocument2;
SchemaCache: IXMLDOMSchemaCollection;
Error: IXMLDOMParseError;
begin
MsxmlDoc := DOMToMSDom(Doc);
SchemaCache := CoXMLSchemaCache40.Create;
SchemaCache.add(SchemaNS, SchemaDoc);
MsxmlDoc.schemas := SchemaCache;
Error := MsxmlDoc.validate;
if Error.errorCode <> S_OK then
raise EValidateXMLError.Create(Error.errorCode, Error.reason);
end;

procedure ValidateXMLDoc(const Doc: IDOMDocument; const SchemaLocation, SchemaNS: WideString);
begin
InternalValidateXMLDoc(Doc, LoadMSDom(SchemaLocation), SchemaNS);
end;

procedure ValidateXMLDoc(const Doc: XMLIntf.IXMLDocument; const SchemaLocation, SchemaNS: WideString);
begin
InternalValidateXMLDoc(Doc.DOMDocument, LoadMSDom(SchemaLocation), SchemaNS);
end;

procedure ValidateXMLDoc(const Doc: IDOMDocument; const Schema: IXMLSchemaDoc);
begin
InternalValidateXMLDoc(Doc, DOMToMSDom(Schema.DOMDocument), '');
end;

procedure ValidateXMLDoc(const Doc: XMLIntf.IXMLDocument; const Schema: IXMLSchemaDoc);
begin
InternalValidateXMLDoc(Doc.DOMDocument, DOMToMSDom(Schema.DOMDocument), '');
end;

end.

Doc := LoadXMLData(XmlFileEdit.Lines.Text);
ValidateXMLDoc(Doc, FSchemaFileName, 'http://www.foo.com');

XML Documents, Schemas and Validation

var
XML, XSDL: Variant;
begin
XSDL := CreateOLEObject('MSXML2.XMLSchemaCache.4.0');
XSDL.validateOnLoad := True;
XSDL.add('','MySchema.xsd'); // 1st argument is target namespace
ShowMessage('Schema Loaded');
XML := CreateOLEObject('MSXML2.DOMDocument.4.0');
XML.validateOnParse := True;
XML.resolveExternals := True;
XML.schemas := XSDL;
XML.load('file.xml');
ShowMessage(XML.parseError.reason);
end.

关于delphi - 使用 Windows DOM 和 TXMLDocument 验证 XML : doesn't work on some computers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30654095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com