在ColdFusion 8中递归查询XML以查找未知数量的子级别

时间:2015-02-06 16:11:48

标签: xml recursion coldfusion-8

我们有一个由两个盒子组成的集群ColdFusion 8环境。我试图遍历由Cognos 10.1创建的XML,以按照它们在XML中出现的顺序查找每个元素。目的是创建报告的唯一布局的缩略图html表示,用于替换元素的图标。

皱纹是Cognos为报表中的每个新对象放置一个新的[block]。[content]元素,然后根据创建报表时确定的分层顺序嵌套它们。

例如,如果您放入一个包含单个列的列表对象,则应用必须遍历

html.body.report.layouts.layout.reportPages.page [...]
pageBody
        block
            contents
                    block=list
                    block
                         contents=column 

每份报告都有不确定的复杂程度。我使用Ben Nadel的CF8技术解析了xml和cfgroovy,并且有可用的xml源代码。

现在我必须找到一个"列表"存在及其在文件中的顺序。

到目前为止,我已经在循环中使用了循环,如:

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

然而,这只会下降两个级别(因为它只有两个循环)。

我也尝试过与Ben's的忏悔: http://www.bennadel.com/blog/1069-ask-ben-simple-recursion-example.htm

失败了,因为CF8不喜欢动态传递父节点。

cleanedXml.html.body.report.layouts.layout.reportPages.page有效且cleanedXml.html.body.report.layouts.layout.reportPages.page.#parentString#无效

这似乎应该是一个简单的解决方案,但我只是不能为每个潜在的级别进行循环或递归,这只会在第一次有人用另一个级别编写报告时中断比我预想的还要好。

如果在cfml中无法做到这一点;我愿意在jQuery中尝试它,但我会重新开始,因为我的jQuery是有限的。

这是一个1400行xml的小样本,它是一个小的报告文件。 注意&#34;列表&#34;在第195行然后是&#34; listColumn&#34;在第226行,我将按此顺序需要这两个。

`
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 个答案:

答案 0 :(得分:1)

我觉得这个完全是假的!我找到了满足我需要的答案;不一定是我的问题。而不是使用recusion;我重新查看了我没有提及的XmlSearch代码,因为我认为我已经将其排除在外。事实证明我只是使用了错误的语法!在stackoverflow上找到ColdFusion XmlSearch(): XPath with namespaces后,我去了这些 -

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.w3.org/TR/xpath/

我已经创建了一个搜索,它会按照它们在xml中显示的顺序显示结果!

`<cfset listInXml = #XmlSearch(cleanedXml,"//*[ local-name()='list' or local-name()='combinationChart' ]")# >`   

这返回了图表,图表,列表,图表等等,就像它在我的jsfiddle- http://jsfiddle.net/asheppardwork/3uuaj5jb/1/

我被困了,因为我只使用了Ben Nadel在他网站上的语法;从来没有想过它可能会因为cf8而发生变化。

我希望这可以帮助其他人寻找相同类型的东西。