是否可以使用XPath获取节点的所有子节点的连接视图?我正在寻找类似JQuery .html()方法的东西.
例如,如果我有以下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>
Run Code Online (Sandbox Code Playgroud)
我想对"h3 [@ class ='title']"进行XPath查询,这会给我"这是一些文字".
这是真正的问题,但如果更多的上下文/背景是有用的,这里是:我使用XPath,我使用这篇文章来帮助我编写一些复杂的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>
Run Code Online (Sandbox Code Playgroud)
我的输出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>
Run Code Online (Sandbox Code Playgroud)
问题是我使用该text()
方法来识别相同的h3.在上面的示例中,"标题2是复杂的"标题的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>
Run Code Online (Sandbox Code Playgroud)
h3[@class='title']/span[@class='content']/text()
Run Code Online (Sandbox Code Playgroud)
像这样?
h3[@class='title']/descendant::*/text()
Run Code Online (Sandbox Code Playgroud)
或这个?
归档时间: |
|
查看次数: |
7768 次 |
最近记录: |