0

わんぱくな Web 開発者によって書かれたドキュメントがあります。次のようなものです。

<div id="details">
    Here is some text without a p tag. Oh, let's write some more.
    <br>
    <br>
    And some more.
    <table id="non-unique">
        ...
    </table>
    Replaces the following numbers:
    <table id="non-unique">
        ... good stuff in here
    </table>
</div>

だから、それはうまくマークアップされていません。適切なものを含むテーブルを取得する必要がありますが、一意の id 値がなく、常に同じ順序であるは限らず、div などで最後になるとは限りません。

唯一の実行中のテーマは、常にテキストに従うことですReplaces the following numbers:が、このテキストは上記の例のようになっている場合もあれば、h4要素内にある場合もあります!

XPath 式を使用して、置換文字列を検索し、次のテーブル要素を要求して、このテーブルを整理することはできますか??

ありがとう!

4

3 に答える 3

1

使用:

//node()[self::h4 or self::text()]
         [normalize-space() = 'Replaces the following numbers:']
           /following-sibling::*[1][self::table]

XSLT ベースの検証:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:copy-of select=
   "//node()[self::h4 or self::text()]
             [normalize-space() = 'Replaces the following numbers:']
               /following-sibling::*[1][self::table]
   "/>
 </xsl:template>
</xsl:stylesheet>

この変換が提供されたドキュメント(整形式の XML ドキュメントになるように修正) に適用されると、次のようになります。

<div id="details">
 Here is some text without a p tag. Oh, let's write some more.
    <br />
    <br />
    And some more.     
    <table id="non-unique">
     ...
  </table>
  Replaces the following numbers:
    <table id="non-unique">
    ... good stuff in here
    </table>
</div>

XPath 式が評価され、選択されたノードが出力にコピーされます。

<table id="non-unique">
    ... good stuff in here
    </table>

この XML ドキュメントに同じ変換 (XPath 式) を適用すると、次のようになります。

<div id="details">
 Here is some text without a p tag. Oh, let's write some more.
    <br />
    <br />
    And some more.     
    <table id="non-unique">
     ...
  </table>
  <h4>Replaces the following numbers:</h4>
    <table id="non-unique">
    ... good stuff in here
    </table>
</div>

もう一度、必要な要素が選択されて出力されます。

<table id="non-unique">
    ... good stuff in here
    </table>
于 2012-06-26T04:42:09.997 に答える
1

それは私には有効に見えます:

//text()[contains(.,"Replaces the following numbers")]/following-sibling::table[1]

ID が一意でなければならないという規則はありません。

于 2012-06-25T22:05:57.043 に答える
-1

いいえ、XPath を実行するには整形式の Xml が必要なためです。

参照。この回答、追加情報を提供します。

于 2012-06-25T14:16:13.593 に答える