I'm trying to get different output depending on the input I have. But somehow I am not able to get that. I have different types of inputs & their respective outputs as below. How can I achive this using a single XSL.
input1
<response status="200">
<Fields>
<Field>
<FieldName>ABC</FieldName>
</Field>
<Field>
<FieldName>XYZ</FieldName>
</Field>
</Fields>
</response>
input2
<response status="200">
<Fields>
<Field>
<FieldName>ABC</FieldName>
</Field>
</Fields>
</response>
The output1 gets genarated when I have the response like input1, and output2, when I have response like input2.
output1
<Body>
<Response>
<output>
<Fields>
<Field>
<FieldName>ABC</FieldName>
</Field>
<Field>
<FieldName>XYZ</FieldName>
</Field>
</Fields>
</output>
</Response>
</Body>
output2
<Body>
<Response>
<output>
<Fields>
<Field>
<FieldName>ABC</FieldName>
</Field>
</Fields>
</output>
</Response>
</Body>
So far I am able to manage to get output2 with the below simple xsl, but how to get both the output with a single xsl depending on the input? Any help will be really helpfull. Thanks.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<xsl:if test="/response/Fields/Field">
<Body>
<Response>
<output>
<Fields>
<Field>
<FieldName>
<xsl:value-of select="/response/Fields/Field/FieldName"/>
</FieldName>
</Field>
</Fields>
</output>
</Response>
</Body>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Also how can I change the "FieldName" element name to fieldName after the transformation so that, my output looks like:
output1
<Body>
<Response>
<output>
<Fields>
<Field>
<fieldName>ABC</fieldName>
</Field>
<Field>
<fieldName>XYZ</fieldName>
</Field>
</Fields>
</output>
</Response>
</Body>
output2
<Body>
<Response>
<output>
<Fields>
<Field>
<fieldName>ABC</fieldName>
</Field>
</Fields>
</output>
</Response>
</Body>