XPath评估仅在完整文档中搜索

时间:2014-10-25 07:24:13

标签: xpath

我遇到的问题是,XPath评估仅针对完整的文档而不是单个节点。我的`xml就像:

<?xml version="1.0" encoding="UTF-8"?>

<RuleConfiguration>
    <Name>RDG1</Name>
    <IsActivated>Y</IsActivated>
    <ColorCode>#FF0000</ColorCode>
    <HaltAppIfRuleMet>Y</HaltAppIfRuleMet>
    <Parameters>
        <Parameter>
            <ParameterName>CompareToBalance</ParameterName>
            <ParameterValue>0.0</ParameterValue>
        </Parameter>
        <Parameter>
            <ParameterName>FONDEP</ParameterName>
            <ParameterValue>520</ParameterValue>
        </Parameter>
        <Parameter>
            <ParameterName>RDG1Counter1</ParameterName>
            <ParameterValue>1</ParameterValue>
        </Parameter>
    </Parameters>
    <Descriptions>
        <Description>
            <LocaleCode>en_US</LocaleCode>
            <LocaleText>The customer has 3 or more active accounts outside FONDEP</LocaleText>
        </Description>
        <Description>
            <LocaleCode>fr_FR</LocaleCode>
            <LocaleText>The customer has 3 or more active accounts outside FONDEP</LocaleText>
        </Description>
    </Descriptions>
</RuleConfiguration>
<!--  Rule no 2  -->
<RuleConfiguration>
    <Name>RDG2</Name>
    <IsActivated>Y</IsActivated>
    <ColorCode>#FF0000</ColorCode>
    <HaltAppIfRuleMet>Y</HaltAppIfRuleMet>
    <Parameters>
        <Parameter>
            <ParameterName>CompareToBalance_ahBal</ParameterName>
            <ParameterValue>0.0</ParameterValue>
        </Parameter>
        <Parameter>
            <ParameterName>CompareToBalance_ahBalArr</ParameterName>
            <ParameterValue>0.0</ParameterValue>
        </Parameter>
        <Parameter>
            <ParameterName>FONDEP</ParameterName>
            <ParameterValue>520</ParameterValue>
        </Parameter>
        <Parameter>
            <ParameterName>RDG2Counter1</ParameterName>
            <ParameterValue>2</ParameterValue>
        </Parameter>
        <Parameter>
            <ParameterName>RDG2Counter2</ParameterName>
            <ParameterValue>1</ParameterValue>
        </Parameter>
    </Parameters>
    <Descriptions>
        <Description>
            <LocaleCode>en_US</LocaleCode>
            <LocaleText>The customer has 3 or more active accounts outside FONDEP</LocaleText>
        </Description>
        <Description>
            <LocaleCode>fr_FR</LocaleCode>
            <LocaleText>The customer has 3 or more active accounts outside FONDEP</LocaleText>
        </Description>
    </Descriptions>
</RuleConfiguration>

我的代码是:

XPath resXPTraverse = XPathFactory.newInstance().newXPath();
String strRuleConfigPath = "/RuleConfigurations/RuleConfiguration";
NodeList nlRuleNodeList = (NodeList)resXPTraverse.compile(strRuleConfigPath).evaluate(ExpConfigXML, XPathConstants.NODESET); 
System.out.println("/RuleConfigurations/RuleConfigurationlength>>>"+nlRuleNodeList.getLength()); //returning length 2 correct
String strDescriptionsPath = strRuleConfigPath+"/Descriptions/Description";
NodeList nlDescriptionsList = (NodeList) resXPTraverse.compile(strDescriptionsPath).evaluate(nlRuleNodeList.item(0), XPathConstants.NODESET);
System.out.println("/Descriptions/Description length>>>"+nlDescriptionsList.getLength()); //returning length 4 instead of 2 

我正在尝试仅获取第一个节点的标记,但它正在搜索完整的XML文档并返回长度为4。 请帮助我。

1 个答案:

答案 0 :(得分:0)

评估strRuleConfigPath+"/Descriptions/Description"意味着评估 /RuleConfigurations/RuleConfiguration/Descriptions/Description。您提供的表达式有4种不同的路径。

您需要评估: "/RuleConfigurations/RuleConfiguration[1]/Descriptions/Description"