delphi xmlchildnode从父级获取属性 [英] delphi xmlchildnode gets attribute from parent

查看:98
本文介绍了delphi xmlchildnode从父级获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Delphi编写xML。

I am trying to write an xML in Delphi.

如果我给一个节点设置了xmlns属性,则该节点的子节点也会显示该属性,但随后为空。

If I gife a node a xmlns attribute, the child node of that node shows the attribute also but then empty. How can I prefent that the child node shows the attribute?

我用以下代码测试了

procedure TForm2.Button1Click(Sender: TObject);
var
  RootNode, CurNode, PmtNode, PmtDetNode : IXMLNODE;
  I:Integer;
begin
   SepaDoc := Newxmldocument;
   SepaDoc.Encoding := 'utf-8';
   SepaDoc.Options := [doNodeAutoIndent];
   RootNode := SepaDoc.AddChild('Document');
   CurNode := RootNode.AddChild('Child1');
   CurNode.Attributes['xmlns'] := 'apenootje';
   CurNode := CurNode.AddChild('Child2');
   CurNode := CurNode.AddChild('Child3');
   SepaDoc.SaveToFile('D:\indir\testsepa.xml');
end;

结果是以下XML

<?xml version="1.0" encoding="UTF-8"?>
-<Document>   -<Child1 xmlns="apenootje">
    -<Child2 xmlns="">
       <Child3/>
     </Child2>
    </Child1>    
  </Document>

感谢
Rob Nowee

Thanks Rob Nowee

推荐答案

由于Child1的子元素没有相同的名称空间,因此必须未声明它,这就是Child2保留空(默认)名称空间的原因。

Since the child elements of Child1 don't carry the same namespace, it must be undeclared and that's the reason that Child2 is holding the empty (default) namespace.

这称为命名空间未声明


当元素带有属性xmlns =时,该元素及其后代的默认名称空间将恢复为 no名称空间:即,不带前缀的名称被认为是在任何名称空间中。

When an element carries the attribute xmlns="", the default namespace for that element and its descendants reverts to "no namespace": that is, unprefixed names are considered not to be in any namespace.

XML名称空间1.1还引入了取消声明其他名称空间前缀的选项。例如,如果属性xmlns:p =出现在元素上时,除非该命名空间前缀p不再由另一个命名空间声明再次引入

XML Namespaces 1.1 also introduces the option to undeclare other namespace prefixes. For example, if the attribute xmlns:p="" appears on an element, the namespace prefix p is no longer in scope (and therefore cannot be used) on that element or on its descendants, unless reintroduced by another namespace declaration

也就是说,解决方法很简单;包括所有后续子节点上的命名空间:

That being said, the fix is simple; include the namespace on all subsequent child nodes:

program SO20424534;

{$APPTYPE CONSOLE}

uses
  ActiveX,
  XMLdom,
  XMLDoc,
  XMLIntf,
  SysUtils;

function TestXML : String;

var
  RootNode,
  CurNode    : IXMLNODE;
  Doc        : IXmlDocument;
  ns         : String;

begin
 Doc := Newxmldocument;
 ns := 'apenootje';
 Doc.Encoding := 'utf-8';
 Doc.Options := [doNodeAutoIndent];
 RootNode := Doc.AddChild('Document');
 CurNode := RootNode.AddChild('Child1');
 CurNode.DeclareNamespace('', ns);
 CurNode := CurNode.AddChild('Child2', ns);
 CurNode := CurNode.AddChild('Child3', ns);
 Result := Doc.XML.Text;
end;
    
begin
  try
   CoInitialize(nil);
   try 
    Writeln(TestXML);
   finally
    CoUninitialize;
   end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
 Readln;
end;

输出:

<?xml version="1.0"?>
<Document>
  <Child1 xmlns="apenootje">
    <Child2>
      <Child3/>
    </Child2>
  </Child1>
</Document>

这篇关于delphi xmlchildnode从父级获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆