0

My xml contains two cases

Case 1:

<section>
            <title>Order Sections</title>
            <para>This function describes. </para>
</section>

Case 2:

<section>
            Band mode is Order
            <p outputclass="termtesttext">In this case a fixed order bandwidth.</p>
        </section>

I want the output to be:

Case 1:

<division>
                <title>Order Sections</title>
                <para>This function describes. </para>
    </division>

Case 2:

<division>
            <title>Band mode is Order</title>
            <para>In this case a fixed order bandwidth.</para>
        </division>

I have used identity transform in the beginning to copy everything.

1 Answer 1

1

Would this work for you:

<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="section">
    <division>
        <xsl:apply-templates select="node()"/>
    </division>
</xsl:template>

<xsl:template match="section/text()">
    <title>
        <xsl:value-of select="normalize-space(.)"/>
    </title>
</xsl:template>

<xsl:template match="section/p">
    <para>
       <xsl:apply-templates/>
    </para>
</xsl:template>

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

1 Comment

This works. Thank you. Would you also please recommend a complete guide for xslt preferably available online. I am fairly new to it.

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.