0
<time-layout time-coordinate="local" summarization="none">
      <layout-key>k-p24h-n7-1</layout-key>
      <start-valid-time>2013-10-30T08:00:00-07:00</start-valid-time>
      <end-valid-time>2013-10-30T20:00:00-07:00</end-valid-time>
      <start-valid-time>2013-10-31T08:00:00-07:00</start-valid-time>
      <end-valid-time>2013-10-31T20:00:00-07:00</end-valid-time>
      <start-valid-time>2013-11-01T08:00:00-07:00</start-valid-time>
      <end-valid-time>2013-11-01T20:00:00-07:00</end-valid-time>
      <start-valid-time>2013-11-02T08:00:00-07:00</start-valid-time>
      <end-valid-time>2013-11-02T20:00:00-07:00</end-valid-time>
      <start-valid-time>2013-11-03T07:00:00-08:00</start-valid-time>
      <end-valid-time>2013-11-03T19:00:00-08:00</end-valid-time>
      <start-valid-time>2013-11-04T07:00:00-08:00</start-valid-time>
      <end-valid-time>2013-11-04T19:00:00-08:00</end-valid-time>
      <start-valid-time>2013-11-05T07:00:00-08:00</start-valid-time>
      <end-valid-time>2013-11-05T19:00:00-08:00</end-valid-time>
    </time-layout>
    <time-layout time-coordinate="local" summarization="none">
      <layout-key>k-p24h-n6-2</layout-key>
      <start-valid-time>2013-10-30T20:00:00-07:00</start-valid-time>
      <end-valid-time>2013-10-31T09:00:00-07:00</end-valid-time>
      <start-valid-time>2013-10-31T20:00:00-07:00</start-valid-time>
      <end-valid-time>2013-11-01T09:00:00-07:00</end-valid-time>
      <start-valid-time>2013-11-01T20:00:00-07:00</start-valid-time>
      <end-valid-time>2013-11-02T09:00:00-07:00</end-valid-time>
      <start-valid-time>2013-11-02T19:00:00-07:00</start-valid-time>
      <end-valid-time>2013-11-03T08:00:00-08:00</end-valid-time>
      <start-valid-time>2013-11-03T19:00:00-08:00</start-valid-time>
      <end-valid-time>2013-11-04T08:00:00-08:00</end-valid-time>
      <start-valid-time>2013-11-04T19:00:00-08:00</start-valid-time>
      <end-valid-time>2013-11-05T08:00:00-08:00</end-valid-time>
    </time-layout>
    <time-layout time-coordinate="local" summarization="none">
      <layout-key>k-p3h-n35-3</layout-key>
      <start-valid-time>2013-10-30T11:00:00-07:00</start-valid-time>
      <start-valid-time>2013-10-30T14:00:00-07:00</start-valid-time>
      <start-valid-time>2013-10-30T17:00:00-07:00</start-valid-time>
      <start-valid-time>2013-10-30T20:00:00-07:00</start-valid-time>
      <start-valid-time>2013-10-30T23:00:00-07:00</start-valid-time>
      <start-valid-time>2013-10-31T02:00:00-07:00</start-valid-time>
      <start-valid-time>2013-10-31T05:00:00-07:00</start-valid-time>
      <start-valid-time>2013-10-31T08:00:00-07:00</start-valid-time>
      <start-valid-time>2013-10-31T11:00:00-07:00</start-valid-time>
      <start-valid-time>2013-10-31T14:00:00-07:00</start-valid-time>
      <start-valid-time>2013-10-31T17:00:00-07:00</start-valid-time>
      <start-valid-time>2013-10-31T20:00:00-07:00</start-valid-time>
      <start-valid-time>2013-10-31T23:00:00-07:00</start-valid-time>
      <start-valid-time>2013-11-01T02:00:00-07:00</start-valid-time>
      <start-valid-time>2013-11-01T05:00:00-07:00</start-valid-time>
      <start-valid-time>2013-11-01T08:00:00-07:00</start-valid-time>
      <start-valid-time>2013-11-01T11:00:00-07:00</start-valid-time>
      <start-valid-time>2013-11-01T14:00:00-07:00</start-valid-time>
      <start-valid-time>2013-11-01T17:00:00-07:00</start-valid-time>
      <start-valid-time>2013-11-01T23:00:00-07:00</start-valid-time>
      <start-valid-time>2013-11-02T05:00:00-07:00</start-valid-time>
      <start-valid-time>2013-11-02T11:00:00-07:00</start-valid-time>
      <start-valid-time>2013-11-02T17:00:00-07:00</start-valid-time>
      <start-valid-time>2013-11-02T23:00:00-07:00</start-valid-time>
      <start-valid-time>2013-11-03T04:00:00-08:00</start-valid-time>
      <start-valid-time>2013-11-03T10:00:00-08:00</start-valid-time>
      <start-valid-time>2013-11-03T16:00:00-08:00</start-valid-time>
      <start-valid-time>2013-11-03T22:00:00-08:00</start-valid-time>
      <start-valid-time>2013-11-04T04:00:00-08:00</start-valid-time>
      <start-valid-time>2013-11-04T10:00:00-08:00</start-valid-time>
      <start-valid-time>2013-11-04T16:00:00-08:00</start-valid-time>
      <start-valid-time>2013-11-04T22:00:00-08:00</start-valid-time>
      <start-valid-time>2013-11-05T04:00:00-08:00</start-valid-time>
      <start-valid-time>2013-11-05T10:00:00-08:00</start-valid-time>
      <start-valid-time>2013-11-05T16:00:00-08:00</start-valid-time>
    </time-layout>

現在、Java で XPath を使用して XML を解析しています。この 3 つのブロックが表示されるまでは、すべて問題ありません。私を悩ませているのは、<time-layout time-coordinate="local" summarization="none">それぞれ異なる種類の情報を内部に持つ3つに出会ったことです. これに対する私の現在の「ハック」は、それぞれの名前locallocal1local2、およびlocal3に変更しているため、 を介して XPath で解析し/dwml/data/time-layout[@time-coordinate='local3']/start-valid-time/text()ます。これは一種のハックですが、この XML がプルされているサイトから XML を直接読み取ることになるので、その方法を使用したくないか、またはそのハックを使用できない可能性があります。似たような名前の要素が 3 つあるため、属性の 1 つの名前を変更せずに、3 番目の要素だけを XPath で解析するにはどうすればよいでしょうか?

ありがとう!

PS<layout-key>値は毎回異なるため、パーサーにハードコードすることはできません。

4

2 に答える 2

1

述語を使用します。

//time-layout[3]
于 2013-10-30T20:28:00.193 に答える
1

<end-valid-time>xpath 述語にが存在するかどうかを確認できます。存在しない場合は、3 番目の要素を見ています。

//time-layout[not(./end-valid-time)]

これもちょっとしたハックですが、少なくともすべて xpath に含まれています。

于 2013-10-30T20:22:24.820 に答える