加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – 如何应用XPath函数“substring-after”

发布时间:2020-12-16 07:59:59 所属栏目:百科 来源:网络整理
导读:什么是XPath表达式,我将用于获取每个书的“HarryPotter:”后面的字符串。 即。给定这个XML: bookstorebook HarryPotter:Chamber of Secrets /bookbook HarryPotter:Prisoners in Azkabahn /book/bookstore 我会回来: Chamber of SecretsPrisoners in Azk
什么是XPath表达式,我将用于获取每个书的“HarryPotter:”后面的字符串。

即。给定这个XML:

<bookstore>
<book>
  HarryPotter:Chamber of Secrets 
</book>
<book>
  HarryPotter:Prisoners in Azkabahn 
</book>
</bookstore>

我会回来:

Chamber of Secrets
Prisoners in Azkabahn

我已经尝试过这样的事情:

/bookstore/book/text()[substring-after(.,'HarryPotter:')]

我认为我的语法不正确…

在XPath 2.0中,这可以通过单个XPath表达式生成:

/ * / * / substring-after(。,’HarryPotter:’)

在这里,我们使用XPath 2.0的非常强大的功能,在位置步骤的末尾,我们可以放置一个函数,并且该函数将应用于当前结果集中的所有节点。

在XPath 1.0中,没有这样的功能,这在一个XPath表达式中无法实现。

我们可以执行如下的XSLT转换:

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

    <xsl:template match="/">
      <xsl:for-each select="/*/*[substring-after(.,'HarryPotter:')]">
        <xsl:value-of select=
         "substring-after(.,'HarryPotter:')"/>
        <xsl:text>&#xA;</xsl:text>
      </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>  

应用于原始XML文档时:

<bookstore>
    <book>  HarryPotter:Chamber of Secrets </book>
    <book>  HarryPotter:Prisoners in Azkabahn </book>
</bookstore>

这个转换产生了想要的结果:

密室阿兹卡班的囚犯

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读