Search code examples
xmlxsltapache-axis

how to collect a particular attribute from a existing file and merge to another file in linux


could anybody tell me how to read xml attribute using xslt for file comparison.

brief description

there are numbers of <transportReceiver> tags are there in old xml, i need to read all and update to new xml .

<transportReceiver name="http"                  class="org.apache.axis2.transport.http.AxisServletListener">
    <parameter name="port">8080</parameter>        
</transportReceiver>

basically i want to read <transportReceiver> tag and make a collection and append to new axis2.xml file . for this i am using xslt. I have created a xsl file for this .

    <?xml version="1.0" encoding="utf-8"?>
<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:variable name="axis2.xml" select="document('axis2.xml')" />



<xsl:template match="/transportReceiver">

</xsl:template>

<xsl:template match="object">
    <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>

i need some help to collect collection of <transportReceiver>


Solution

  • Below is an approach on how this can be achieved. Let us assume First.xml file contains the <transportReceiver> node along with other nodes.

    First.xml

    <root>
        <otherNode>
            <otherNodeValue>XXXX</otherNodeValue>
        </otherNode>
        <someOtherNode>
            <someOtherNodeValue>YYYY</someOtherNodeValue>
        </someOtherNode>
        <transportReceiver name="http"  class="org.apache.axis2.transport.http.AxisServletListener">
            <parameter name="port">8080</parameter>
        </transportReceiver>
    </root>
    

    The Second.xml is the one where the <transportReceiver> node needs to be merged along with its own existing nodes.

    Second.xml

    <rootNode>
        <axisNode>
            <axisValue>AXIS</axisValue>
        </axisNode>
    </rootNode>
    

    The XSLT below when applied to Second.xml, generates the desired output.

    XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes" />
        <xsl:param name="fileName" select="document('First.xml')" />
        <!-- identity transform -->
        <xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="rootNode">
            <xsl:copy>
                <!-- copy required node from First.xml -->
                <xsl:apply-templates select="$fileName/root/transportReceiver" />
                <!-- retain existing nodes of Second.xml as is -->
                <xsl:apply-templates select="@* | node()" />
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
    

    Output

    <rootNode>
        <transportReceiver name="http" class="org.apache.axis2.transport.http.AxisServletListener">
            <parameter name="port">8080</parameter>
        </transportReceiver>
        <axisNode>
            <axisValue>AXIS</axisValue>
        </axisNode>
    </rootNode>