What happens to these XML nodes called "#text"?

I have a simple XML processing code that should find the child element of a node passed in node based on the attribute value:

function GetNodeByAttributeValue(
  const AParentNode: IXMLNode;
  const AttributeName: string; AttributeValue: Variant): IXMLNode;
var
  i: integer;
  value: Variant;
begin
  result := nil;
  if (not Assigned(AParentNode)) or (AttributeName = '') then
    exit;
  for i := 0 to AParentNode.ChildrenCount-1 do
  begin
    result := AParentNode.Children[i];
    value := result.GetAttributeValue(AttributeName, UnAssigned);
    if not VarIsEmpty(value) then
      exit;
  end;
  result := nil;
end;

Pretty simple, right? But when I try to run this, under certain circumstances it crashes with access violation. Here's what happens:

The IXML * implementation is provided by the RemObjects SDK. result.GetAttributeValuecalls uROMSXMLImpl.TROMSXMLNode.GetAttributeValuethat calls TROMSXMLNode.GetAttributeByNamewhich says

  node := fNode.attributes.getNamedItem(anAttributeName);

And this is a failure because it fNode.attributesreturns nil . As I understand it, this should never be.

, for , AParentNode.ChildrenCount 3. node XML node. , .

<ParentNode>
  <namespace:ChildNode name="right-name">

AParentNode.ChildrenCount 3. :

AParentNode.Children[0].name: '#text'
AParentNode.Children[1].name: 'namespace:ChildNode'
AParentNode.Children[2].name: '#text'

"#text" ? XML, . , , -, , , ?

+3
3

- , .
<namespace:ChildNode name="right-name">

<ParentNode>

+7

. , ( ) - , node, , , . , XML : <?some wired stuff?>, , AV . NodeType :

function GetNodeByAttributeValue(
  const AParentNode: IXMLNode;
  const AttributeName: string; AttributeValue: Variant): IXMLNode;
var
  i: integer;
  value: Variant;
begin
  result := nil;
  if (not Assigned(AParentNode)) or (AttributeName = '') then
    exit;
  for i := 0 to AParentNode.ChildrenCount-1 do
  begin
    result := AParentNode.Children[i];
    if result.NodeType = ntElement then
    begin
      value := Result.GetAttributeValue(AttributeName, UnAssigned);
      if not VarIsEmpty(value) and (value = AttributeValue) then
        exit;
    end;
  end;
  result := nil;
end;

, , XSLT / XPath, , XPath , XSLT .

+2

#Text nodes are bits of spaces before and after <namespace:ChildNode>. Since #text nodes are just bits of text, they have no attributes. If you want to get rid of these nodes, try using xsl: strip-space in the XSL transform, or just check to see if the node consists solely of spaces.

+1
source

Source: https://habr.com/ru/post/1755834/


All Articles