使用XSD的Delphi XML验证

问题描述 投票:0回答:1

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

What effect does the attribute noNamespaceSchemaLocation have on XML parsing?

Validate XML using Windows DOM and TXMLDocument: doesn't work on some computers

schema validation with msxml in delphi

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

准备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#有效。

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

My generated xml (on my google drive):

有人可以帮我吗?

xml delphi xsd
1个回答
0
投票

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

  • 属性xsi:noNamespaceSchemaLocation声明在哪里可以找到文档的XSD模式(特别是,没有名称空间的元素的模式)

  • 除非您调用XSD模式验证,否则它无效。一些解析器可能将此属性的存在解释为调用模式验证的信号,但这是非常不寻常的。

  • 在针对XSD架构进行验证时,如果该属性的值是有效的URI,则该属性始终有效。模式不需要显式允许此属性。

  • 当针对DTD进行验证时,除非编写DTD以明确允许它,否则此属性为INVALID。

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

© www.soinside.com 2019 - 2024. All rights reserved.