带谓词的xpath查询

时间:2010-11-17 01:27:26

标签: javascript xpath

我在编写xpath查询时遇到问题。我正在尝试获得具有serviceTechnology ='DSL'的产品。

我试过了:

//offering/productDefinition[definitionID='Product_1' or definitionID='Product_2' or definitionID='Product_3' or definitionID='Product_4' and serviceTechnologyList[serviceTechnology='DSL']]

//offering/productDefinition[definitionID='Product_1' or definitionID='Product_2' or definitionID='Product_3' or definitionID='Product_4' and serviceTechnologyList='DSL']

这是基于此处的其他问题列表。我一直得到“谓词格式错误”。

<offeringList>
    <offering>
        <productDefinition>
            <definitionID>Product_1</definitionID>
            <productLineList>
                <productLine>DSL</productLine>
            </productLineList>
            <serviceTechnologyList>
                <serviceTechnology>DSL</serviceTechnology>
            </serviceTechnologyList>
        </productDefinition>
    </offering>
    <offering>
        <productDefinition>
            <definitionID>Product_2</definitionID>
            <productLineList>
                <productLine>DSL</productLine>
            </productLineList>
            <serviceTechnologyList>
                <serviceTechnology>IPTV</serviceTechnology>
            </serviceTechnologyList>
        </productDefinition>
    </offering>
    <offering>
        <productDefinition>
            <definitionID>Product_3</definitionID>
            <productLineList>
                <productLine>DSL</productLine>
            </productLineList>
            <serviceTechnologyList>
                <serviceTechnology>DSL</serviceTechnology>
            </serviceTechnologyList>
        </productDefinition>
    </offering>
    <offering>
        <productDefinition>
            <definitionID>Product_4</definitionID>
            <productLineList>
                <productLine>DSL</productLine>
            </productLineList>
        </productDefinition>
    </offering>
</offeringList>

任何帮助都将不胜感激。

4 个答案:

答案 0 :(得分:1)

我一眼就看不到任何错误 - 两种工作:),也许你没有逃避某些事情?

在此处尝试查询以确保工作正常:xpath tester

答案 1 :(得分:0)

为什么不呢:

/offeringList/offering/productDefinition[serviceTechnologyList/serviceTechnology = 'DSL']

如果您想具体了解definitionID,可以执行以下操作:

//offeringList/offering/productDefinition[serviceTechnologyList/serviceTechnology = 'DSL'][definitionID='Product_1' or definitionID='Product_2' or definitionID='Product_3' or definitionID='Product_4']

答案 2 :(得分:0)

您需要使用括号来定义andor运算符的优先级:

//offering/productDefinition[(definitionID='Product_1' or definitionID='Product_2' or definitionID='Product_3' or definitionID='Product_4') and serviceTechnologyList[serviceTechnology='DSL']]

如果您只想要拥有productDefinition的{​​{1}}个节点,您可以简化为:

serviceTechnology='DSL'

答案 3 :(得分:0)

@ icyrock.com指出,问题在于布尔运算符:and的优先级高于or

此表达式使用另一种方法来匹配XPath 1.0中的序列:

//offering/productDefinition
             [contains('|Product_1|Product_2|Product_3|Product_4|',
                       concat('|',definitionID,'|')) 
              and serviceTechnologyList/serviceTechnology='DSL']

如果ID列表太大,您可以使用:

//offering/productDefinition
             [starts-with(definitionID,'Product_')
              and contains('|1|2|3|4|6|8|22|',
                           concat('|',
                                  substring-after(definitionID,
                                                  'Product_'),
                                  '|')) 
              and serviceTechnologyList/serviceTechnology='DSL']

或者如果有范围:

//offering/productDefinition
             [starts-with(definitionID,'Product_')
              and substring-after(definitionID,'Product_') >= 1
              and 4 >= substring-after(definitionID,'Product_')
              and serviceTechnologyList/serviceTechnology='DSL']