Bob Swart の "Delphi XE2 XML, SOAP & Web Services Development" bookのドキュメントに基づいて、XML ドキュメントで XSL 変換を行っています。
フォームに TXSLPageProducer を使用すると、これは正常に機能します。
procedure TFrmRemoveNamespaces.Button1Click(Sender: TObject);
const
cRemoveNSTransform =
'<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">' +
'<xsl:output method="xml" indent="no"/>' +
'<xsl:template match="/|comment()|processing-instruction()">' +
' <xsl:copy>' +
' <xsl:apply-templates/>' +
' </xsl:copy>' +
'</xsl:template>' +
'<xsl:template match="*">' +
' <xsl:element name="{local-name()}">' +
' <xsl:apply-templates select="@*|node()"/>' +
' </xsl:element>' +
'</xsl:template>' +
'<xsl:template match="@*">' +
' <xsl:attribute name="{local-name()}">' +
' <xsl:value-of select="."/>' +
' </xsl:attribute>' +
'</xsl:template>' +
'</xsl:stylesheet>';
// From http://wiki.tei-c.org/index.php/Remove-Namespaces.xsl
// This is a quick XSLT script for removing the namespaces from any document. It will remove the prefix as well.
var
SS: TStringStream;
TS: TStringList;
XMLDoc: TXMLDocument;
XSLPP: TXSLPageProducer;
begin
XMLDoc := TXMLDocument.Create(self);
XMLDoc.active := false;
MmoAfter.Clear;
SS := TStringStream.Create('',TEncoding.UTF8);
SS.Position := 0;
MmoBefore.Lines.SaveToStream(SS);
SS.Position := 0;
XMLDoc.LoadFromStream(SS);
SS.Free;
TS := TStringList.Create;
TS.Text := cRemoveNSTransform;
XSLPP := TXSLPageProducer.Create(self);
XSLPP.XML := TS;
XSLPP.XMLData := XMLDoc;
XMLDoc.active := true;
MmoAfter.Text := XSLPP.Content;
TS.Free;
XMLDoc.Free;
XSLPP.Free;
end;
ただし、コードを別のユニットのクラス ヘルパー関数に移動すると、「Result := XSLPP.Content」で無効なポインター操作が発生します。
呼び出しコードは単純に「MmoAfter.Text := TXMLHelper.RemoveNameSpaces(MmoBefore.Text);」です。このコードを使用して、クラス ヘルパー ユニットを作成します。
class function TXMLHelper.RemoveNameSpaces(XMLString: String): String;
const
// An XSLT script for removing the namespaces from any document. It will remove the prefix as well.
// From http://wiki.tei-c.org/index.php/Remove-Namespaces.xsl
cRemoveNSTransform =
'<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">' +
'<xsl:output method="xml" indent="no"/>' +
'<xsl:template match="/|comment()|processing-instruction()">' +
' <xsl:copy>' +
' <xsl:apply-templates/>' +
' </xsl:copy>' +
'</xsl:template>' +
'<xsl:template match="*">' +
' <xsl:element name="{local-name()}">' +
' <xsl:apply-templates select="@*|node()"/>' +
' </xsl:element>' +
'</xsl:template>' +
'<xsl:template match="@*">' +
' <xsl:attribute name="{local-name()}">' +
' <xsl:value-of select="."/>' +
' </xsl:attribute>' +
'</xsl:template>' +
'</xsl:stylesheet>';
var
SS: TStringStream;
TS: TStringList;
XSLPP: TXSLPageProducer;
XMLDoc : TXMLDocument;
begin
XMLDoc := TXMLDocument.Create(nil);
// XMLDoc.active := true;
SS := TStringStream.Create(XMLString,TEncoding.UTF8);
SS.Position := 0;
XMLDoc.LoadFromStream(SS);
SS.Free;
TS := TStringList.Create;
TS.Text := cRemoveNSTransform;
XSLPP := TXSLPageProducer.Create(nil);
XSLPP.DOMVendor := GetDOMVendor('MSXML');
XSLPP.XML := TS;
XSLPP.XMLData := XMLDoc;
// XSLPP.Active := true;
Result := XSLPP.Content;
TS.Free;
XMLDoc.Free;
XSLPP.Free;
end;
ご覧のとおり、DOMVendor を設定する必要があり、「Create(nil)」は異なります。
その最後の違いが理由でしょうか? またその理由は?