R
ravi.velamuri
Hi,
1. My source XML will almost look like attached "Source.XML"
2. I have to transform that into Destination XML almost look like
attached"Dest.xml"
Here, my destination XML should be always in the same sequence of
elements as it is in sample shown whatever way the Source XML
comes...OR XSL transforms it...
Please advice how to I achieve it...One rude way is write for each
Column Details one For loop...which is worst, I know in all ways.
Your early advice will help me a lot. (let me know if any further
clarification required on my prob).
Ravi Velamuri.
Source.xml:
<?xml version="1.0"?>
<TEST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:///d:sample.xsd">
<Table_Info>
<Table_Name>TBLGS_COUNTRY</Table_Name>
<Operation_Type>I</Operation_Type>
<Prev_Seq>190952</Prev_Seq>
<Curr_Seq/>
</Table_Info>
<Table_Data>
<Col_Details>
<Col_Name>GS_CURR</Col_Name>
<Col_Value>JPY</Col_Value>
</Col_Details>
<Col_Details>
<Col_Name>GS_CTRY_ABBR</Col_Name>
<Col_Value/>
</Col_Details>
<Col_Details>
<Col_Name>GS_CTRY</Col_Name>
<Col_Value>CTRY attribute</Col_Value>
</Col_Details>
<Col_Details>
<Col_Name>GS_CTRY_DES</Col_Name>
<Col_Value>JAPAN</Col_Value>
</Col_Details>
<Col_Details>
<Col_Name>BLB_ISO_CTRY</Col_Name>
<Col_Value>JP</Col_Value>
</Col_Details>
<Col_Details>
<Col_Name>BLB_ISO_CTRY_DES</Col_Name>
<Col_Value/>
</Col_Details>
<Col_Details>
<Col_Name>BLB_CDR_CTRY</Col_Name>
<Col_Value/>
</Col_Details>
</Table_Data>
<Error_Info>
<Error_Msg1/>
<Error_Msg2/>
</Error_Info>
</TEST>
XSL transform logic:
<?xml version='1.0' ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:abc="http://www.abc.com/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xslutput method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template name="country">
<xsl:for-each select="TEST/Table_Data/Col_Details">
<xsl:if test="Col_Name = 'BLB_ISO_CTRY'">
<test:code>
<xsl:value-of select="Col_Value"/>
</test:code>
</xsl:if>
<xsl:if test="Col_Name= 'GS_CTRY_DES'">
<test:name>
<xsl:value-of select="Col_Value"/>
</test:name>
</xsl:if>
<xsl:if test="Col_Name = 'GS_REGION_CODE'">
<test:timezone>
<xsl:value-of select="Col_Value"/>
</test:timezone>
</xsl:if>
<xsl:if test="Col_Name = 'GS_CTRY'">
<attribute>
<test:name>
GS_CTRY
</test:name>
<test:value>
<xsl:value-of select="Col_Value"/>
</test:value>
</attribute>
</xsl:if>
<xsl:if test="Col_Name = 'GS_CURR'">
<attribute>
<test:name>
GS_CURR
</test:name>
<test:value>
<xsl:value-of select="Col_Value"/>
</test:value>
</attribute>
</xsl:if>
</xsl:for-each>
<test:enabled> true </test:enabled>
</xsl:template>
</xsl:stylesheet>
Dest.xml:
<?xml version="1.0" encoding="UTF-8"?>
<test:testDocument
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:abc="http:/
/www.abc.com/xml">
<test:testObject test:type="TBLGS_COUNTRY" action="I">
<test:Attribute>
<test:name>GS_CTRY</test:name>
<test:value>949</test:value>
</test:Attribute>
<test:Attribute>
<test:name>GS_CURR</test:name>
<test:value/>
</test:Attribute>
<test:name/>
<test:code/>
<test:timezone/>
<test:enabled>true</test:enabled>
<test:activeFrom>1970-01-01+00:00</test:activeFrom>
<test:activeTo>2100-01-01+00:00</test:activeTo>
<test:holiday/>
</test:testObject>
</test:testDocument>
1. My source XML will almost look like attached "Source.XML"
2. I have to transform that into Destination XML almost look like
attached"Dest.xml"
Here, my destination XML should be always in the same sequence of
elements as it is in sample shown whatever way the Source XML
comes...OR XSL transforms it...
Please advice how to I achieve it...One rude way is write for each
Column Details one For loop...which is worst, I know in all ways.
Your early advice will help me a lot. (let me know if any further
clarification required on my prob).
Ravi Velamuri.
Source.xml:
<?xml version="1.0"?>
<TEST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:///d:sample.xsd">
<Table_Info>
<Table_Name>TBLGS_COUNTRY</Table_Name>
<Operation_Type>I</Operation_Type>
<Prev_Seq>190952</Prev_Seq>
<Curr_Seq/>
</Table_Info>
<Table_Data>
<Col_Details>
<Col_Name>GS_CURR</Col_Name>
<Col_Value>JPY</Col_Value>
</Col_Details>
<Col_Details>
<Col_Name>GS_CTRY_ABBR</Col_Name>
<Col_Value/>
</Col_Details>
<Col_Details>
<Col_Name>GS_CTRY</Col_Name>
<Col_Value>CTRY attribute</Col_Value>
</Col_Details>
<Col_Details>
<Col_Name>GS_CTRY_DES</Col_Name>
<Col_Value>JAPAN</Col_Value>
</Col_Details>
<Col_Details>
<Col_Name>BLB_ISO_CTRY</Col_Name>
<Col_Value>JP</Col_Value>
</Col_Details>
<Col_Details>
<Col_Name>BLB_ISO_CTRY_DES</Col_Name>
<Col_Value/>
</Col_Details>
<Col_Details>
<Col_Name>BLB_CDR_CTRY</Col_Name>
<Col_Value/>
</Col_Details>
</Table_Data>
<Error_Info>
<Error_Msg1/>
<Error_Msg2/>
</Error_Info>
</TEST>
XSL transform logic:
<?xml version='1.0' ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:abc="http://www.abc.com/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xslutput method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template name="country">
<xsl:for-each select="TEST/Table_Data/Col_Details">
<xsl:if test="Col_Name = 'BLB_ISO_CTRY'">
<test:code>
<xsl:value-of select="Col_Value"/>
</test:code>
</xsl:if>
<xsl:if test="Col_Name= 'GS_CTRY_DES'">
<test:name>
<xsl:value-of select="Col_Value"/>
</test:name>
</xsl:if>
<xsl:if test="Col_Name = 'GS_REGION_CODE'">
<test:timezone>
<xsl:value-of select="Col_Value"/>
</test:timezone>
</xsl:if>
<xsl:if test="Col_Name = 'GS_CTRY'">
<attribute>
<test:name>
GS_CTRY
</test:name>
<test:value>
<xsl:value-of select="Col_Value"/>
</test:value>
</attribute>
</xsl:if>
<xsl:if test="Col_Name = 'GS_CURR'">
<attribute>
<test:name>
GS_CURR
</test:name>
<test:value>
<xsl:value-of select="Col_Value"/>
</test:value>
</attribute>
</xsl:if>
</xsl:for-each>
<test:enabled> true </test:enabled>
</xsl:template>
</xsl:stylesheet>
Dest.xml:
<?xml version="1.0" encoding="UTF-8"?>
<test:testDocument
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:abc="http:/
/www.abc.com/xml">
<test:testObject test:type="TBLGS_COUNTRY" action="I">
<test:Attribute>
<test:name>GS_CTRY</test:name>
<test:value>949</test:value>
</test:Attribute>
<test:Attribute>
<test:name>GS_CURR</test:name>
<test:value/>
</test:Attribute>
<test:name/>
<test:code/>
<test:timezone/>
<test:enabled>true</test:enabled>
<test:activeFrom>1970-01-01+00:00</test:activeFrom>
<test:activeTo>2100-01-01+00:00</test:activeTo>
<test:holiday/>
</test:testObject>
</test:testDocument>