我有一个XML文档,其中包含带有名称空间的元素。我想根据某个名称空间和某个元素从该XML中选择节点。在使用MSXML Vendor时,您可以使用SetProperty('SelectionNameSpaces','nn:mmmm')语句完成此操作。但是因为我们当前的项目将是多平台的,所以我不能使用MSXML供应商。我正在尝试OmniXML供应商,但我找不到如何在SelectNodes()语句中使用名称空间。
在下面的代码中,我尝试使用DeclareNameSpace(),但不起作用。SelectNodes语句找不到任何节点,列表保持为空。
我该如何解决这个问题呢?
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
XML.XMLDom,
XML.XMLDoc,
XML.omnixmldom,
XML.XMLIntf
;
const
cXML = '<?xml version="1.0"?>' +
'<catalog xmlns:xs12=''http://www.w3.org/2001/XMLSchema-instance''>' +
' <xs12:book id="bk101">' +
' <xs12:author>Gambardella, Matthew</xs12:author>' +
' <xs12:title>XML Developers Guide</xs12:title>' +
' <xs12:genre>Computer</xs12:genre>' +
' <xs12:price>44.95</xs12:price>' +
' <xs12:publish_date>2000-10-01</xs12:publish_date>' +
' <xs12:description>An in-depth look at creating applications with XML.</xs12:description>' +
'</xs12:book>'
+ '</catalog>'
;
var
lDoc: IXMLDocument;
lList: IDOMNodeList;
lSelectNode: IdomNodeSelect;
begin
DefaultDOMVendor := sOmniXmlVendor;
try
try
lDoc := LoadXMLData(cXML);
lDoc.DocumentElement.DeclareNamespace('a', 'http://www.w3.org/2001/XMLSchema-instance');
if supports(lDoc.DOMDocument, IDomNodeSelect, lSelectNode) then
begin
lList := lSelectNode.selectNodes('a:book');
Writeln(Format( '%d nodes', [lList.length]));
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
finally
end;
end.发布于 2016-03-09 16:29:18
看起来OmniXML并不支持这一点。我在这个主题上找到的所有帖子都没有提供这个问题的答案。
我确实设法通过使用另一个XML实现解决了这个问题: OXML这个实现有一个SelectNodesNS()函数,它恰好能做我想要的事情。它可以通过subversion获得。更多信息请点击此处:http://www.kluug.net/oxml.php
使用OXML的示例项目:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
OXMLPDOM
;
const
cXML = '<?xml version="1.0"?>' +
'<catalog xmlns:xs12=''http://www.w3.org/2001/XMLSchema-instance''>' +
' <xs12:book id="bk101">' +
' <xs12:author>Gambardella, Matthew</xs12:author>' +
' <xs12:title>XML Developers Guide</xs12:title>' +
' <xs12:genre>Computer</xs12:genre>' +
' <xs12:price>44.95</xs12:price>' +
' <xs12:publish_date>2000-10-01</xs12:publish_date>' +
' <xs12:description>An in-depth look at creating applications with XML.</xs12:description>' +
'</xs12:book>'
+ '</catalog>'
;
var
doc: IXMLDocument;
list: IXMLNodeList;
begin
try
try
doc := CreateXMLDoc;
Doc.LoadFromXML(cXML);
doc.DocumentElement.SelectNodesNS('http://www.w3.org/2001/XMLSchema-instance', 'book', list);
Writeln(Format( '%d nodes', [List.count]));
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
finally
end;
end.https://stackoverflow.com/questions/35885223
复制相似问题