2

我想用XSLT 3.0 及其 json-to-xml() 函数进行试验(在 Delphi 代码中) :

在 XSLT 3.0 中,入站文档可以是 JSON,而不是 XML。处理器可以获取该文档,使用 json-to-xml() 函数将其转换为特定的已知 XML 格式,通过模板对其进行处理,然后将结果输出转换回 JSON(或者可以将其转换为 HTML 5其他格式

但我被困在两个地方:

  • 如何使用 JSON 字符串作为转换源?试图将它加载到一个TXMLDocument给我(当然?)“格式错误”的错误

  • 然后我将如何应用“json-to-xml()功能”。我发现的所有关于在 Delphi 中使用 XSLT 转换的示例都使用了 TransformNode 函数,如下面的代码。lDoc.Node.json-to-xml不编译之类的东西。

.

var
  lDoc, lXSL, lRes: IXMLDocument;
  lUTF8Str        : UTF8String;
begin
  lDoc := LoadXMLData(AXMLString);
  lXSL := LoadXMLData(cRemoveNSTransform);
  lRes := NewXMLDocument;
  lDoc.Node.TransformNode(lXSL.Node,lRes);  // Param types IXMLNode, IXMLDocument
  lRes.SaveToXML(lUTF8Str);     

谁能指出我正确的方向?

4

1 回答 1

1

我将为我自己的问题编写一个“指南”,该问题不使用 XSLT,而是使用IP*Works!我们订阅的Delphi 组件。

这至少可以给其他人一个可用的选项,或者一个粗略的想法如何“自己动手”。

我们使用 IP*Works!TipwJSONTipwXML组件。诀窍是拦截JSON组件的解析,然后将检测到的数据写入XML组件。这是来自测试应用程序的代码,显示了我们是如何做到的(我已经留下了登录代码):

TJSONTOXML = class(TIpwJSON)
            private
               FXML         : TipwXML;
               FLogLevel    : Integer;
               procedure ShowLogLine(AMsg: String);
               procedure InterceptJSONStartElement(Sender: TObject; const Element: string);
               procedure InterceptJSONEndElement(Sender: TObject; const Element: string);
               procedure InterceptCharacters(Sender: TObject; const Text: string);
               function GetXML: String;
            public
               property XML: String read GetXML;
               constructor Create(AOwner: TForm; ALogLevel: Integer); overload;  // For now testing on a Form
            end;

constructor TJSONTOXML.Create(AOwner: TForm; ALogLevel: Integer);
begin
   inherited Create(AOwner);
   FLogLevel := ALogLevel;
   Self.BuildDOM  := false;
   Self.OnStartElement := InterceptJSONStartElement;
   Self.OnEndElement   := InterceptJSONEndElement;
   Self.OnCharacters   := InterceptCharacters;
   FXML := TipwXML.Create(nil);
end;

procedure TJSONTOXML.InterceptJSONEndElement(Sender: TObject; const Element: string);
begin
   if Element = '' then  // End of array
   begin
      if FLogLevel > 2 then ShowLogLine('JSON parse EndElement - Array');
      FXML.EndElement;
   end
   else
   begin
      if FLogLevel > 2 then ShowLogLine('JSON parse EndElement - Element: ' + Element);
      FXML.EndElement;
   end;
end;

procedure TJSONTOXML.InterceptJSONStartElement(Sender: TObject; const Element: string);
begin
   if Element = '' then  // Start of array
   begin
      if FLogLevel > 2 then ShowLogLine('JSON parse StartElement - Array');
      FXML.StartElement('ARRAY','');
   end
   else
   begin
      if FLogLevel > 2 then ShowLogLine('JSON parse StartElement - Element: ' + Element);
      FXML.StartElement(Uppercase(Element),'');
   end;
end;

procedure TJSONTOXML.ShowLogLine(AMsg: String);
// Use WM_COPYDATA to send log info to form
var CopyDataStruct: TCopyDataStruct;
begin
  CopyDataStruct.dwData := 0;
  CopyDataStruct.cbData := 2 + 2 * Length(AMsg);
  CopyDataStruct.lpData := PChar(AMsg);
  SendMessage((Owner as TForm).Handle, WM_COPYDATA, (Owner as TForm).Handle, lParam(@CopyDataStruct));
end;

function TJSONTOXML.GetXML: String;
begin
   FXML.EndElement;
   Result := FXML.OutputData;
end;

procedure TJSONTOXML.InterceptCharacters(Sender: TObject; const Text: string);
var lText: String;
begin
   // Always surrounded by quotes, remove:
   lText := StripQuotes(Text);
   if FLogLevel > 2 then ShowLogLine('JSON parse characters: ' + lText);
   FXML.PutString(lText);
end;

有了这个你可以

lJSONToXML := TJSONTOXML.Create(Self,FDataLogLvl);
// Get your JSON data from somewhere, e.g. a HTTP component. Then:
lJSONToXML.Inputdata := lData;
lJSONToXML.Parse;         // The Parse method initiates the parsing that was postponed by setting BuildDom := false
// The XML is now in the OutputData property of the TipwXML and can e.g. be retrieved by our:
lOutputData := lJSONToXML.XML;

注意:

  • XML 中没有命名空间信息
  • 转换为 XML 时的 JSON 数组将转换为名为 ARRAY 的节点
  • 所有数据都保存在内存中
于 2019-07-01T14:30:49.293 回答