2016-01-28 52 views
1

我有一個分配給ColdFusion變量的XML文檔。在本文檔中調用XmlSearch()會生成一個XML節點數組。在其中一個XML節點上調用XmlSearch()將產生與在原始XML文檔上調用它相同的輸出。XmlSearch()不處理數組中的單個XML節點

問題是什麼?

下面是一個代碼示例(CFSCRIPT):

// xmlSource is an XML file that has been read in 
xmlDoc = XmlParse(xmlSource); 

// "return" is a high-level XML node in xmlSource that appears more than one time 
xmlNodeArray = XmlSearch(xmlDoc, "//return"); 

// a single "return" node from xmlDoc 
xmlNode = xmlNodeArray[1]; 

// "recipients" is an XML node that appears one or more times beneath each "return" XML node 
xmlArray = XmlSearch(xmlNode, "//recipients"); 

// this prints out all of the "recipients" nodes in xmlDoc instead of just from xmlNode 
WriteDump(xmlArray); 
+1

嘗試使用「./recipients」作爲XPath。 – abbottmw

+0

這可能會爲您帶來一些燈光:http://blog.adamcameron.me/2014/01/expectations-management-what-does.html –

+0

@abbottmw'。/ recipients'工作,謝謝。 ColdFusion似乎在'XmlSearch()'的返回值中有或多或少的指針式變量數組......使得'xmlNode'中的整個原始'xmlDoc'可用。 – user3071284

回答

1

的問題是與XPath的。更改

xmlArray = XmlSearch(xmlNode, "//recipients"); 

xmlArray = XmlSearch(xmlNode, "./recipients"); 

固定它。

+1

澄清那些誰沒有閱讀[亞當的評論](http://stackoverflow.com/questions/35066144/xmlsearch-does-not-process-an-individual-xml-node-in-an-array#comment57857324_35066144) ,使用相對'.'而不是'//'強制xmlSearch從所提供的節點開始搜索,即'xmlNode',而不是整個文檔。 – Leigh