Steve H.
Steve H.

Reputation: 350

Retrieve XML nodes by Element Value with Javascript or JQuery

I am working on an issue in which I need to filter an XML document by a specific attributes value and retrieve all the nodes that match. However, I am not getting the desired result.

This is the jQuery that I am using to traverse two separate XML Documents. Right now I am hardcoding a definitionId for simplicity sake. I then need to filter the instanceData XML document by the definitionId and return all the nodes that match that criteria.

$(definitionData).find("Entity").each(function (index) {
    var definitionId ="b1df0728-d761-e311-84c5-6c3be5a8bcd8";

    var instanceXmlRecords = $cm(instanceData).find("Entity").each(function () {
    if ($(this).find("Id").text() == definitionId) {
        return this;
    }
});
}

Sample XML

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body>
<ExecuteResponse xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services">
    <ExecuteResult i:type="a:RetrieveMultipleResponse" xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:ResponseName>RetrieveMultiple</a:ResponseName>
        <a:Results xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
            <a:KeyValuePairOfstringanyType>
                <b:key>EntityCollection</b:key>
                <b:value i:type="a:EntityCollection">
                    <a:Entities>
                        <a:Entity>
                            <a:Attributes>
                                <a:KeyValuePairOfstringanyType>
                                    <b:key>cm_completedon</b:key>
                                    <b:value i:type="c:dateTime" xmlns:c="http://www.w3.org/2001/XMLSchema">2013-12-10T20:18:35Z</b:value>
                                </a:KeyValuePairOfstringanyType>
                                <a:KeyValuePairOfstringanyType>
                                    <b:key>cm_nurturetrackinstanceid</b:key>
                                    <b:value i:type="c:guid" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/">b1df0728-d761-e311-84c5-6c3be5a8bcd8</b:value>
                                </a:KeyValuePairOfstringanyType>
                                <a:KeyValuePairOfstringanyType>
                                    <b:key>cm_startedon</b:key>
                                    <b:value i:type="c:dateTime" xmlns:c="http://www.w3.org/2001/XMLSchema">2013-12-10T20:10:49Z</b:value>
                                </a:KeyValuePairOfstringanyType>
                                <a:KeyValuePairOfstringanyType>
                                    <b:key>cm_progress</b:key>
                                    <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">
                                        [AddToMarketingList]=Completed; &#xD;
                                        [SendNotificationEmail]=Completed; &#xD;
                                    </b:value>
                                </a:KeyValuePairOfstringanyType>
                                <a:KeyValuePairOfstringanyType>
                                    <b:key>statuscode</b:key>
                                    <b:value i:type="a:OptionSetValue">
                                        <a:Value>4</a:Value>
                                    </b:value>
                                </a:KeyValuePairOfstringanyType>
                                <a:KeyValuePairOfstringanyType>
                                    <b:key>cm_parent_nurturetrackdefinitionid</b:key>
                                    <b:value i:type="a:EntityReference">
                                        <a:Id>b52b7b97-945e-e311-bb8e-6c3be5a8a238</a:Id>
                                        <a:LogicalName>cm_nurturetrackdefinition</a:LogicalName>
                                        <a:Name>Test Recurring 001</a:Name>
                                    </b:value>
                                </a:KeyValuePairOfstringanyType>
                                <a:KeyValuePairOfstringanyType>
                                    <b:key>cm_name</b:key>
                                    <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">12/10/2013 8:10:47 PM</b:value>
                                </a:KeyValuePairOfstringanyType>
                            </a:Attributes>
                            <a:EntityState i:nil="true"/>
                            <a:FormattedValues>
                                <a:KeyValuePairOfstringstring>
                                    <b:key>cm_completedon</b:key>
                                    <b:value>12/10/2013 3:18 PM</b:value>
                                </a:KeyValuePairOfstringstring>
                                <a:KeyValuePairOfstringstring>
                                    <b:key>cm_startedon</b:key>
                                    <b:value>12/10/2013 3:10 PM</b:value>
                                </a:KeyValuePairOfstringstring>
                                <a:KeyValuePairOfstringstring>
                                    <b:key>statuscode</b:key>
                                    <b:value>Completed</b:value>
                                </a:KeyValuePairOfstringstring>
                            </a:FormattedValues>
                            <a:Id>b1df0728-d761-e311-84c5-6c3be5a8bcd8</a:Id>
                            <a:LogicalName>cm_nurturetrackinstance</a:LogicalName>
                            <a:RelatedEntities/>
                        </a:Entity>
                        <a:Entity>
                            <a:Attributes>
                                <a:KeyValuePairOfstringanyType>
                                    <b:key>cm_name</b:key>
                                    <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">12/9/2013 5:15:32 PM</b:value>
                                </a:KeyValuePairOfstringanyType>
                                <a:KeyValuePairOfstringanyType>
                                    <b:key>cm_parent_nurturetrackdefinitionid</b:key>
                                    <b:value i:type="a:EntityReference">
                                        <a:Id>b52b7b97-945e-e311-bb8e-6c3be5a8a238</a:Id>
                                        <a:LogicalName>cm_nurturetrackdefinition</a:LogicalName>
                                        <a:Name>Test Recurring 001</a:Name>
                                    </b:value>
                                </a:KeyValuePairOfstringanyType>
                                <a:KeyValuePairOfstringanyType>
                                    <b:key>cm_startedon</b:key>
                                    <b:value i:type="c:dateTime" xmlns:c="http://www.w3.org/2001/XMLSchema">2013-12-09T17:15:44Z</b:value>
                                </a:KeyValuePairOfstringanyType>
                                <a:KeyValuePairOfstringanyType>
                                    <b:key>cm_progress</b:key>
                                    <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">
                                        [AddToMarketingList]=Completed; &#xD;
                                        [SendNotificationEmail]=Queued; &#xD;
                                    </b:value>
                                </a:KeyValuePairOfstringanyType>
                                <a:KeyValuePairOfstringanyType>
                                    <b:key>statuscode</b:key>
                                    <b:value i:type="a:OptionSetValue">
                                        <a:Value>7</a:Value>
                                    </b:value>
                                </a:KeyValuePairOfstringanyType>
                                <a:KeyValuePairOfstringanyType>
                                    <b:key>cm_nurturetrackinstanceid</b:key>
                                    <b:value i:type="c:guid" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/">7d817988-f560-e311-9849-6c3be5a8fdb8</b:value>
                                </a:KeyValuePairOfstringanyType>
                            </a:Attributes>
                            <a:EntityState i:nil="true"/>
                            <a:FormattedValues>
                                <a:KeyValuePairOfstringstring>
                                    <b:key>cm_startedon</b:key>
                                    <b:value>12/9/2013 12:15 PM</b:value>
                                </a:KeyValuePairOfstringstring>
                                <a:KeyValuePairOfstringstring>
                                    <b:key>statuscode</b:key>
                                    <b:value>Executing</b:value>
                                </a:KeyValuePairOfstringstring>
                            </a:FormattedValues>
                            <a:Id>7d817988-f560-e311-9849-6c3be5a8fdb8</a:Id>
                            <a:LogicalName>cm_nurturetrackinstance</a:LogicalName>
                            <a:RelatedEntities/>
                        </a:Entity>
                    </a:Entities>
                    <a:EntityName>cm_nurturetrackinstance</a:EntityName>
                    <a:MinActiveRowVersion>-1</a:MinActiveRowVersion>
                    <a:MoreRecords>false</a:MoreRecords>
                    <a:PagingCookie>&lt;cookie page="1"&gt;&lt;cm_startedon last="2013-12-09T12:15:44-05:00" first="2013-12-10T15:10:49-05:00" /&gt;&lt;cm_nurturetrackinstanceid last="{7D817988-F560-E311-9849-6C3BE5A8FDB8}" first="{B1DF0728-D761-E311-84C5-6C3BE5A8BCD8}" /&gt;&lt;/cookie&gt;</a:PagingCookie>
                    <a:TotalRecordCount>-1</a:TotalRecordCount>
                    <a:TotalRecordCountLimitExceeded>false</a:TotalRecordCountLimitExceeded>
                </b:value>
            </a:KeyValuePairOfstringanyType>
        </a:Results>
    </ExecuteResult>
</ExecuteResponse>

Upvotes: 3

Views: 845

Answers (1)

Kevin B
Kevin B

Reputation: 95054

You have to first convert the xml string into an xml document, then you can search within that document by tag name. Leave off the namespace.

http://jsfiddle.net/2D9MB/

var xml = $("#xml").text();
var xmlDoc = $.parseXML(xml);
var definitionId ="b1df0728-d761-e311-84c5-6c3be5a8bcd8";
// find all entities that have an Id that contains the definitionId
var entities = $(xmlDoc).find("Entity:has(Id:contains(" + definitionId + "))");
console.log(entities); // one entity

You might even be able to shorten the last line to this:

var entities = $(xmlDoc).find("Entity:contains(" + definitionId + ")");

Upvotes: 4

Related Questions