smr5
smr5

Reputation: 2793

Add Space in the XSLT V 1.0

I have two selects statements in the XSLT file. What I'm trying to accomplish is to add a space in the output of the select statement.

For example, the following code:

<xsl:value-of select="translate(CAudioFile/CRI/PrivateData/PrivateData/DictionaryEntry[Key='CD1']/Value, ',',';')" />

returns this output:

9702195481; 31201(CCC AGENT)

I want to add a space right before the opening parenthesis. So the output would be:

9702195481; 31201 (CCC AGENT)

I tried to use the "translate" function but it only allows two inputs.

The second select statement:

<xsl:for-each select="/CAudioFile/Agent/GroupsList/int"><xsl:value-of select="@name"/></xsl:for-each> 

returns

53115(ABCDE City IIM)

I would like to add a space before the first parenthesis. So the output is:

53115 (ABCDE City IIM)

Any suggestions?

Thank you

EDIT:

When I use the following code, it works 99% of the time and it gives me the output I want:

<xsl:variable name="cd1"    select="CAudioFile/CRI/PrivateData/PrivateData/DictionaryEntry[Key='CD1']/Value" />
<xsl:value-of select="translate(substring-before($cd1, '('), ',', ';')"/>
<xsl:text> (</xsl:text>
<xsl:value-of select="substring-after($cd1, '(')"/> 
<xsl:text>, </xsl:text>

The output is:

9702195481; 31201 (CCC AGENT)

However, there're times that the code will return a slightly different output:

9286136438; 31172 (GCG Agent), 18887113613

I want this code to replace EVERY comma with semicolon, the output from this string looks like this:

9286136438; 31172 (GCG Agent); 18887113613

This way I can keep them under one column.

Thank you so much for your help.

Upvotes: 0

Views: 960

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 117100

You can't use translate to replace one character with two. Try it this way:

<xsl:variable name="value" select="CAudioFile/CRI/PrivateData/PrivateData/DictionaryEntry[Key='CD1']/Value" />

<xsl:value-of select="translate(substring-before($value, '('), ',', ';')"/>
<xsl:text> (</xsl:text>
<xsl:value-of select="substring-after($value, '(')"/>

or, if you prefer:

<xsl:value-of select="concat(translate(substring-before($value, '('), ',', ';'), ' (', substring-after($value, '('))"/>

Upvotes: 2

helderdarocha
helderdarocha

Reputation: 23627

Placing your value in a variable so it will be easier to reuse:

<xsl:variable name="string"  
              select="translate(CAudioFile/CRI/PrivateData/PrivateData/DictionaryEntry[Key='CD1']/Value, ',',';')" />

You can use concat() and substring() functions:

concat( substring-before($string, '('), ' (', substring-after($string, '(') )

The ( can be used as a delimiter for the substring functions if it appears not more than once. This works in XSLT 1.0 or +.

Upvotes: 1

Related Questions