在XPath中选择特定节点

时间:2014-05-06 09:10:58

标签: xml xpath

我有以下XML结构。我可以识别突出显示的<story contenttype="News" id="cbc655617-ba25-4629-b2a2-073ba2369758">个节点,我需要到达通过XPath突出显示的<packagesection id="264311652" navTitle="News">个节点。你能帮忙找一个可以识别相同的XPath表达式吗?我无法使用 navTitle 属性,因为它是动态的,但我只知道 packagesection 节点必须是<的最顶级父级的第一级子级< strong>故事节点

<?xml version="1.0" encoding="UTF-8"?>
<package id="cfc35dc14-5345-42a4-b4f9-0b833e3fde3a">
    <title>Gas Daily</title>
    <sectionelement>
        <packagesection id="264311518" navTitle="Gas Daily">
            <sectionelement>
                <packagesection id="264311652" navTitle="News">
                    <sectionelement>
                        <packagesection id="264679044" navTitle="Test1">
                            <sectionelement>
                                <packagesection id="264679046" navTitle="Test2">
                                    <sectionelement>
                                        <topicref format="xml"
                                            href="cbc655617-ba25-4629-b2a2-073ba2369758" id="264679396"
                                            sequence="1" type="story">
                                            <story contenttype="News"
                                                id="cbc655617-ba25-4629-b2a2-073ba2369758">
                                                <slug>test_arko_20140502_special_byline</slug>
                                                <storyTitlealts>
                                                    <headline>test_arko_20140502_special_byline</headline>
                                                </storyTitlealts>
                                                <lead>test_arko_20140502_special_byline</lead>
                                                <storybody>
                                                    <p id="43102552-91a9-44a5-af4a-72b742460a5b">test_arko_20140502_special_byline</p>
                                                </storybody>
                                            </story>
                                        </topicref>
                                    </sectionelement>
                                </packagesection>
                            </sectionelement>
                            <sectionelement>
                                <topicref format="xml"
                                    href="c177f4ddd-e983-4730-b835-c7724178b223" id="264679820"
                                    sequence="2" type="story">
                                    <story contenttype="News"
                                        id="c177f4ddd-e983-4730-b835-c7724178b223">
                                        <slug>nothing</slug>
                                        <storyTitlealts>
                                            <headline>nothing</headline>
                                        </storyTitlealts>
                                        <lead>nothing</lead>
                                        <storybody>
                                            <p id="d558b9ac-5b28-4690-bb0c-664ccbe14e8f">nothing</p>
                                        </storybody>
                                    </story>
                                </topicref>
                            </sectionelement>
                        </packagesection>
                    </sectionelement>
                </packagesection>
            </sectionelement>
            <sectionelement>
                *<packagesection id="264679052" navTitle="Test5">*
                    <sectionelement>
                        <topicref format="xml" href="c16f1fca2-31ed-4973-b201-a5a089c788a6"
                            id="264679826" sequence="12" type="story">
                            **<story contenttype="News" id="c16f1fca2-31ed-4973-b201-a5a089c788a6">**
                                <slug>test story stringer3</slug>
                                <storyTitlealts>
                                    <headline>test story stringer3</headline>
                                </storyTitlealts>
                                <lead>test story stringer3</lead>
                                <storybody>
                                    <p id="c5d5c883-d860-46fa-b035-64c13a5fcd23">test story stringer3</p>
                                </storybody>
                            </story>
                        </topicref>
                    </sectionelement>
                </packagesection>
            </sectionelement>
        </packagesection>
    </sectionelement>
</package>

1 个答案:

答案 0 :(得分:0)

我尝试使用以下xpath并且它有效。感谢Paul的帮助!!

//sectionelement/packagesection[@navTitle="Gas Daily"]/sectionelement/packagesection[descendant::story[@id=$id] or descendant::analytic[@id=$id]]