Delphi XML validation with XSD



我正在使用Delphi 10.2更新3。 我按照这些说明验证生成的 xml 文档。

属性 noNamespaceSchemaLocation 对 XML 解析有什么影响?

使用 Windows DOM 和 TXML 验证 XML:在某些计算机上不起作用

在德尔福中使用 MSXML 进行架构验证

但我有一个错误。元素'jegyzek_adatok'上的属性'noNamespaceSchemaLocation'未在DTD/Schema中定义。

准备 xml 文档:

const
cSchemaLocation = 'noNamespaceSchemaLocation';
procedure PreparePostBookXMLDocument(ARootNode: IXMLNode);
var
xDoc: IXMLDocument;
begin
if ARootNode.OwnerDocument = nil then Exit;
xDoc := ARootNode.OwnerDocument;
xDoc.Version := '1.0';
xDoc.Encoding := 'windows-1250';
xDoc.Options := xDoc.Options + [doNodeAutoIndent];
ARootNode.Attributes['xmlns:xsi'] := 'http://www.w3.org/2001/XMLSchema-instance';
ARootNode.Attributes['xsi:' + cSchemaLocation] := 'https://www.posta.hu/static/internet/download/level_ver8_ugyfeleknek_8p4.xsd';
end;

验证:

function ValidatePostBookXMLDocument(ARootNode: IXMLNode): IResult;
var
xDocument: IXMLDocument;
xMsxmlDoc: IXMLDOMDocument3;
xXSDDocument: IXMLDOMDocument3;
xSchemaCache: IXMLDOMSchemaCollection;
xSchemaLocation: string;
xError: IXMLDOMParseError;
begin
Result := ERRUnknown;
try
if ARootNode = nil then Exit;
xDocument := ARootNode.OwnerDocument;
if xDocument = nil then Exit;
xMsxmlDoc := ((xDocument.DOMDocument as IXMLDOMNodeRef).GetXMLDOMNode as IXMLDOMDocument3);
xSchemaLocation := ARootNode.AttributeNodes.FindNode(cSchemaLocation).Text;
xXSDDocument := CoDOMDocument60.Create;
xXSDDocument.async := False;
xXSDDocument.validateOnParse := True;
if not xXSDDocument.load(xSchemaLocation) then Exit(MakeErrorResult(ohFileError, 'A validációhoz szükséges séma fájlt nem sikerült betölteni!'));
xSchemaCache := CoXMLSchemaCache60.Create;
xSchemaCache.add('', xXSDDocument);
xMsxmlDoc.schemas := xSchemaCache;
xError := xMsxmlDoc.validate;
case xError.errorCode of
S_OK: Result := Success;
else Exit(MakeErrorResult(ohError, xError.reason));
end;
except
on E:Exception do Result := HandleException;
end;
end;

生成的 xml 文件通过 https://www.freeformatter.com/xml-validator-xsd.html# 有效。

XSD (https://www.posta.hu/static/internet/download/level_ver8_ugyfeleknek_8p4.xsd(:

我生成的 xml(在我的谷歌驱动器上(:

有人可以帮助我吗?

我不知道你在Delphi中使用的特定XML解析器。但是,要回答一般问题:

  • 属性xsi:noNamespaceSchemaLocation声明在何处查找文档的 XSD 架构(具体而言,是无命名空间中元素的架构(

  • 除非调用 XSD 架构验证,否则它不起作用。某些解析器可能会将此属性的存在解释为调用架构验证的信号,但这相当不寻常。

  • 针对 XSD 架构进行验证时,此属性始终有效,前提是其值是有效的 URI。架构不需要显式允许此属性。

  • 针对 DTD 进行验证时,此属性无效,除非编写 DTD 以明确允许它。

我怀疑您正在运行启用了 DTD 验证的解析器,并且 DTD 不允许存在此属性。但这有点猜测。

最新更新