5

'Company'というテキストを持つ正確なノードに到達したいと思います。このノードに到達したら、このノードの直後にある次のテキストノードに到達したいと思います。これには会社名が含まれているためです。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を使用してCompanyタグ//*[text()= 'Company'] に到達しました。次のテキストノードに到達したいと思います。私のXMLは動的です。<dd>そのため、会社の価値を取得するためのようにノードタイプをハードコーディングすることはできません。ただし、これは、値がすぐ隣のテキストノードにあることを確認するためです。

では、テキストがCompanyであるノードの直後にテキストノードに到達するにはどうすればよいですか?

4

4 に答える 4

3

ノードのどの部分もハードコーディングできない場合following-sibling、xpathは次のようになります。

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

目的のテキストが常に。のような別の要素で囲まれていると仮定しますspan


特定のテキストをテストdtするには、xpathを次のように変更します

//*[text()='Company' or text()='Company:' or text()='Company Name']/following::*/*/text()
于 2012-06-08T07:49:16.900 に答える
0

//*[text()='Company']/following-sibling::dd次のddを取得するために使用します。

そのddの条件を挿入して、さらに進んでいくこともできます。 following-sibling::elementName要件を満たす同じ親レベルで次の兄弟を探すだけです。上記のように条件がない場合、「会社」の次のddが取得されます。

テキストはスパン内にあるので、試してみてください

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

もう1つの明確な例は、現在選択されている「会社」の次の業界テキストも取得したいとします。

持っている//*[text()='Company'

次のように変更できます。//*[text()='Company']/following-sibling::dt[text()='Industries']/dd/span

もちろん、text()の値をハードコーディングする代わりに、変数を使用できます。

于 2012-06-08T07:42:09.490 に答える
0

XPathNavigatorを使用して、すべてのノードタイプに1つずつ進むことができます

XPathNavigator::MoveToNextがあなたが探しているメソッドだと思います。

サンプルコードもあります 。http://msdn.microsoft.com/en-us/library/9yxc3x24.aspx

于 2012-06-08T08:03:07.233 に答える
0

静的に未知のマークアップ要素でラップされている場合でも、必要なテキストノードを選択するこの一般的なXPath式を使用します

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

この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>

正確に必要なテキストノードが選択されます:

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
于 2012-06-08T12:34:29.953 に答える