How do I convert an attribute?

10 views
Skip to first unread message

Dave

unread,
Jun 8, 2012, 5:02:04 PM6/8/12
to xs...@googlegroups.com

Hi,


In my source document, I have 


        <answer correctness="0">

                …

        </answer>


and in my result document, I want to output an attribute "isCorrect='false'" if the above "correctness" attribute is equal to "0" and "isCorrect='true'" otherwise.  So the above would appear as


    <answer isCorrect="false">…</answer>


I'm having trouble figuring out how I write the above in template form …


        <xsl:template match="answer">

                <answer isCorrect="???"></answer>

        </xsl:template>


Thanks for any help, - Dave

Christian Wahl

unread,
Jun 12, 2012, 8:16:01 AM6/12/12
to xs...@googlegroups.com
I think it depends on the complete output – are there more differences between the two answer elements? So, my two suggestions:
1:
Create two templates, one for correct and on for incorrect:
<xsl:template match="answer[@correctness = '0']">
   <answer isCorrect="false">…</answer>
</xsl:template>

<xsl:template match="answer[@correctness = '1']">
   <answer isCorrect="true">…</answer>
</xsl:template>

2:
Or just one template with a switch element

<xsl:template match="answer">
   <answer>
      <xsl:choose>
         <xsl:when test="@correctness = '1'">
            <xsl:attribute name="isCorrect" select="'true'"/>
         </xsl:when>
         <xsl:otherwise>
            <xsl:attribute name="isCorrect" select="'false'"/>
         </xsl:otherwise>
      </xsl:choose>
      …
   </answer>
</xsl:template>

Hope it helps
Christian
Reply all
Reply to author
Forward
0 new messages