问题
We have a clustered ColdFusion 8 environment consisting of two boxes. I am trying to traverse XML that was created by Cognos 10.1 to find every element in the order they appear in the XML. The purpose is to create a thumbnail html representation of the report's unique layout that substitutes icons for elements.
The wrinkle is that Cognos puts a new [block].[content] element for each new object in a report and then nests them based on a layered order determined when the report is created.
For example if you put in a list object with a single column the app will have to traverse through
html.body.report.layouts.layout.reportPages.page [...]
pageBody
block
contents
block=list
block
contents=column
Each report will have a undetermined amount of complexity. I have parsed the xml using Ben Nadel's techniques for CF8 with cfgroovy and have usable xml source.
Now I have to find if a "list" exists and the order it is in the file.
So far, I've used loops in loops like:
<cfset cleanedXml = xmlParse( xhtml ) />
<cfloop array="#XmlSearch(cleanedXml.html.body.report.layouts.layout.reportPages.page,'./.')#" index="i">
<cfoutput>
<cfif structKeyExists(i, "xmlattributes")>
<cfloop collection="#i.xmlattributes#" item="a">
#a#: #i.xmlAttributes[a]#<br/>
</cfloop>
</cfif>
<cfloop array="#i.xmlchildren#" index="x">
#x.xmlName#: #x.xmlText#<br/>
<cfif structKeyExists(x, "xmlchildren") >
<cfloop array="#x.xmlchildren#" index="z">
#z.xmlName#: #z.xmlText#<br/>
</cfloop>
</cfif>
</cfloop>
</cfoutput>
<br/>
</cfloop>
However, that only goes down two levels (because its only two loops).
I've also tried Recusion with Ben's: http://www.bennadel.com/blog/1069-ask-ben-simple-recursion-example.htm
That failed because CF8 does not like having the parent node passed in dynamically.
cleanedXml.html.body.report.layouts.layout.reportPages.page
works and cleanedXml.html.body.report.layouts.layout.reportPages.page.#parentString#
does not
It seems like this should be a simple fix, but I just can't get past having to have a loop or recursion for each potential level, which would only break the first time someone wrote a report with one more level than I've anticipated.
If this can't be done in cfml; I am willing try it in jQuery but I would be starting all over again as my jQuery is limited.
Here is a small sample of a 1400 line xml which is a small report file. Notice the "list" on line 195 then its "listColumn" on line 226, I would need both of these in this order.
`
xmlresults - array - Top 1 of 1 rows
1) [xml element]
XmlName: pageBody
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: style
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: contents
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: block
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: contents
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: promptButton
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
type: back
XmlChildren:
[xml element]
XmlName: contents
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: style
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: HTMLItem
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: dataSource
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: staticValue
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: textItem
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: dataSource
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: staticValue
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText: Note: Clicking the "Back" button will not clear any selected Filters.
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: style
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: conditionalStyles
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: conditionalStyleCases
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
refvariable: back1
XmlChildren:
[xml element]
XmlName: conditionalStyle
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
refvariablevalue: 1
XmlChildren:
[xml element]
XmlName: CSS
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
value: visibility:hidden
XmlChildren:
[xml element]
XmlName: conditionalStyleDefault
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: block
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: contents
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: list
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
horizontalpagination: true
name: List1
refquery: Query1
XmlChildren:
[xml element]
XmlName: style
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: listColumns
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: listColumn
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: listColumnTitle
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
[xml element]
XmlName: style
XmlNsPrefix:
XmlNsURI: http://www.w3.org/1999/xhtml
XmlText:
XmlComment:
XmlAttributes: [struct]
XmlChildren:
`
回答1:
I feel like a complete dummy on this one! I found a answer to my need; not necessarily my question. Rather than use recusion; I re-looked at my XmlSearch code which I didn't mention because I thought I already ruled it out as a possibility. Turns out I was just using the wrong syntax! After finding: ColdFusion XmlSearch(): XPath with namespaces on stackoverflow I went to these-
http://www.coldfusionguy.com/ColdFusion/blog/index.cfm/2008/9/26/XMLSearch-Specify-xmlns-namespaces-in-an-xPath-Search
http://www.bennadel.com/blog/827-nylon-technology-presentation-introduction-to-xpath-and-xmlsearch-in-coldfusion.htm
Which led to: http://www.w3.org/TR/xpath/
I've created a search that will display the results in the order they appear in in the xml!
`<cfset listInXml = #XmlSearch(cleanedXml,"//*[ local-name()='list' or local-name()='combinationChart' ]")# >`
This returned Chart, Chart, List, Chart... etc. like it is in my jsfiddle- http://jsfiddle.net/asheppardwork/3uuaj5jb/1/
I got stuck because I was only using the syntax that Ben Nadel had on his site; I never figured it might have changed since then for cf8.
I hope this helps anyone else looking for the same type of thing.
来源:https://stackoverflow.com/questions/28370017/recursive-query-of-xml-in-coldfusion-8-to-find-unknown-number-of-children-sub-le