0

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>

2 Answers 2

2

How about simply:

XSLT 1.0

<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:strip-space elements="*"/>

<xsl:template match="/response">
    <Body>
        <Response>
            <output>
                <xsl:copy-of select="Fields"/>
            </output>
        </Response>
    </Body>
</xsl:template>

</xsl:stylesheet>

Added:

how can I rename the FieldName element to fieldName

Like this:

<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:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="/response">
    <Body>
        <Response>
            <output>
                <xsl:apply-templates/>
            </output>
        </Response>
    </Body>
</xsl:template>

<xsl:template match="FieldName">
    <fieldName>
        <xsl:apply-templates/>
    </fieldName>
</xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Michael, how can I rename the FieldName element to fieldName after the transformation?
See the addition to my answer.
1

See this example: http://www.w3schools.com/xsl/

Print the top level tags once and the Field tags once for each FieldName tag

<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="/">
             <Body>
                <Response>
                    <output>
                        <Fields>
                        <xsl:for-each select="response/Fields/Field/FieldName">
                            <Field>
                                <FieldName>
                                    <xsl:value-of select="."/>
                                </FieldName>
                            </Field>
                        </xsl:for-each>
                        </Fields>
                    </output>
                </Response>
            </Body>
    </xsl:template>
</xsl:stylesheet>

1 Comment

Himanshu, how can I rename the FieldName element to fieldName after the transformation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.