1

コメントまたはその後のテキストから、xpath を使用して値「バンクーバー」を返そうとしています。誰かが私を正しい方向に向けることができますか?

位置 li は常に最初の項目ですが、常に存在するとは限りません。その後のリスト項目の数は項目ごとに異なります。

<item>
<title>
<description>
<!-- Comment #1 -->
<ul class="class1">
<li> <!-- ABC Location=Vancouver -->Location: Vancouver</li>
<li> <!-- More comments -->Text</li>
<li> text</li>
</ul>
</description>
</item>
4

3 に答える 3

2

これは、コメントの後のテキストからそれを引き出します:

substring-after(//ul[@class='class1']/li[position()=1 and contains(.,'Location:')],'Location: ')

<li>これは、'Location:' が含まれている場合にのみ、クラス 'class1' の内部の最初のものを指定し、<ul>'Location:' の後の文字列を取ります。最初の li であるという要件を緩和したい場合は、次のようにします。

substring-after(//ul[@class='class1']/li[contains(.,'Location:')],'Location: ')
于 2012-05-03T19:29:24.293 に答える
0

これは雄弁ではありません。これは静的な解決策であるため、「場所: #####」が構造的に変更された場合に問題が発生する可能性がありますが、上記の場合は機能します。

substring(//item//li[1],12,string-length(//item//li[1])-10)

そして、これはノードではなく、同等の文字列を返します。これを少し急いだので、時間の経過とともにより良い解決策を提供しますが、これは考えるべきことです...(「場所:」を取り除き、その後にあるものを返すだけです..)

于 2012-05-03T19:28:41.743 に答える
0

使用:

  substring-after(/*/description/ul
                       /li[1]/text()[starts-with(., 'Location: ')],
                  'Location: '
                  )

コメントから場所を抽出するには、次を使用します

  substring-after(/*/description/ul
                       /li[1]/comment()[starts-with(., ' ABC Location=')],
                  ' ABC Location='
                  )

XSLT ベースの検証:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
     <xsl:copy-of select=
     "substring-after(/*/description/ul
                           /li[1]/text()[starts-with(., 'Location: ')],
                      'Location: '
                      )
     "/>
==========

     <xsl:copy-of select=
     "substring-after(/*/description/ul
                           /li[1]/comment()[starts-with(., ' ABC Location=')],
                      ' ABC Location='
                      )
     "/>

 </xsl:template>
</xsl:stylesheet>

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

<item>
    <title/>
    <description>
        <!-- Comment #1 -->
        <ul class="class1">
            <li>
                <!-- ABC Location=Vancouver -->Location: Vancouver
            </li>
            <li>
                <!-- More comments -->Text
            </li>
            <li> text</li>
        </ul>
    </description>
</item>

2 つの XPath 式が評価され、評価の結果が出力にコピーされます。

Vancouver

==========

Vancouver 
于 2012-05-04T13:25:06.270 に答える