ChildElementNS - cXMLDOMDocumentFragment

Returns the object handle of the first child element node whose NamespaceURI and BaseName matches the passed values

Type: Function

Return Data Type: Handle

Parameters: string sNameSpaceURI string sBaseName

ParameterDescription
sNamespaceURINamespace-URI string (e.g. http://dataaccess.com/MyURI)
sBaseNameName of Element excluding the prefix


Return Value

Returns the object handle of the first child element node whose NamespaceURI and BaseName matches the passed values. Returns zero if no node is found.


Syntax
Function ChildElementNS string sNameSpaceURI string sBaseName Returns Handle

Call: Get ChildElementNS sNameSpaceURI sBaseName to HandleVariable


Description

ChildElementNS is used to find a child element with a passed NamespaceURI and BaseName. It returns the object Id of first node that matches or zero if no node is found. As with any XML object node, you must make sure you destroy the node when you are done with it.

If the node is a simple text only element you just need to get the node's text value you can use the ChildElementValueNS message.

Assume we have the following XML document:

<m:Customer xmlns:m="http://www.dataaccess.com/Test/CustomerList">
    <m:Name cnum="12">3A Software</m:Name>
    <m:State>CA</m:State>
</m:Customer>

The NamespaceURI for all of these elements is "http://www.dataaccess.com/Test/CustomerList". As we parse this, notice that we ignore the prefix name ("m:"). We are only interested in the NamespaceURI and the BaseName. We could search for elements as follows:

Move "http://www.dataaccess.com/Test/CustomerList" to sNS // the namespace
Get DocumentElement of hoXML to hoRoot

// we need to get the element value and an attribute value from "Name". So we get its node
Get ChildElementNS of  hoRoot sNS "Name" to hoName
Get psText of hoName to sName
Get AttributeValueNS of hoName "" "cnum" to sNumber
Send Destroy of hoName

// State is a simple element. We can get the text an easier way
Get ChildElementValueNS of  hoRoot sNS "State" to sState

Send ShowInfo sName sNumber sState

You can also use this message along with NextElementNS to traverse a list of child objects. Assume you have the following xml document:

<CustomerList xmlns="http://www.dataaccess.com/Test/CustomerList">
    <Customer>
        <Name>3A Software</Name>
        <Number>13</Number>
        <State>CA</State>
    </Customer>
    <Customer>
        <Name>Ace Manufacturers, Inc.</Name>
        <Number>4</Number>
        <State>IL</State>
    </Customer>
    <Customer>
        <Name>All Canada Brewing Company</Name>
        <Number>24</Number>
        <State>CA</State>
    </Customer>
</CustomerList>

Here the NamespaceURI is "http://www.dataaccess.com/Test/CustomerList". In this case, all elements are part of the default namespace (i.e., no prefixes). Once again, we ignore the prefixes and rely on the NamespaceURI and BaseName. You could parse all customers as follows:

Move "http://www.dataaccess.com/Test/CustomerList" to sNS // the namespace
Get DocumentElement of hoXML to hoRoot // this is CustomerList
Get ChildElementNS of hoRoot sNS "Customer" to hoCust
While hoCust
    Get ChildElementValueNS of  hoCust sNS "Name" to sName
    Get ChildElementValueNS of  hoCust sNS "Number" to sNumber 
    Get ChildElementValueNS of  hoCust sNS "State" to sState
    Send DoThisCustomer sName sNumber sState
    Get NextElementNS of hoCust sNS "Customer" to hoCust
end

Elements with no NamespaceURI

If your document does not have any namespaces or an element within the document does not have a namespace, its namespace is considered to be global and its NamespaceURI is represented as an empty string (""). In such a case you can and should use these namespace aware messages passing an empty string for the NameSpaceURI.

Note:When parsing XML documents with namespaces you should always search by NamespaceURI and BaseName. You should not use the element's prefix and you should not attempt to ignore the NamespaceURI.

See Also

ChildElementValueNS | NextElementNS | IsElementNS | FirstChild | NextNode