1

I know there is already a question out there about replacing strings in XSLT, but I need a conditional statement to replace one string with multiple variables.

Here is my code:

<xsl:template name="section-01">
  <xsl:call-template name="table-open"/>
  <xsl:text disable-output-escaping="yes">&lt;table style="text-align=center;"&gt;</xsl:text>  
  <xsl:call-template name="display-gen">
    <xsl:with-param name="value" select="./z30-collection"/>
    <xsl:with-param name="width" select="'30%'"/>
  </xsl:call-template>
  <xsl:call-template name="display-gen">
    <xsl:with-param name="value" select="./call-no-piece-01"/>
    <xsl:with-param name="width" select="'30%'"/>
  </xsl:call-template>
  <xsl:call-template name="table-close"/>
</xsl:template>

I need a statement to replace "./z30-collection"

If ./z30-collection = "Deposit" replace with "DEP"
if ./z30-collection = "General" replace with "GEN" 
if ./z30-collection = "Storage" replace with "STORE"

etc...

Any help would be greatly appreciated!

2
  • You probably mean just string output (not replacement). Or which string do you want to replace? It is not clear from your description. Commented Sep 30, 2013 at 9:42
  • By the way. Check <xsl:choose> element. w3schools.com/xsl/xsl_choose.asp Commented Sep 30, 2013 at 9:45

2 Answers 2

1

The most "XSLT" way to approach something like this is to define different templates for the different cases

<xsl:template match="z30-collection[. = 'Deposit']">
  <xsl:text>DEP</xsl:text>
</xsl:template>
<xsl:template match="z30-collection[. = 'General']">
  <xsl:text>GEN</xsl:text>
</xsl:template>
<xsl:template match="z30-collection[. = 'Storage']">
  <xsl:text>STORE</xsl:text>
</xsl:template>
<!-- catch-all for elements that don't have any of the three specific values -->
<xsl:template match="z30-collection">
  <xsl:value-of select="." />
</xsl:template>

and then when you need the value you do

<xsl:apply-templates select="z30-collection"/>

and the template matcher will automatically pick out the most specific template that applies to this particular case. There's no need for any explicit conditional checks, the matcher takes care of that for you.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is the XSLT function which will work similar to the String.Replace()

This template has the 3 Parameters as below

text :- your main string

replace :- the string which you want to replace

by :- the string which will reply by new string

refer http://exslt.org/str/functions/replace/index.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.