用于在 ColdFusion-8 中查找 parent 个具有特定名称的 children 个节点的 XPath

XPath to find parent of children nodes with certain names in ColdFusion-8

使用 Coldfusion 8 和 XMLSearch() 我正在尝试构建一个 XPath() 语句,它将找到 parent.

的特定 children 的出现

我有 parents combinationCharts 他们可以有 children 或 barline 或两者兼而有之。我需要能够找到 bar no line 的三个组合; line no bar;或者 bar and line 我已经尝试了几次迭代

<cfset myBarLineChart = XMLSearch(cleanedXML, "//*[descendant::combinationChartTypes][contains(name(),'bar') and contains(name(),'line')]" )>

使用 descendantchild//* 等的不同组合。不知何故,我就是找不到获得任何结果的魔法命令。

有效的是:

<cfset myBarChart = XMLSearch(cleanedXML, "//*[name()='combinationChartTypes']//*[name()='bar']")>  

<cfset myLineChart = XMLSearch(cleanedXML, "//*[name()='combinationChartTypes']//*[name()='line']")> 

但问题是,当 combinationChartTypes 同时具有 barline

时,它们会重复计算

我试过以下解决方案:

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

Is it possible in XPath to find parent when none of the children met some criteria

一切都无济于事,即使在阅读和 re-reading 之后: http://www.w3schools.com/xpath/xpath_axes.asp

http://zvon.org/comp/r/tut-XPath_1.html#Pages~List_of_XPaths

任何帮助我指出正确方向或彻底解决这个问题的人都将不胜感激!

这是我正在搜索的示例 xml:

<?xml version="1.0" encoding="UTF-8"?>
<report expressionlocale="en" ignorefiltercontext="false" xmlns="http://www.w3.org/1999/xhtml"><reportName>Revenue by GO Subsidiary 2005</reportName>
...
<!--- Example of line chart only --->
<combinationChartTypes>
    <line bordercolor="black" datapointsize="4" pointchartdatapointshape="verticalLine" showabsolutevalues="true" showborders="false" showline="false" showvalues="false" usenumericalaxis="numericalAxisY1" valuetype="absolute">
        <chartNodes>
            <chartNode>
                <chartNodeMembers>
                    <chartNodeMember refdataitem="Example of line chart only">
                        <chartContents>
                            <chartTextItem>
                                <dataSource>
                                </dataSource>
                            </chartTextItem>
                        </chartContents>
                    </chartNodeMember>
                </chartNodeMembers>
            </chartNode>
        </chartNodes>
    </line>
</combinationChartTypes>
...
<!--- Example of bar chart only --->
...
<combinationChartTypes>
    <bar>
        <chartNodes>
            <chartNode>
                <chartNodeMembers>
                    <chartNodeMember refdataitem="Example of bar chart only">
                        <chartContents>
                            <chartTextItem>
                                <dataSource>
                                </dataSource>
                                    <conditionalDataSources refvariable="Report Language1">
                                    <conditionalDataSource refvariablevalue="de">
                                        <staticValue>Einnahmen (Millionen)</staticValue>
                                    </conditionalDataSource>
                                    <conditionalDataSource refvariablevalue="fr">
                                        <staticValue>Revenus (millions)</staticValue>
                                    </conditionalDataSource>
                            </chartTextItem>
                        </chartContents>
                    </chartNodeMember>
                </chartNodeMembers>
            </chartNode>
        </chartNodes>
    </bar>
</combinationChartTypes>
...
<!--- Example of bar and line chart mixed --->
...
<combinationChartTypes>
    <bar>
        <chartNodes>
            <chartNode>
                <chartNodeMembers>
                    <chartNodeMember refdataitem="example of bar and line chart mixed">
                        <chartContents>
                            <chartTextItem>
                                <dataSource>
                                </dataSource>
                                    <conditionalDataSources refvariable="Report Language1">
                                    <conditionalDataSource refvariablevalue="de">
                                        <staticValue>Einnahmen (Millionen)</staticValue>
                                    </conditionalDataSource>
                                    <conditionalDataSource refvariablevalue="fr">
                                        <staticValue>Revenus (millions)</staticValue>
                                    </conditionalDataSource>
                            </chartTextItem>
                        </chartContents>
                    </chartNodeMember>
                </chartNodeMembers>
            </chartNode>
        </chartNodes>
    </bar>
    <line bordercolor="black" datapointsize="4" pointchartdatapointshape="verticalLine" showabsolutevalues="true" showborders="false" showline="false" showvalues="false" usenumericalaxis="numericalAxisY1" valuetype="absolute">
        <chartNodes>
            <chartNode>
                <chartNodeMembers>
                    <chartNodeMember refdataitem="Same quarter, 2004">
                        <chartContents>
                            <chartTextItem>
                                <dataSource>
                                </dataSource>
                            </chartTextItem>
                        </chartContents>
                    </chartNodeMember>
                </chartNodeMembers>
            </chartNode>
        </chartNodes>
    </line>
</combinationChartTypes>
...
</report>

如果您想要正在搜索的文件的完整 long-ugly 格式,请在此处:http://jsfiddle.net/asheppardwork/3uuaj5jb/

您缺少默认命名空间 (xmlns="http://www.w3.org/1999/xhtml")。 (您的 XML 在 XHTML 命名空间中而 实际上 是 XHTML 的事实相当可疑。我认为它的创建过程已损坏。如果您对此有任何影响,我建议你从那里开始。)

在标准 XPath 1.0 使用期间,搜索 //combinationChartTypes 只会出现 <combinationChartTypes> 个不在命名空间 中的节点

但你没有这些。

现在通常您会注册您需要的名称空间 URI,然后才能在 XPath 中使用它们,但 ColdFusion 8 不提供这样做的方法。 (如果可以,您可以将 http://www.w3.org/1999/xhtml 注册为 xhtml,然后查询 "//xhtml:combinationChartTypes"。)


XmlSearch() 自动支持 XML 来源中的名称空间前缀:

<doc xmlns:foo="http://foo">
  <foo:element />
</doc>

<cfset result = XmlSearch(doc, "//foo:element")>

会return一些东西。但是,对于具有默认名称空间(即无前缀)的 XML 情况并非如此。

在那种情况下,您仍然可以退回到这个更麻烦的模式:

<cfset path = "//*[local-name() = 'combinationChartTypes' and *[local-name() = 'bar'] and not(*[local-name() = 'line'])]">
<cfset myBarChart = XMLSearch(cleanedXML, path)>

或者,作为一个肮脏的 hack,你 可以 摆脱 XmlParse(Replace(xml, 'xmlns="namespace-uri-in-question"', '')) 然后在现在没有命名空间 [=50] 上正常使用 XmlSearch() =].