Is there a way to determine if a xsl:matching-substring
is the last()
matching substring?
Example data:
<data>
<value>1 A 1 2 B 2 1 C 3</value>
</data>
Example XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output media-type="xhtml" encoding="UTF-8" />
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>xsl:matching-substring problem</title>
</head>
<body>
<h1>xsl:matching-substring problem</h1>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="//value">
<xsl:element name="p" namespace="http://www.w3.org/1999/xhtml">
<xsl:analyze-string select="." regex="\p{{Nd}}\s\p{{Lu}}\s\p{{Nd}}" flags="i">
<xsl:matching-substring>
<xsl:value-of select="."/>
<xsl:if test="not(last())">
<xsl:element name="br" namespace="http://www.w3.org/1999/xhtml" />
</xsl:if />
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Result:
<?xml version="1.0" encoding="UTF-8"?><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="xhtml; charset=UTF-8" />
<title>xsl:matching-substring problem</title>
</head>
<body>
<h1>xsl:matching-substring problem</h1>
<p>1 A 12 B 21 C 3</p>
</body>
</html>
So, it seems last()
is true also for the first and the second matching substring (in fact, last()
seems to return the position of the last character of the matching substring).
Without the xsl:if
, i get:
<?xml version="1.0" encoding="UTF-8"?><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="xhtml; charset=UTF-8" />
<title>xsl:matching-substring problem</title>
</head>
<body>
<h1>xsl:matching-substring problem</h1>
<p>1 A 1<br />2 B 2<br />1 C 3<br /></p>
</body>
</html>
But i would prefer <p>1 A 1<br />2 B 2<br />1 C 3</p>
. Is there a way to achieve this using XSLT 2.0? (Im using Saxon HE as xslt engine).