【问题标题】:Using XPath expression how can i get the first text node immediately following a node?使用 XPath 表达式我如何获得紧跟节点的第一个文本节点?
【发布时间】:2012-06-08 07:31:56
【问题描述】:

我想找到具有以下文本的确切节点:“公司”。到达该节点后,我想立即到达该节点之后的下一个文本节点,因为它包含公司名称。如何使用 Xpath 做到这一点?

XML 的片段是:

<div id="jobsummary">
    <div id="jobsummary_content">
        <h2>Job Summary</h2>
        <dl>
            <dt>Company</dt>
            <!-- the following element is the one I'm looking for -->
            <dd><span class="wrappable">Pinpoint IT Services, LLC</span></dd>
            <dt>Location</dt>
            <dd><span class="wrappable">Newport News, VA</span></dd>
            <dt>Industries</dt>
            <dd><span class="wrappable">All</span></dd>
            <dt>Job Type</dt>
            <dd class="multipledd"><span class="wrappable">Full Time</span></dd><dd class="multipleddlast"><span class="wrappable"> Employee</span></dd>
        </dl>
    </div>
</div>

我使用以下 xpath 进入公司标签://*[text()= 'Company'] 现在我想进入下一个文本节点。我的 XML 是动态的。所以我不能硬编码像&lt;dd&gt; 这样的节点类型来获得公司价值。但这是肯定的,该值在 立即下一个文本节点中。

那么如何才能在文本为 Company 的节点之后立即到达文本节点?

【问题讨论】:

    标签: xpath


    【解决方案1】:

    如果您无法对 following-sibling 节点的任何部分进行硬编码,您的 xpath 应如下所示:

    //*[text()='Company']/following::*/*/text()
    

    假设所需的文本总是包含在另一个元素中,例如span


    要测试给定的dt 文本,请将您的 xpath 修改为

    //*[text()='Company' or text()='Company:' or text()='Company Name']/following::*/*/text()
    

    【讨论】:

    • 谢谢您的及时回复。上面的表达式非常接近我所需要的。但问题是下一个 text() 标记不一定是兄弟节点。它也可以是子节点或概括任何以下节点。所以我只是稍微修改了你的表达并得到了我需要的东西:我使用了://*[text()='Company']/following::*/*/text()
    • 总是乐于提供帮助。我修改了答案以反映您的最终解决方案。
    • 我的公司标题可以是“公司”或“公司:”或“公司名称”。所以我如何设置 OR 条件来匹配其中任何一个。我不想要使用 contains 因为我想要与其中任何一个完全匹配。我尝试了类似:"//*[text()='Company|Company:|Company Name']/following::*/text()" 但它不起作用。我怎么能做到这一点?在此先感谢:)
    • 您不能将union 运算符放在要匹配的文本中。请参阅我关于如何测试替代值的更新。
    • 感谢一吨的朋友。像魅力一样工作。
    【解决方案2】:

    使用//*[text()='Company']/following-sibling::dd 获取下一个dd。

    您甚至可以为该 dd 插入条件并在其中走得更远。 following-sibling::elementName 只是在同一父级别查找满足您要求的下一个兄弟姐妹。 没有条件,像上面一样,它会得到 'Company' 之后的下一个 dd。

    文本在跨度中,所以你可以试试

    //*[text()='Company']/following-sibling::dd/span

    另一个明确的例子是,假设您还想获取当前所选“公司”的下一个行业文本。

    //*[text()='Company'

    你可以这样修改://*[text()='Company']/following-sibling::dt[text()='Industries']/dd/span

    当然,您可以使用变量,而不是硬编码 text() 的值。

    【讨论】:

      【解决方案3】:

      您可以使用 XPathNavigator 并逐个访问每个节点类型

      我认为 XPathNavigator::MoveToNext 是您正在寻找的方法。

      还有示例代码在.. http://msdn.microsoft.com/en-us/library/9yxc3x24.aspx

      【讨论】:

        【解决方案4】:

        使用这个通用的 XPath 表达式来选择所需的文本节点,即使它包含在静态未知的标记元素中

        (//*[text()='Company']/following-sibling::*[1]//text())[1]
        

        当根据提供的 XML 文档评估此 XPath 表达式时

        <div id="jobsummary">
            <div id="jobsummary_content">
                <h2>Job Summary</h2>
                <dl>
                    <dt>Company</dt>
                    <!-- the following element is the one I'm looking for -->
                    <dd><span class="wrappable">Pinpoint IT Services, LLC</span></dd>
                    <dt>Location</dt>
                    <dd><span class="wrappable">Newport News, VA</span></dd>
                    <dt>Industries</dt>
                    <dd><span class="wrappable">All</span></dd>
                    <dt>Job Type</dt>
                    <dd class="multipledd"><span class="wrappable">Full Time</span></dd><dd class="multipleddlast"><span class="wrappable"> Employee</span></dd>
                </dl>
            </div>
        </div>
        

        正好选择了想要的文本节点

        Pinpoint IT Services, LLC
        

        即使我们将 XML 更改为这个

        <div id="jobsummary">
            <div id="jobsummary_content">
                <h2>Job Summary</h2>
                <div>
                    <p>Company</p>
                    <!-- the following element is the one I'm looking for -->
                    <dd><span class="wrappable"><b><i><u>Pinpoint IT Services, LLC</u></i></b></span></dd>
                    <dt>Location</dt>
                    <dd><span class="wrappable">Newport News, VA</span></dd>
                    <dt>Industries</dt>
                    <dd><span class="wrappable">All</span></dd>
                    <dt>Job Type</dt>
                    <dd class="multipledd"><span class="wrappable">Full Time</span></dd><dd class="multipleddlast"><span class="wrappable"> Employee</span></dd>
                </div>
            </div>
        </div>
        

        上面的 XPath 表达式仍然选择想要的文本节点:

        Pinpoint IT Services, LLC
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-07-19
          • 2011-01-25
          • 2011-11-21
          • 2010-10-10
          • 2021-11-15
          • 2017-01-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多