views:

42

answers:

2

I want to use XSL to convert XML to another XML

The input xml contains the following element

<ViewSideIndicator>0</ViewSideIndicator>

which need to be converted to the following

<ImageViewDetail ViewSideIndicator="Front"/>

in the input file, if the value is "0", then it should be "Front" in the output and if the value is "1", then it should be "Back" in the output

I know that we can use <xsl:choose> to make the value based on a decision, but i'm not sure how to do it for this case.

A: 

Do you mean something like this (or a version of it as a template)?

<ImageViewDetail>
 <xsl:choose>
  <xsl:when test="ViewSideIndicator=0">
   <xsl:attribute name="ViewSideIndicator">Front Gray</xsl:attribute>
  </xsl:when>
  <xsl:otherwise>
   <xsl:attribute name="ViewSideIndicator">Back Gray</xsl:attribute>
  </xsl:otherwise>
 </xsl:choose>
</ImageViewDetail>
Rich
+1  A: 

In the template (assuming that the current source context is the ViewSideIndicator element):

<ImageViewDetail>
    <xsl:attribute name="ViewSideIndicator">
        <xsl:choose>
            <xsl:when test="text()='0'">Front</xsl:when>
            <xsl:when test="text()='1'">Back</xsl:when>
        </xsl:choose>
    </xsl:attribute>
</ImageViewDetail>
Lucero
+1. Better yet, use `(. = 0)` and `(. = 1)` for comparisons - it's both shorter, and will correctly handle any number representation (e.g. `00` and `01`), which is likely to be what is wanted here.
Pavel Minaev