我想使用xslt选择字符串的一部分。
投入:
p:endnote_bl1
p:endnote_bl2
p:endnote_bl3
p:endnote_bl4产出应是:
endnote_bl1
endnote_bl2
endnote_bl3
endnote_bl4我能用斯普利特还是别的什么来做这个吗?我正在使用XSLT2.0
发布于 2019-07-30 05:11:13
使用替换函数
replace('p:endnote_bl1', '^[^:]*:', '')发布于 2019-07-30 05:36:45
你可以简单地做:
substring-after($string, ':')发布于 2019-07-31 03:39:48
有多种方法可以做到这一点。
此转换显示了四种不同的解决方案,其中前两种是XPath 1.0 (XSLT1.0)解决方案:
<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文档时(没有提供任何!)
<t>
<s>p:endnote_bl1</s>
<s>p:endnote_bl2</s>
<s>p:endnote_bl3</s>
<s>p: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>
==================
<t>
<s>endnote_bl1</s>
<s>endnote_bl2</s>
<s>endnote_bl3</s>
<s>endnote_bl4</s>
</t>https://stackoverflow.com/questions/57264500
复制相似问题