You are missing an identification template:
<xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template>
I fixed that you answer ..:
<?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="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="/XMLTest/result/*"> <answer> <xsl:value-of select="local-name()"/> </answer> <id> <xsl:value-of select="@answerid"/> </id> <value> <xsl:value-of select="@*"/> </value> </xsl:template> </xsl:stylesheet>
Edit1: the updated template to cancel if the attributes are null: the if condition checks if the attribute is null before converting it to an element.
<xsl:template match="/XMLTest/result/*"> <answer> <xsl:value-of select="local-name()"/> </answer> <xsl:if test="@answerid/.!=''"> <id> <xsl:value-of select="@answerid"/> </id> </xsl:if> <xsl:if test="@*/.!=''"> <value> <xsl:value-of select="@*"/> </value> </xsl:if> </xsl:template>
Edit2: in your early ur attempts, you tried to copy the @ * value, @* indicates the anyname attribute, so he copied the @answerid value (since that was the only attribute available) .. what you had to do .. valu-of="." .. try the code below ..
<xsl:template match="/XMLTest/result/*"> <answer> <xsl:value-of select="local-name()"/> </answer> <xsl:if test="@answerid/.!=''"> <id> <xsl:value-of select="@answerid"/> </id> </xsl:if> <xsl:if test=".!=''"> <value> <xsl:value-of select="."/> </value> </xsl:if> </xsl:template>
source share