简体   繁体   English

使用TXMLDocument的XSLT转换

[英]XSLT transform using TXMLDocument

I was trying to use TXMLDocument for XSLT transforming and found unexplained (for me) behavior - TXMLDocument.transformNode does't process the '/' pattern (matched with root node https://msdn.microsoft.com/en-us/library/ms256113(v=vs.85).aspx ). 我试图将TXMLDocument用于XSLT转换,并且发现了无法解释的行为(对我而言)-TXMLDocument.transformNode不处理“ /”模式(与根节点https://msdn.microsoft.com/zh-cn/library匹配) /ms256113(v=vs.85).aspx )。

XML: XML:

<notelist>
  <note>NoteText</note>
</notelist>

Xslt: XSLT:

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml"  indent="yes" encoding="utf-8" />

  <xsl:template match="/">
    <TransformedNoteList>
      <xsl:apply-templates/>  
    </TransformedNoteList>
  </xsl:template>

  <xsl:template match="text()" /> 
</xsl:stylesheet>

Using TXMLDocument Delphi code: 使用TXMLDocument Delphi代码:

var
  XML: IXMLDocument;
  XSL: IXMLDocument;
  Output: XmlDomString;
begin
  ResultMemo.Clear;

  XML := LoadXMLData(XMLMemo.Text);
  XSL := LoadXMLData(XsltMemo.Text);

  XML.DocumentElement.TransformNode(XSL.DocumentElement, Output);

  ResultMemo.Text := Output;

I expected some thing like this as result: 我期望这样的结果:

<?xml version="1.0"?>
<TransformedNoteList></TransformedNoteList>

but got empty xml file. 但是得到了空的xml文件。

I tried to reproduce this behavior via using IXslProcessor and all works fine. 我试图通过使用IXslProcessor重现此行为,并且一切正常。 Can somebody explain me what is using TXMLDocument for transforming because as is understood in not working via IXslProccessor? 有人可以解释一下为什么使用TXMLDocument进行转换,因为不能通过IXslProccessor工作可以理解吗?

Using IXSLProcessor: 使用IXSLProcessor:

var
  XMLDoc, XSLStylesheet: IXMLDOMDocument;
  XSLProcessor: IXSLProcessor;
  XSLTemplate: IXSLTemplate;
begin
  ResultMemo.Clear;

  XMLDoc := CoDOMDocument60.Create;
  XMLDoc.LoadXML(XMLMemo.Text);

  XSLStylesheet := CoDOMDocument60.Create;
  XSLStylesheet.LoadXML(XsltMemo.Text);

  XSLTemplate := CoXSLTemplate60.Create;
  XSLTemplate._Set_stylesheet(XSLStylesheet);
  XSLProcessor := XSLTemplate.createprocessor;
  XSLProcessor.Input := XMLDoc;

  if XSLProcessor.Transform then
    ResultMemo.Text := XSLProcessor.Output;
end;

And what is the correct solution for XSLT transformation? XSLT转换的正确解决方案是什么?

As Martin says, match="/" matches the top node in the tree (DOM and XPath 2.0 call this a document node, XPath 1.0 calls it a root node). 正如Martin所说, match="/"匹配树中的顶部节点(DOM和XPath 2.0将其称为文档节点,XPath 1.0将其称为根节点)。 But you have started the transformation by supplying the outermost element in the tree (DOM calls this the document element). 但是,您已经通过在树中提供了最外层的元素(DOM将其称为文档元素)来开始了转换。 To match the outermost element you need the pattern match="/*" . 要匹配最外面的元素,您需要使用模式match="/*"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM