Thursday, February 20, 2014

XML to XML tranformation using XSLT

In this example I am transforming an XML using XSLT by modifying certain child elements. In coming XML message:
<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="/xsl/error.xsl"?>
<outtask_error>
    <server>
        <name>RSDC-C12</name>
    </server>
    <application>
        <name>Jobs</name>
        <version>2012_06_15</version>
        <prod_status>qa</prod_status>
        <datacenter_location>rtwa3</datacenter_location>
        <request_id>n6dft440-8049-4dff-982a-ee05abc8433</request_id>
    </application>
    <error time="2014-02-06T20:01:05" timezone="Pacific Standard Time">
        <error_id>aab10-825-4a52d-a261-7a845262f</error_id>
        <error_default_source>ICSYC</error_default_source>
        <request_id>b2263a40-8049-4dff-942a-ee05ef3f8433</request_id>
        <error_source>ICSync</error_source>
        <error_description>Connection Entity:abc no DB name is not configured</error_description>
        <stack_trace>at Company.Core.Sync.GetConnectionInfo...
        </stack_trace>
    </error>
    <server_variables>
        <param name="SERVER_NAME">RSDC-C12</param>
        <param name="SCRIPT_NAME">C:\Sync.exe</param>
        <param name="QUERY_STRING">Sync.exe Day1</param>
    </server_variables>
    <Error_Context>
        <param name="databasename">Company_MT</param>
        <param name="error_defaultsource">SYS</param>
        <param name="error_defaultapplicationname">Jobs</param>
        <param name="requestid">b8943a40-1349-4dff-232a-ee05ef523452342f</param>
        <param name="dbservername">fabc-sql02,1420</param>
    </Error_Context>
</outtask_error>
XSLT for the transformation:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml"/>

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

    <xsl:template match="outtask_error/server_variables">
        <server_variables>
            <xsl:for-each select="param">
                <xsl:text disable-output-escaping="yes"><</xsl:text><xsl:value-of select="./@name"/><xsl:text disable-output-escaping="yes">></xsl:text>
                <xsl:value-of select="."/>
                <xsl:text disable-output-escaping="yes"><</xsl:text>/<xsl:value-of select="./@name"/><xsl:text disable-output-escaping="yes">></xsl:text>
            </xsl:for-each>
        </server_variables>
    </xsl:template>

    <xsl:template match="outtask_error/Error_Context">
        <Error_Context>
            <xsl:for-each select="param">
                <xsl:text disable-output-escaping="yes"><</xsl:text><xsl:value-of select="./@name"/><xsl:text disable-output-escaping="yes">></xsl:text>
                <xsl:value-of select="."/>
                <xsl:text disable-output-escaping="yes"><</xsl:text>/<xsl:value-of select="./@name"/><xsl:text disable-output-escaping="yes">></xsl:text>
            </xsl:for-each>
        </Error_Context>
    </xsl:template>

</xsl:stylesheet>
Out going XML:
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="/xsl/error.xsl"?><outtask_error>
    <server>
        <name>RSDC-C12</name>
    </server>
    <application>
        <name>Jobs</name>
        <version>2012_06_15</version>
        <prod_status>qa</prod_status>
        <datacenter_location>rtwa3</datacenter_location>
        <request_id>n6dft440-8049-4dff-982a-ee05abc8433</request_id>
    </application>
    <error time="2014-02-06T20:01:05" timezone="Pacific Standard Time">
        <error_id>aab10-825-4a52d-a261-7a845262f</error_id>
        <error_default_source>ICSYC</error_default_source>
        <request_id>b2263a40-8049-4dff-942a-ee05ef3f8433</request_id>
        <error_source>ICSync</error_source>
        <error_description>Connection Entity:abc no DB name is not configured</error_description>
        <stack_trace>at Company.Core.Sync.GetConnectionInfo...
        </stack_trace>
    </error>
    <server_variables><SERVER_NAME>RSDC-C12</SERVER_NAME><SCRIPT_NAME>C:\Sync.exe</SCRIPT_NAME><QUERY_STRING>Sync.exe Day1</QUERY_STRING></server_variables>
    <Error_Context><databasename>Company_MT</databasename><error_defaultsource>SYS</error_defaultsource><error_defaultapplicationname>Jobs</error_defaultapplicationname><requestid>b8943a40-1349-4dff-232a-ee05ef523452342f</requestid><dbservername>fabc-sql02,1420</dbservername></Error_Context>
</outtask_error>

2 comments:

Unknown said...

Hi, I tried to implement part of your xsl code and doesn't work. For example the line 15 throw me "The content of elements must consist of well-formed character data or markup". Why that could be? Greetings.

Heshan Suriyaarachchi said...

Your XML might be not well formed. Please check whether it is having additional characters such as escape characters.