Andrew Truckle
Andrew Truckle

Reputation: 19147

Trimming specific characters from an XML node value in XSL?

This is some XML:

<Test>This is some text</Test>
<Test>This is some text:</Test>
<Test>This is some text :</Test>

Using XSL 1.0 is it possible to get the value and trim from the right any " " and ":" character?

Thank you.

Upvotes: 1

Views: 77

Answers (1)

Flynn1179
Flynn1179

Reputation: 12075

Here's a quick & dirty way:

<xsl:template match="Test/text()">
  <xsl:value-of select="normalize-space(translate(.,':',''))"/>
</xsl:template>

This will cause problems for any elements that have a : or double spaces in the middle or a leading space, but if you don't have anything like that, it should work.

This removes any colons by replacing them with nothing, then does 'normalize-space', which trims trailing, leading and double spaces from text.

Upvotes: 1

Related Questions