jquery - 使用 Javascript 或 JQuery 按元素值检索 XML 节点

标签 jquery xml

我正在处理一个问题,我需要按特定属性值过滤 XML 文档并检索所有匹配的节点。但是,我没有得到想要的结果。

这是我用来遍历两个单独的 XML 文档的 jQuery。为了简单起见,现在我正在对 definitionId 进行硬编码。然后我需要按 definitionId 过滤 instanceData XML 文档并返回所有符合该条件的节点。

$(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;
    }
});
}

示例 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>

最佳答案

您必须首先将 xml 字符串转换为 xml 文档,然后您可以通过标签名称在该文档中进行搜索。离开命名空间。

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

您甚至可以将最后一行缩短为:

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

关于jquery - 使用 Javascript 或 JQuery 按元素值检索 XML 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20551790/

相关文章:

javascript - 如何使用正则表达式确保 3 个字符后有空格

javascript - 如何关闭 iframe 窗口?

java - 将 Element 对象转换为 Int?

java - 错误: Error parsing XML: not well-formed (invalid token)?

sql - 通过 SQL 从 Oracle 中的 clob 中提取 XML 子标签

javascript - 查找日期之间所有 6 个月的开始日期和结束日期 javascript

javascript - 使用 jQuery 调整事件大小,删除并重新添加 DOM 中的元素

jquery - 有没有办法在同一时隙中在其他事件后面全 Angular 显示 FullCalendar 事件?

android - 在 gridview 项目之间显示一条细线

java - 如何将 EditText 中的所有单个整数相加?