Hello,

I am quite new with XSLT and I need to convert this xml:

<?xml version="1.0" standalone="yes"?>
<assesmentItem>
<choiceInteraction responseIdentifier="correctresponse" title="title" discipline="discipline1" dificulty="dificulty1" timexpected="20" basetype="choice" cardinality="single">
<prompt>answer1</prompt>
<simplechoice0 >response0</simplechoice0>
<simplechoice1 >response1</simplechoice1>
<simplechoice2>response2</simplechoice2>
</choiceInteraction>

</assesmentItem>

into this one:


<?xml version="1.0" standalone="yes"?>
<assesmentItem>

<choiceInteraction responseIdentifier="correctresponse" title="title" discipline="discipline1" dificulty="dificulty1" timexpected="20" basetype="choice" cardinality="single">

<prompt>answer1</prompt>
<simplechoice identifier="choiceA">response1</simplechoice>
<simplechoice identifier="choiceB">response2</simplechoice>
<simplechoice identifier="choiceC">response3</simplechoice>
</choiceInteraction>

</assesmentItem>

Where identifier choiceA would be added for the element simplechoice0 and so on with the others. I have to do the conversion and then the inversion but that one I already have it tough my problem is to do this conversion of attributes and elements.

Thanks so much for your help

Member Avatar for gravyboat
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
    	<assesmentItem>
			<xsl:apply-templates/>
		</assesmentItem>
    </xsl:template>
    
    <xsl:template match="choiceInteraction">
    	<choiceInteraction>
    	    <xsl:copy-of select="@*"/>
		    <xsl:copy-of select="./prompt"/>
		    <xsl:apply-templates/>
    	</choiceInteraction>
    </xsl:template>
    
    <xsl:template match="simplechoice0">
		<simplechoice identifier="choiceA">
			<xsl:value-of select="."/>
		</simplechoice>
    </xsl:template>
    
    <xsl:template match="simplechoice1">
		<simplechoice identifier="choiceB">
			<xsl:value-of select="."/>
		</simplechoice>
    </xsl:template>
    
    <xsl:template match="simplechoice2">
		<simplechoice identifier="choiceC">
			<xsl:value-of select="."/>
		</simplechoice>
    </xsl:template>

	<xsl:template match="text()"/>
</xsl:stylesheet>

If you want the text of the simplechoice elements to change (e.g,. from 'response0' to 'response1') just replace the <xsl:value-of select statement with the actual text you want in each template.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.