xmlChildPos

Description

Gets the position of a child element within an XML document object.

Categories

Related

History

ColdFusion MX: Added this function.

Syntax

XmlChildPos(elem, childName, N) 

Attributes

AttributeDescriptionRequiredDefault
childNameXML child element for which to search. Must be an immediate child of the elem parameter.
elemXML element within which to search.
NIndex of XMLchild element for which to search.

Returns

The position, in an XmlChildren array, of the Nth child that has the specified name.

Usage

You can use the returned index in the ArrayInsertAt and ArrayDeleteAt functions to change XML document objects. If the specified child is not found, the function returns -1.

Example

The following example searches XML document element, xmlobject.employee.name[1], for its second Status element child and uses the position in an ArrayDeleteAt function to remove the element:
<!--- Create an XML document object ---> 
<cfxml variable="xmlobject"> 
<employee> 
    <!-- A list of employees --> 
    <name EmpType="Regular"> 
        <first>Almanzo</first> 
        <last>Wilder</last> 
        <Status>Medical Absence</Status> 
        <Status>Extended Leave</Status> 
    </name> 
    <name EmpType="Contract"> 
        <first>Laura</first> 
        <last>Ingalls</last> 
    </name> 
</employee> 
</cfxml> 
 
<!--- Find the second Status child of the first employee.name element ---> 
<cfscript> 
elempos=XMLChildPos(xmlobject.employee.name[1], "Status", 2); 
ArrayDeleteAt(xmlobject.employee.name[1].XmlChildren, elempos); 
</cfscript> 
 
<!--- Dump the resulting document object to confirm the deletion ---> 
<cfdump var="#xmlobject#">