祖先/先前/以下条件到Xpath

时间:2014-03-06 16:06:57

标签: xml xpath

我有以下xml

<ROOT>
<ITC>44</ITC>
<Description></Description>
<Desc_ID></Desc_ID>
<GenCommunity>
    <Community>
        <SubCommunity>
            <CID>6655779999</ID>
        </SubCommunity>
        <CommunityIdGroup>
            <Group>Charly</Group>
        </CommunityIdGroup>
        <CommunitySchool>
            <CID>1143211234</CID>
            <SchoolScheme>
                <ID>DEY</ID>
            </SchoolScheme>
        </CommunitySchool>
        <CommunitySchool>
            <CID>1143211234</CID>
            <SchoolScheme>
                <ID>BSY</ID>
            </SchoolScheme>
        </CommunitySchool>
    </Community>
</GenCommmunity>
</ROOT>

目前我使用这个完美的XPath:

//Community/CommunitySchool/CID[following-sibling::SchoolScheme/ID = 'DEY']
[text()="1143211234"

但现在我需要在此Xpath中添加一个条件,以便在文件开头不选择ITC = 44。

我试过了:

//Community/CommunitySchool/CID[following-sibling::SchoolScheme/ID = 'DEY' and  
ancestor::ITC!='44'][text()="1143211234"

//Community/CommunitySchool/CID[following-sibling::SchoolScheme/ID = 'DEY' and 
not(ancestor::ITC='44')][text()="1143211234"

还有更多没有运气。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您可以使用:

/ROOT[not(ITC='44')]//Community/CommunitySchool/CID[following-sibling::SchoolScheme/ID = 'DEY']
[text()="1143211234"]

您也可以添加谓词,如:

[not(preceding::ITC='44')]

到现有路径中的某个节点。您无法使用ancestor::,因为ITC实际上不是Community的祖先。