0

Am writing some xslt script (version 1.0) in BizTalk mapper, VS2010

Now in input xml file I have the following tags

<STUDENTS>
<STUDENT>&lt;DETAILS NAME="Tuna"&gt;These are student. details. of Student1.&lt;/DETAILS&gt;</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=&quot;'),'&quot;'))" />
  </xsl:attribute>
  <xsl:variable name="replace1" select="normalize-space(substring-before(substring-after(.,'&gt;'),'&lt;/DETAILS&gt;'))" />  
<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.

Nikki
  • 31
  • 6
  • possible duplicate of [Removing the last characters in an XSLT string](http://stackoverflow.com/questions/1119449/removing-the-last-characters-in-an-xslt-string) – Tomalak Feb 17 '14 at 12:30

2 Answers2

1

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) = '.'))"/> 
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Clever, I like it :-) – Ian Roberts Feb 17 '14 at 13:34
  • @IanRoberts Oh, gosh. I am sure you meant that as a compliment, but I swore I'd stop doing "clever" and "cute trick". Evidently I had a relapse. I'll edit my answer and post a less "clever" and more straightforward approach. – michael.hor257k Feb 17 '14 at 16:19
0

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.

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=&quot;'),'&quot;'))" />
         </xsl:attribute>
         <xsl:variable name="replace1" select="normalize-space(substring-before(substring-after(.,'&gt;'),'&lt;/DETAILS&gt;'))" />  
         
         <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>
Elikill58
  • 4,050
  • 24
  • 23
  • 45
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75