user1146822
user1146822

Reputation:

XSLT transforming data using choose

I have an XML Document, I want to use XSLT to change the formatting of the data.

XML Values

<UniqueName>XXX</UniqueName>
<Parent>XYZ</Parent>
<Name>ABC</Name>

Anytime there is an XXX in Uniquename I want to change Uniquename value to 123

I tried the below however It seems to add another column to the XML rather than transforming the XXX to 123

 <th style="text-align:left">UniqueName</th>
 <th style="text-align:left">Parent</th>
 <th style="text-align:left">Name</th>

 </tr>
 <xsl:for-each select="units/row">
 <tr>


 <Review> 
 <td><xsl:value-of select="UniqueName"/></td> 
 <xsl:if test="UniqueName= 'XXX'"><text>123</text> 
 </xsl:if>  
 </Review> 

What do i need to change to get the values I want

Upvotes: 0

Views: 32

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117073

As your title says, you need to use xsl:choose, rather than xsl:if. Try it this way:

<xsl:for-each select="units/row">
    <tr>
        <td>
            <xsl:choose>
                <xsl:when test="UniqueName='XXX'">
                    <xsl:text>123</xsl:text> 
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="UniqueName"/>
                </xsl:otherwise>
            </xsl:choose>
        </td> 
        <!-- more cells ... -->
    </tr>   
</xsl:for-each>

Note the difference between <text> and <xsl:text>. And I am not sure why you need the <Review> element in the middle of a table.

Upvotes: 1

Related Questions