String 在XSLT中选择字符串的一部分
我想使用xslt选择字符串的一部分 输入:String 在XSLT中选择字符串的一部分,string,xslt,xslt-2.0,String,Xslt,Xslt 2.0,我想使用xslt选择字符串的一部分 输入: p:endnote_bl1 p:endnote_bl2 p:endnote_bl3 p:endnote_bl4 输出应为: endnote_bl1 endnote_bl2 endnote_bl3 endnote_bl4 我可以用split或其他方法来完成吗?我正在使用XSLT2.0使用替换函数 replace('p:endnote_bl1', '^[^:]*:', '') 你可以做的很简单: substring-after($string, ':
p:endnote_bl1
p:endnote_bl2
p:endnote_bl3
p:endnote_bl4
输出应为:
endnote_bl1
endnote_bl2
endnote_bl3
endnote_bl4
我可以用split或其他方法来完成吗?我正在使用XSLT2.0使用替换函数
replace('p:endnote_bl1', '^[^:]*:', '')
你可以做的很简单:
substring-after($string, ':')
有很多方法可以做到这一点 此转换显示了四种不同的解决方案,前两种是XPath 1.0(XSLT 1.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>
p:尾注(bl1)
p:尾注(bl2)
p:尾注(bl3)
p:尾注(bl4)
我们得到四个相同、正确的结果,每个结果由四种解决方案中的一种产生:
<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>
(完)
尾注(二)
尾注(三)
(完)
==================
(完)
尾注(二)
尾注(三)
(完)
==================
(完)
尾注(二)
尾注(三)
(完)
==================
(完)
尾注(二)
尾注(三)
(完)