13

結果の表があり、次のようになります。

<table>
  <thead>
    <tr>
      <th>Id</th>
      <th>Type</th>
      <th>Amount</th>
      <th>Price</th>
      <th>Name</th>
      <th>Expiration</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>123</td>
      <td>Paper</td>
      <td>10 pcs.</td>
      <td>$10</td>
      <td>Premium Copier paper</td>
      <td>None</td>
    </tr>
    <tr>
      <td>321</td>
      <td>Paper</td>
      <td>20 pcs.</td>
      <td>$20</td>
      <td>Extra Copier paper</td>
      <td>None</td>
    </tr>
  </tbody>

そして、xpathを使用してその名前で列全体を選択したいと思います。たとえば{<td>$10</td>, <td>$20</td>}、列名「Price」で選択した場合、返される結果を配列にします。私はxpathを初めて使用し、これを行う方法がよくわかりませんが、それが可能であると確信しています。

4

3 に答える 3

29

わかりました、私は十分で非常にエレガントに見える答えを見つけました。必要なXPath文字列は次のとおりです。

//table/tbody/tr/td[count(//table/thead/tr/th[.="$columnName"]/preceding-sibling::th)+1]

$columnNameの代わりに列の名前を入力します。これは私にとってはうまくいきます。XSLなどはなく、純粋なxpath文字列だけです。それをどのように適用するか-それは別の質問です。

于 2013-02-11T06:52:19.843 に答える
1

このXPathを使用できます。

/table/tbody/tr/td[count(preceding-sibling::td)+1 = count(ancestor::table/thead/tr/th[.='Price']/preceding-sibling::th)+1]

関連する位置に対しての位置(position())をテストすることはうまくいくと思いますが、私がテストしていたときはそうではなかったようです。tdth

于 2013-02-07T07:25:04.517 に答える
1

解決策を見つけた場合は、ここに回答として投稿することをお勧めしますが、楽しみのために、これが私がこれにアプローチする方法です:

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

  <xsl:key name="kCol" match="td" use="count(. | preceding-sibling::td)"/>
  <xsl:param name="column" select="'Price'" />

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/">
    <found>
      <xsl:apply-templates select="table/thead/tr/th" />
    </found>
  </xsl:template>

  <xsl:template match="th">
    <xsl:if test=". = $column">
      <xsl:apply-templates select="key('kCol', position())" />
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

パラメータ値として「Price」を使用して実行した場合:

<found>
  <td>$10</td>
  <td>$20</td>
</found>

パラメータ値として「名前」を使用して実行する場合:

<found>
  <td>Premium Copier paper</td>
  <td>Extra Copier paper</td>
</found>
于 2013-02-09T05:19:20.490 に答える