首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在XSLT中选择字符串的一部分

在XSLT中选择字符串的一部分
EN

Stack Overflow用户
提问于 2019-07-30 04:53:25
回答 3查看 1.1K关注 0票数 1

我想使用xslt选择字符串的一部分。

投入:

代码语言:javascript
运行
复制
p:endnote_bl1
p:endnote_bl2
p:endnote_bl3
p:endnote_bl4

产出应是:

代码语言:javascript
运行
复制
endnote_bl1
endnote_bl2
endnote_bl3
endnote_bl4

我能用斯普利特还是别的什么来做这个吗?我正在使用XSLT2.0

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-07-30 05:11:13

使用替换函数

代码语言:javascript
运行
复制
replace('p:endnote_bl1', '^[^:]*:', '')
票数 1
EN

Stack Overflow用户

发布于 2019-07-30 05:36:45

你可以简单地做:

代码语言:javascript
运行
复制
substring-after($string, ':')
票数 1
EN

Stack Overflow用户

发布于 2019-07-31 03:39:48

有多种方法可以做到这一点。

此转换显示了四种不同的解决方案,其中前两种是XPath 1.0 (XSLT1.0)解决方案:

代码语言:javascript
运行
复制
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vSolution1">
   <xsl:apply-templates select="/*" mode="substring-after"/>
 </xsl:variable>

 <xsl:variable name="vSolution2">
   <xsl:apply-templates select="/*" mode="substring"/>
 </xsl:variable>

 <xsl:variable name="vSolution3">
   <xsl:apply-templates select="/*" mode="split"/>
 </xsl:variable>

 <xsl:variable name="vSolution4">
   <xsl:apply-templates select="/*" mode="replace"/>
 </xsl:variable>

  <xsl:template match="/">
    <xsl:copy-of select="$vSolution1"/>
    ==================
    <xsl:copy-of select="$vSolution2"/>
    ==================
    <xsl:copy-of select="$vSolution3"/>
    ==================
    <xsl:copy-of select="$vSolution4"/>

  </xsl:template>

  <xsl:template match="node()|@*" mode="#all">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*" mode="#current"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="s/text()" mode="substring-after">
    <xsl:value-of select="substring-after(., 'p:')"/>
  </xsl:template>

  <xsl:template match="s/text()" mode="substring">
    <xsl:value-of select="substring(., 3)"/>
  </xsl:template>

  <xsl:template match="s/text()" mode="split">
    <xsl:value-of select="tokenize(., ':')[2]"/>
  </xsl:template>

  <xsl:template match="s/text()" mode="replace">
    <xsl:value-of select="replace(., '^.+:', '')"/>
  </xsl:template>
</xsl:stylesheet>

当将此转换应用于下面的XML文档时(没有提供任何!)

代码语言:javascript
运行
复制
<t>
    <s>p:endnote_bl1</s>
    <s>p:endnote_bl2</s>
    <s>p:endnote_bl3</s>
    <s>p:endnote_bl4</s>
</t>

我们得到了四个相同的、正确的结果,每个结果都是由四种解决方案中的一种产生的:

代码语言:javascript
运行
复制
<t>
      <s>endnote_bl1</s>
      <s>endnote_bl2</s>
      <s>endnote_bl3</s>
      <s>endnote_bl4</s>
</t>
    ==================
    <t>
      <s>endnote_bl1</s>
      <s>endnote_bl2</s>
      <s>endnote_bl3</s>
      <s>endnote_bl4</s>
</t>
    ==================
    <t>
      <s>endnote_bl1</s>
      <s>endnote_bl2</s>
      <s>endnote_bl3</s>
      <s>endnote_bl4</s>
</t>
    ==================
    <t>
      <s>endnote_bl1</s>
      <s>endnote_bl2</s>
      <s>endnote_bl3</s>
      <s>endnote_bl4</s>
</t>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57264500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档