2013-12-12 114 views
3

我正在處理一個問題,其中需要按特定屬性值篩選XML文檔並檢索所有匹配的節點。但是,我沒有得到理想的結果。通過使用Javascript或JQuery的元素值檢索XML節點

這是我用來遍歷兩個單獨的XML文檔的jQuery。爲了簡單起見,我正在對一個定義進行硬編碼。然後我需要通過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> 

+0

你可以補充足夠的重建中的問題你的問題?示例xml?你如何定義'cm'和'definitionData'?你想選擇什麼樣的XML?你使用的是什麼版本的jQuery? –

+0

Kevin,我想選擇符合條件的所有實體元素。我已將$ cm更改爲$以消除混淆。在這種情況下,我使用定義數據的唯一方法是檢索我需要用來過濾實例數據的正確ID。謝謝你快速的回覆。 –

回答

4

你必須首先將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 + ")"); 
+0

此外,只需要清楚,您要過濾的是nodeText,而不是屬性值。 –

+0

就是這樣。非常感謝!!! –