修飾子ノード値の文字列連結を返すXPath
-
05-07-2019 - |
質問
要素の特定の修飾子ノードの連結値を含む文字列値を返すが、他を無視するXPath式形式を誰でも提案してください:
<div>
This text node should be returned.
<em>And the value of this element.</em>
And this.
<p>But this paragraph element should be ignored.</p>
</div>
返される値は単一の文字列でなければなりません:
This text node should be returned. And the value of this element. And this.
これは単一のXPath式で可能ですか?
ありがとう。
解決
XPath 1.0の場合:
使用できます
/div//text()[not(parent::p)]
必要なテキストノードをキャプチャします。連結自体はXPath 1.0では実行できません。ホストアプリケーションで実行することをお勧めします。
他のヒント
XPath 2.0の場合:
string-join(/ * / node()[not(self :: p)]、 '')
/div//text()
二重スラッシュは、中間ノードに関係なくテキストを抽出します
動作するこの外観:
コンテキストとして使用 / div /
:
text() | em/text()
またはコンテキストを使用しない場合:
/div/text() | /div/em/text()
最初の2つの文字列を連結する場合は、これを使用します:
concat(/div/text(), /div/em/text())
p以外のすべての子が必要な場合は、次を試すことができます...
string-join(//*[name() != 'p']/text(), "")
これは戻る...
This text node should be returned.
And the value of this element.
And this.
for-eachループも使用して、このような変数に値を組み立てることができます
<xsl:variable name="newstring">
<xsl:for-each select="/div//text()">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:variable>
所属していません StackOverflow