简体   繁体   English

在ColdFusion 8中对XML进行递归查询以查找子级数未知的子级

[英]Recursive Query of XML in ColdFusion 8 to find unknown number of children sub levels

We have a clustered ColdFusion 8 environment consisting of two boxes. 我们有一个由两个盒子组成的集群式ColdFusion 8环境。 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. 我试图遍历由Cognos 10.1创建的XML,以按照它们在XML中出现的顺序查找每个元素。 The purpose is to create a thumbnail html representation of the report's unique layout that substitutes icons for elements. 目的是创建报告的唯一布局的缩略图html表示,以图标代替元素。

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. 难题在于,Cognos为报表中的每个新对象放置一个新的[block]。[content]元素,然后根据创建报表时确定的分层顺序将它们嵌套。

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. 我已经使用Ben Nadel的CF8技术和CFgroovy对xml进行了解析,并且具有可用的xml源。

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 我还尝试了Ben的Recusion: 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. 失败是因为CF8不喜欢让父节点动态传递。

cleanedXml.html.body.report.layouts.layout.reportPages.page works and cleanedXml.html.body.report.layouts.layout.reportPages.page.#parentString# does not cleanedXml.html.body.report.layouts.layout.reportPages.page有效, cleanedXml.html.body.report.layouts.layout.reportPages.page.#parentString#不起作用

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; 如果无法在cfml中完成; I am willing try it in jQuery but I would be starting all over again as my jQuery is limited. 我愿意在jQuery中尝试,但由于jQuery受限制,我将重新开始。

Here is a small sample of a 1400 line xml which is a small report file. 这是一个1400行xml的小样本,这是一个小报告文件。 Notice the "list" on line 195 then its "listColumn" on line 226, I would need both of these in this order. 请注意第195行的“列表”,然后是第226行的“ listColumn”,我将按此顺序同时使用这两个函数。

`
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:
                                                         `

I feel like a complete dummy on this one! 我觉得这是一个完全的假人! I found a answer to my need; 我找到了需要的答案; not necessarily my question. 不一定是我的问题。 Rather than use recusion; 而不是使用recussion; I re-looked at my XmlSearch code which I didn't mention because I thought I already ruled it out as a possibility. 我重新查看了我没有提到的XmlSearch代码,因为我认为我已经排除了这种可能性。 Turns out I was just using the wrong syntax! 原来我只是使用了错误的语法! After finding: ColdFusion XmlSearch(): XPath with namespaces on stackoverflow I went to these- 找到之后: ColdFusion XmlSearch():在stackoverflow上使用名称空间的XPath我去了这些-

http://www.coldfusionguy.com/ColdFusion/blog/index.cfm/2008/9/26/XMLSearch-Specify-xmlns-namespaces-in-an-xPath-Search 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 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/ 导致了: 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! 我创建了一个搜索,该搜索将按照结果在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/ 这返回了图表,图表,列表,图表等,就像在我的jsfiddle中一样-http: //jsfiddle.net/asheppardwork/3uuaj5jb/1/

I got stuck because I was only using the syntax that Ben Nadel had on his site; 我之所以陷入困境,是因为我只使用Ben Nadel在他的网站上使用的语法; I never figured it might have changed since then for cf8. 从那以后,我再也没有想到cf8可能会改变。

I hope this helps anyone else looking for the same type of thing. 我希望这可以帮助其他人寻找相同类型的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM