2010-12-07 13 views
1

XMLSearch()はXMLノードセットの配列を返します。しかし、これは私が必要なものではありません。例えば、現在私はXMLノード上にいて、次のノード(その兄弟)に移動したいのですが、どうすればいいのですか?私が知っているのは、XMLSearch()とXPathを使用することですが、実際には配列ではなくXML要素またはXMLドキュメントが必要です。ColdFusion XMLSearch

返された配列をXMLに変更するにはどうすればよいですか?


最初にArrayToListを使用して、XMLParseを使用してリストをxmlドキュメントに変換できますか?

答えて

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

私は役に立つと思います。

+0

これは、兄弟が同じノード名であれば機能しますが、兄弟が異なるノード名である場合は、配列に返されません。 –

2

配列に返されるノードは、完全な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::*')#"> 
0

XMLドキュメントのXMLドキュメントを使用するには、XMLTransform()を使用できます。

XSLTをビルドする必要がありますが、現在使用しているXPathが動的ではない場合は動作します。
XSLTに慣れていない場合は、新しい学習曲線が得られますが、それは登るのに便利な曲線です。