Lisa
Lisa

Reputation: 77

Transform xml based on child element using xslt

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.

Upvotes: 0

Views: 37

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

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>

Upvotes: 1

Related Questions