0

I have an MVC application which is querying a Lucene/Solr index. In a partial view, I am displaying metadata on a series of XML documents returned from the query. The metadata is extracted from the XML documents using XPath queries and is paged, listing 10 documents at a time. The view cshtml is as follows...

@model SolrMVCTest2.Models.Facet

<section>


@foreach (System.Xml.XmlNode doc in Model.SearchResult.SelectNodes("/response/result/doc"))
{
 <article>
    <b>Document type:  @doc.SelectSingleNode("//str[@name='docType']").InnerText  </b>  <br />
    <b>NHS Number:  @doc.SelectSingleNode("//str[@name='crn']").InnerText  </b>  <br />
    @doc.OuterXml.ToString()
</article>
<p></p>

}
</section>

I have included a display of the OuterXML for debugging purposes and a partial listing of the results highlights a serious issue. FOr each group of 10 documents, the innertext of the selected nodes is not updated for each successive document, but always displays the values from the first document of the set of 10 as shown in this partial result....

Document type: MedicalHistory  
Patient Number: 7024326654  
 <doc><str name="id">mh00k2jr001002</str><str name="crn">7024326654</str><str     name="docType">MedicalHistory</str></doc> 


Document type: MedicalHistory  
Patient Number: 7024326654  
 <doc><str name="id">mh00k2ju001002</str><str name="crn">0437861414</str><str name="docType">MedicalHistory</str></doc> 


Document type: MedicalHistory  
Patient Number: 7024326654  
 <doc><str name="id">mh00k2k1001002</str><str name="crn">7626346416</str><str name="docType">MedicalHistory</str></doc> 


Document type: MedicalHistory  
Patient Number: 7024326654  
 <doc><str name="id">mh00k2mp001002</str><str name="crn">2637403616</str><str name="docType">MedicalHistory</str></doc> 


Document type: MedicalHistory  
Patient Number: 7024326654  
 <doc><str name="id">mh00k2nm001001</str><str name="crn">0874706416</str><str name="docType">MedicalHistory</str></doc> 


Document type: MedicalHistory  
Patient Number: 7024326654  
 <doc><str name="id">mh00k2qq001002</str><str name="crn">1867082616</str><str name="docType">MedicalHistory</str></doc> 


Document type: MedicalHistory  
Patient Number: 7024326654  
 <doc><str name="id">mh00k2ss001001</str><str name="crn">5436985416</str><str name="docType">MedicalHistory</str></doc> 


Document type: MedicalHistory  
Patient Number: 7024326654  
 <doc><str name="id">mh00k2yi002003</str><str name="crn">8731654606</str><str name="docType">MedicalHistory</str></doc> 


Document type: MedicalHistory  
Patient Number: 7024326654  
 <doc><str name="id">mh00k2yr001001</str><str name="crn">0906133416</str><str name="docType">MedicalHistory</str></doc> 


Document type: MedicalHistory  
Patient Number: 7024326654  
 <doc><str name="id">op00k23e001001</str><str name="crn">6335192616</str><str name="docType">OutpatientAnnotation</str></doc> 

The Patient Number display (extracted from the str element named "crn" is not updated obn the second and successive documents, and the Document Type (fromt he docType element) is not updated on the 10th.

Does anyone have any ideas which may explain this behaviour?

4

1 回答 1

0

您有这种行为,因为您的内部 xpath 从双斜杠开始//

@doc.SelectSingleNode("//str[@name='docType']")

这意味着对 xml 文档进行全局搜索,str无论它在文档中的哪个位置,它都会返回与您的搜索匹配的第一个找到的元素。这就是为什么你总是看到第一个doc元素的数据。

使用不带双斜杠的 xpath 获取当前节点str的元素:doc

@doc.SelectSingleNode("str[@name='docType']")

还要考虑以下 - 而不是在视图中解析数据,而是创建视图模型,它将包含所需的数据:

public class Document
{
    public string Type { get; set; }
    public string PatientNumber { get; set; }
}

并在控制器中创建文档

var documents = 
      from doc in SearchResult.SelectNodes("/response/result/doc")
      select new Document {
          Type = doc.SelectSingleNode("str[@name='docType']").InnerText,
          PatientNumber = doc.SelectSingleNode("str[@name='crn']").InnerText
      };

return View(documents);
于 2013-07-31T12:17:40.660 回答