Next / Previous / Contents / Shipman's homepage

3.3. getChildContent(): Get text from a child node

This function returns the text content of a node's child.

txny.py
# - - -   g e t C h i l d C o n t e n t

def getChildContent ( node, childName ):
    """Get the text from a child node.

      [ (node is an et.Element) and
        (childName is a string) ->
          if node has any children named childName ->
            return the concatenation of the first such child
            node's direct text content
          else -> return "" ]
    """
    childList  =  node.xpath ( childName )
    if  len(childList) == 0:
        return ""
    else:
        return  textContent ( childList[0] )