About us
Products
Services
Articles
Contact us

Mixed Transformer

This example uses SAXDOMIX to apply an XSLT transformation to each DOM tree created during the mixed SAX-DOM parsing of an XML document. All SAX events that aren't used to build DOM trees are copied to the resulted XML document.

Download Java source code, compiled classes and full documentation.

We have the following MixedTransformer.xml file:

    <?xml version="1.0" encoding="utf-8"?>

    <root>
        <elem1 wantDOM="true">
            <!-- comment1 -->
            <elem11>text11</elem11>
            <elem12><![CDATA[cdata12]]></elem12>
            <elem13>
                <elem131/>
                <elem132/>
            </elem13>
            <?proc1 data?>
        </elem1>
        <elem2>
            <!-- comment2 -->
            <elem21 wantDOM="true">data21</elem21>
            <elem22 wantDOM="true"><![CDATA[cdata22]]></elem22>
            <elem23>text23</elem23>
            <?proc2 data?>
        </elem2>
    </root>

We want the apply the transformation instructions contained by a file called MixedTransformer.xsl to all XML fragments that start with an element that has a wantDOM="true" attribute. The content of MixedTransformer.xsl is included below:

    <?xml version="1.0" encoding="utf-8"?>

    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

        <xsl:output cdata-section-elements="elem12 elem22"/>

        <xsl:template match="elem1">
            <NEW-elem1>
                <xsl:comment> NEW<xsl:value-of select="comment()"/></xsl:comment>
                <NEW-elem11>NEW <xsl:value-of select="elem11"/></NEW-elem11>
                <NEW-elem12><xsl:value-of select="concat('NEW ', elem12)"/></NEW-elem12>
                <NEW-elem13>
                    <NEW-elem131/>
                    <NEW-elem132/>
                </NEW-elem13>
                <xsl:processing-instruction name="NEW-proc1"> NEW <xsl:value-of
                    select="processing-instruction()"/></xsl:processing-instruction>
            </NEW-elem1>
        </xsl:template>

        <xsl:template match="elem21">
            <NEW-elem21>NEW <xsl:value-of select="."/></NEW-elem21>
        </xsl:template>

        <xsl:template match="elem22">
            <NEW-elem22><xsl:value-of select="concat('NEW ', .)"/></NEW-elem22>
        </xsl:template>

    </xsl:stylesheet>

Execute this command:

    java com.devsphere.examples.xml.saxdomix.MixedTransformer
    -xsl MixedTransformer.xsl -in MixedTransformer.xml

You'll get the following output:

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
        <NEW-elem1>
            <!-- NEW comment1 -->
            <NEW-elem11>NEW text11</NEW-elem11>
            <NEW-elem12>NEW cdata12</NEW-elem12>
            <NEW-elem13>
                <NEW-elem131/>
                <NEW-elem132/>
            </NEW-elem13>
            <?NEW-proc1 NEW data?>
        </NEW-elem1>
        <elem2>
            <!-- comment2 -->
            <NEW-elem21>NEW data21</NEW-elem21>
            <NEW-elem22>NEW cdata22</NEW-elem22>
            <elem23>text23</elem23>
            <?proc2 data?>
        </elem2>
    </root>

Copyright © 2000-2020 Devsphere

About us
Products
Services
Articles
Contact us