for example I have this xml:
<ns5:folio><ns5:portfolioName ns5:portfolioNameScheme="test">ROOT:B1:B2:B3:B4:B5:B6</ns5:portfolioName></ns5:folio>
And I want the xslt to find the string after the 4th : and before the 5th : which is B4.
How can I do this?
Thanks.开发者_如何学运维
Look at substring-before
and substring-after
.
<xsl:variable name="x">ROOT:B1:B2:B3:B4:B5:B6</xsl:variable>
<xsl:value-of select="substring-before(substring-after(substring-after(substring-after(substring-after($x, ':'), ':'), ':'), ':'), ':')"/>
In XSLT/XPath 2.0, you can use tokenize()
:
tokenize($x, ':')[5]
Besides the good answer by @LarsH (+1), if you are stuck with XSLT 1.0, you can use the str-split-to-words
function/template of FXSL:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common">
<xsl:import href="strSplit-to-Words.xsl"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="vwordNodes">
<xsl:call-template name="str-split-to-words">
<xsl:with-param name="pStr" select="/"/>
<xsl:with-param name="pDelimiters"
select="':'"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="ext:node-set($vwordNodes)/*[5]"/>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document (with added namespace definition to make it well-formed):
<ns5:folio xmlns:ns5="unknown!!!" >
<ns5:portfolioName ns5:portfolioNameScheme="test">ROOT:B1:B2:B3:B4:B5:B6</ns5:portfolioName>
</ns5:folio>
the wanted, correct result is produced:
B4
Do note: This solution is very easy to use (just change [5]
to [1000]
) in case we need to retrieve the 1000-th component in the colon-separated string.
精彩评论