Hi,
Im using xmldom.
for child in document.getElementsByTagName "div":
echo child.getAttribute "id"
Its work OK. But, If I create custom function as
proc getAttributeOrDefault(node: PElement, name: string, default: string): string =
if node.hasAttribute name:
return node.getAttribute name
return default
for child in document.getElementsByTagName "div":
echo child.getAttributeOrDefault "id", "my value"
Compile with error:
proc getAttributeOrDefault(node: PElement; name: string; default: string): string
first type mismatch at position: 1
required type: PElement
but expression 'child' is of type: PNode
Why I can using it?
thanks Petr
for child in document.getElementsByTagName "div":
echo child.PElement.getAttributeOrDefault("id", "my value")
getElementsByTagName returns seq od PNode, which is the superclass for PElement, i.e. for compiler not every PNode is PElement, so you can just call PElement's procs on it. But really they all should be PElement, so you can do that conversion there.
Pay attention yet, those parentheses are needed, otherwise echo gets the 2nd argument.