开发者

ColdFusion XMLSearch

I know XMLSearch() will return a array of XML node sets. But this is NOT what I need. For example, currently I am on a xml node and I want to move to next node,(its sibling),how can do this? What I know is to use XMLSearch() and XPath, but I really need what I got is still XML Element or XML document rather than array.

How can I change the returned array to XML or is there any other better way?


Is is possible to first use ArrayToList and then use use XMLParse to co开发者_如何转开发nvert the list to xml document?


The nodes returned in the array are references to the nodes in the full xml document. There is also an undocumented "attribute" of an xml node called xmlparent.

<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::*')#">


<!--- 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#" />

I hope that helps.


If you want an XML doc from an XML doc you could use XMLTransform().

You'd need to build an XSLT but, if the XPath you use now isn't dynamic it would work.
If you aren't familiar with XSLT you've got a new little learning curve but it's a useful curve to climb.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜