1
votes

I'm using Delphi 10.2 update 3. I followed these instructions to validate a generated xml document.

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

But I have an error. "The attribute 'noNamespaceSchemaLocation' on the element 'jegyzek_adatok' is not defined in the DTD/Schema."

Preparing the xml document:

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;

The validation:

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;

The generated xml file, is valid via 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):

Can somebody help me?

1

1 Answers

1
votes

I don't know the specific XML parser you're using in Delphi. However, to answer the general questions:

  • the attribute xsi:noNamespaceSchemaLocation declares where to find an XSD schema for the document (specifically, a schema for elements in no namespace)

  • it has no effect unless you invoke XSD schema validation. Some parsers may interpret the presence of this attribute as a signal to invoke schema validation, but that's fairly unusual.

  • when validating against an XSD schema, this attribute is always valid provided that its value is a valid URI. The schema doesn't need to explicitly permit this attribute.

  • when validating against a DTD, this attribute is INVALID unless the DTD is written to explicitly allow it.

I suspect that you are running the parser with DTD validation enabled and that the DTD does not permit this attribute to be present. But that's a bit of a guess.