我知道XMLSearch()將返回一個XML節點集的數組。但這不是我所需要的。例如,目前我在一個XML節點上,並且我想要移動到下一個節點(它的兄弟節點),那怎麼辦?我所知道的是使用XMLSearch()和XPath,但我真的需要我得到的仍然是XML元素或XML文檔而不是數組。ColdFusion XMLSearch
如何將返回的數組更改爲XML還是有其他更好的方法?
是可能首先使用ArrayToList然後使用XMLParse將列表轉換爲XML文檔?
我知道XMLSearch()將返回一個XML節點集的數組。但這不是我所需要的。例如,目前我在一個XML節點上,並且我想要移動到下一個節點(它的兄弟節點),那怎麼辦?我所知道的是使用XMLSearch()和XPath,但我真的需要我得到的仍然是XML元素或XML文檔而不是數組。ColdFusion XMLSearch
如何將返回的數組更改爲XML還是有其他更好的方法?
是可能首先使用ArrayToList然後使用XMLParse將列表轉換爲XML文檔?
<!--- Create an example XML document ---->
<cfxml variable="xml">
<node1>
<child>hello1</child>
<child>hello2</child>
</node1>
</cfxml>
<!--- Search for nodes you need --->
<cfset res = xmlSearch(xml, "/node1/child") />
<!--- Output just the 2nd child as an XML document --->
<cfset xmlAsString = toString(res[2]) />
<cfdump var="#xmlAsString#" />
我希望有所幫助。
數組中返回的節點是對完整xml文檔中節點的引用。還有一個名爲xmlparent的xml節點的未記錄「屬性」。
<cfxml variable="foo">
<employee>
<!-- A list of employees -->
<name EmpType="Regular">
<first>Almanzo</first>
<last>Wilder</last>
<Status>Medical Absence</Status>
<Status>Extended Leave</Status>
</name>
<name EmpType="Contract">
<first>Laura</first>
<last>Ingalls</last>
</name>
</employee>
</cfxml>
<cfdump var="#foo#">
<cfset bar = xmlSearch(foo,"/employee/name/last[normalize-space()='Wilder']")>
<!--- If you know the node name of the sibling, you can just access it --->
<cfdump var="#bar[1].xmlparent['first']#">
<!--- If you don't know the node names, and just want to traverse via preceding and following order, you can do another xpath on the returned nodes --->
<cfdump var="#xmlSearch(bar[1],'./preceding-sibling::*')#">
<cfdump var="#xmlSearch(bar[1],'./following-sibling::*')#">
如果您想要XML文檔中的XML文檔,您可以使用XMLTransform()。
你需要構建一個XSLT,但是,如果你現在使用的XPath不是動態的,它就可以工作。
如果你對XSLT不熟悉,你會得到一個新的小學習曲線,但它是一條有用的曲線。
如果兄弟姐妹是相同的節點名稱,但是如果兄弟姐妹是不同的節點名稱,它們將不會在數組中返回。 – 2010-12-07 13:43:16