XPath可以获得节点的所有子节点的连接视图吗?我正在寻找类似于JQuery .html()方法的东西。如何连接XPath中节点的所有子节点的内容?
举例来说,如果我有下面的XML:
<h3 class="title">
<span class="content">this</span>
<span class="content"> is</span>
<span class="content"> some</span>
<span class="content"> text</span>
</h3>
我想上一个XPath查询“H3 [@类=‘标题’]”这会给我“这是一些文本”。
这是一个真正的问题,但如果更多的上下文/背景有帮助,这里是:我正在使用XPath,并且我使用this post来帮助我编写一些复杂的XSL。我的源XML看起来像这样。
<h3 class="title">Title</h3>
<p>
<span class="content">Some</span>
<span class="content"> text</span>
<span class="content"> for</span>
<span class="content"> this</span>
<span class="content"> section</span>
</p>
<p>
<span class="content">Another</span>
<span class="content"> paragraph</span>
</p>
<h3 class="title">
<span class="content">Title</span>
<span class="content"> 2</span>
<span class="content"> is</span>
<span class="content"> complex</span>
</h3>
<p>
<span class="content">Here</span>
<span class="content"> is</span>
<span class="content"> some</span>
<span class="content"> text</span>
</p>
我的输出XML考虑每个<h3>
以及所有<p>
标签,直到下一个<h3>
。我写的XSL如下:
<xsl:template match="h3[@class='title']">
...
<xsl:apply-templates select="following-sibling::p[
generate-id(preceding-sibling::h3[1][@class='title'][text()=current()/text()])
=
generate-id(current())
]"/>
...
</xsl:template>
的问题是,我使用text()
方法来识别是相同的3H公司。在上面的例子中,“Title 2 is complex”标题的text()方法返回空白。我的想法是使用像JQuery的.html这样的方法,将返回我“标题2是复杂的”。
更新:这可能有助于澄清。转换之后,上述所需的输出会是这个样子:
<section>
<title>Title</title>
<p>
<content>Some</content>
<content> text</content>
<content> for</content>
<content> this</content>
<content> section</content>
</p>
<p>
<content>Another</content>
<content> paragraph</content>
</p>
</section>
<section>
<title>
<content>Title</content>
<content> 2</content>
<content> is</content>
<content> complex</content>
</title>
<p>
<content>Here</content>
<content> is</content>
<content> some</content>
<content> text</content>
</p>
</section>
已添加完整样式表以响应添加完整的所需输出。 – 2010-06-09 13:43:38
这是一个方便的工具(对于任何其他人有类似的任务)http://codebeautify.org/Xpath-Tester – Rimian 2015-09-18 11:21:46