我正在研究一个问题,我需要通过特定的属性值过滤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; 
[SendNotificationEmail]=Completed; 
</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; 
[SendNotificationEmail]=Queued; 
</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><cookie page="1"><cm_startedon last="2013-12-09T12:15:44-05:00" first="2013-12-10T15:10:49-05:00" /><cm_nurturetrackinstanceid last="{7D817988-F560-E311-9849-6C3BE5A8FDB8}" first="{B1DF0728-D761-E311-84C5-6C3BE5A8BCD8}" /></cookie></a:PagingCookie>
<a:TotalRecordCount>-1</a:TotalRecordCount>
<a:TotalRecordCountLimitExceeded>false</a:TotalRecordCountLimitExceeded>
</b:value>
</a:KeyValuePairOfstringanyType>
</a:Results>
</ExecuteResult>
</ExecuteResponse>
答案 0 :(得分:4)
您必须先将xml字符串转换为xml文档,然后才能通过标记名称在该文档中进行搜索。不要使用命名空间。
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 + ")");