xslt - 使用 Xpath 和 XSLT 选择特定字符串
问题描述
我想在<
和之间选择字符串>
。
输入 :
<p type="Endnote Text"><p:endnote_bl1>This is a bullet list in an endnote</p>
<p type="Endnote Text"><p:endnote_bl2>This is a bullet list in an endnote</p>
<p type="Endnote Text"><p:endnote_bl3>This is a bullet list in an endnote</p>
我想p:endnote_bl1,p:endnote_bl2, etc..
从文本中选择。<
它表示和之间的任何文本>
。我怎样才能为此编写 XPath。
解决方案
在 XSLT 中,使用 xpath,您可以简单地选择所有p
元素(或tps:p
元素,如果您有命名空间),并使用substring-before
andsubstring-after
提取文本,但请注意,这假设每个<
和>
<xsl:for-each select="//p[@type='Endnote Text']">
<xsl:value-of select="substring-before(substring-after(., '<'), '>')" />
<xsl:text> </xsl:text>
</xsl:for-each>
在http://xsltfiddle.liberty-development.net/bnnZX7上查看它的实际应用
如果您可以使用 XSLT 2.0,则无需xsl:for-each
...
<xsl:value-of select="//p[@type='Endnote Text']/substring-before(substring-after(., '<'), '>')" separator=" " />
或者您也可以replace
在 XSLT 2.0 中使用......
<xsl:value-of select="//p[@type='Endnote Text']/replace(., '<(.+)>.*', '$1')" separator=" " />
推荐阅读
- jquery - jQuery 在另一个图标上切换(脉动)一个图标
- javascript - Vue Draggable 移动到上一个列表
- dart - 有没有办法调用 4 个 API,然后创建一个列表并绘制一个饼图
- c# - 如何检查我的双变量是否为整数?
- excel - 无法在 Excel VBA 中迭代函数返回的对象
- django - 如何在模板中调用模型方法
- google-cloud-dataflow - 如何在预定义模板中禁用公共 IP 以启动数据流作业
- php - 是否可以从 html 表单操作中调用 codeigniter 控制器功能
- php - 使用存储过程将值从控制器传递到模型 CodeIginter
- java - 嵌套异常是 java.util.concurrent.ExecutionException