Am writing some xslt script (version 1.0) in BizTalk mapper, VS2010
Now in input xml file I have the following tags
<STUDENTS>
<STUDENT><DETAILS NAME="Tuna">These are student. details. of Student1.</DETAILS></STUDENT>
<STUDENT></STUDENT>
</STUDENTS>
Now for each above, output has to look as follows
<INFO NAME="Tuna">These are student. details. of Student1</INFO>
Am using the following script.
<xsl:for-each select="//STUDENTS/STUDENT">
<INFO>
<xsl:attribute name="NAME">
<xsl:value-of select="normalize-space(substring-before(substring-after(.,'NAME="'),'"'))" />
</xsl:attribute>
<xsl:variable name="replace1" select="normalize-space(substring-before(substring-after(.,'>'),'</DETAILS>'))" />
<xsl:value-of select="translate($replace1,'.','')"/>
</INFO>
</xsl:for-each>
My ouput looks like follows
<INFO NAME="Tuna">These are student details of "Student1" </INFO>
But I want remove only "." which appears at end. How do I do that? Any suggestions are really appreciated.
Thanks in advance.
Am writing some xslt script (version 1.0)
If you are using XSLT 1.0, try something like:
<xsl:value-of select="substring($replace1, 1, string-length($replace1) - contains(concat($replace1, '§'), '.§'))"/>
Or, preferably:
<xsl:value-of select="substring($replace1, 1, string-length($replace1) - (substring($replace1, string-length($replace1), 1) = '.'))"/>
EDIT Note that this is an XSLT 2.0 answer. I will delete it if it is of no use at all.
Test if your condition (.
at the end of a string) is met with the matches()
function and a regular expression. You'll find a fiddle of this here.
If matches()
returns true, output a substring of the input text that excludes the last character. In other words, it returns a substring of $replace1
starting from the first character (index 1) and of length string-length() -1
.
Note that I have taken the liberty to remove xsl:for-each
from your stylesheet. Using templates is a better approach in many cases.
Stylesheet
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/STUDENTS">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="STUDENT">
<INFO>
<xsl:attribute name="NAME">
<xsl:value-of select="normalize-space(substring-before(substring-after(.,'NAME="'),'"'))" />
</xsl:attribute>
<xsl:variable name="replace1" select="normalize-space(substring-before(substring-after(.,'>'),'</DETAILS>'))" />
<xsl:choose>
<xsl:when test="matches($replace1,'\.$')">
<xsl:value-of select="substring($replace1,1,string-length($replace1)-1)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$replace1"/>
</xsl:otherwise>
</xsl:choose>
</INFO>
</xsl:template>
</xsl:stylesheet>
Output
<?xml version="1.0" encoding="UTF-8"?>
<STUDENTS>
<INFO NAME="Tuna">These are student. details. of Student1</INFO>
<INFO NAME=""/>
</STUDENTS>