coldfusionxmlsearch
我知道XMLSearch()将返回一个XML节点集数组。但这不是我需要的。例如,当前我在一个xml节点上,我想移动到下一个节点(它的同级节点),如何做到这一点?我知道的是使用XMLSearch()和XPath,但我真正需要的仍然是XML元素或XML文档,而不是数组 如何将返回的数组更改为XML,或者还有其他更好的方法吗coldfusionxmlsearch,xml,coldfusion,Xml,Coldfusion,我知道XMLSearch()将返回一个XML节点集数组。但这不是我需要的。例如,当前我在一个xml节点上,我想移动到下一个节点(它的同级节点),如何做到这一点?我知道的是使用XMLSearch()和XPath,但我真正需要的仍然是XML元素或XML文档,而不是数组 如何将返回的数组更改为XML,或者还有其他更好的方法吗 是否可以先使用ArrayToList,然后使用XMLParse将列表转换为xml文档? <!--- Create an example XML document ----
是否可以先使用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,您会有一条新的学习曲线,但这是一条有用的曲线。如果兄弟节点是相同的节点名,则这条曲线有效,但如果兄弟节点是不同的节点名,则它们不会返回到数组中。