Reputation: 52366
I have this string (which is stored in an XElement):
<MergeFields xmlns="urn:www-xxx-com:schema.xx-calls">
<MergeField name="XAccountID" value="1234" />
<MergeField name="XDate" value="01/20/2013 10:00:00 AM" />
</MergeFields>
Mergefields will be storing different attributes.
I need to convert it to a string like this:
<MergeFields>
<XAccountID>1234</XAccountID>
<XDate>01/20/2013 10:00:00</XDate>
</MergeFields>
I have read about using XSLT, but I'm having a hard time finding sample code. How can I do this?
Upvotes: 0
Views: 63
Reputation: 8058
I believe there are some good XSLT tutorials on IBM's DeveloperWorks website. I'd recommend reading those; they should include examples.
Generally, the right answer is to start with the Identity Transform. Then add a template which handles the exceptional cases.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Identity: Copy all nodes unchanged, recursively -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Exception: Attributes of MergeFields should be turned into elements
with the same name and value -->
<xsl:template match="MergeFields/@*">
<xsl:element name="name()"><xsl:value-of select="."/></xsl:element>
</template>
</xsl:stylesheet>
Upvotes: 2