Need XSL code to convert dataset to xml file?  
Author Message
Vaish





PostPosted: XML and the .NET Framework, Need XSL code to convert dataset to xml file? Top

Hi

I am looking for XSL code to convert my dataset to xml file.


I have a dataset like below. I want to detect the second element of <Tbl2>, if second element of <Tbl2> is 'APPLE' then all elements within <Tbl2></Tbl2> should be in <Tropical> element of the xml file as below (Pls see sample output xml file).

If 'PEACH' detected then all element should be in <Organic> element of the xml file below.

Please let me know which method I can use (if, choose, when ..etc). XSL CODE WILL BE MORE APPRICIATABLE.

Advance thanks.

Datset

<DataSet>

<Tbl1>

<A>aaa</A>

<B>bbb</B>

</Tbl1>

<Tbl2>

<a1>one</a1>

<a2>APPLE</a2>

<a3>three</a3>

</Tbl2>
<Tbl2>

<a1>one</a1>

<a2>APPLE</a2>

<a3>three</a3>

</Tbl2>

<Tbl2>

<a1>one</a1>

<a2>PEACH</a2>

<a3>three</a3>

</Tbl2>

</DataSet>

Sample out put xml file

<Body>

<Retail>

<book>aaa</book>

<Pen>bbb</Pen>

</Retail>

<Tropical>

<Grade>

<No>one</No>

<Fruit>APPLE</Fruit>

<Qty>three</Qty>

</Grade>
<Grade>

<No>one</No>

<Fruit>APPLE</Fruit>

<Qty>three</Qty>

</Grade>

</Tropical>

<Organic>

<Sample>

<No>one</No>

<Fruit>PEACH</Fruit>

<Qty>three</Qty>

</Sample>

</Organic>

</Body>




.NET Development12  
 
 
Martin Honnen





PostPosted: XML and the .NET Framework, Need XSL code to convert dataset to xml file? Top

You can simply write XPath expressions and match patterns with the condition you have have e.g. Tbl2[a2 = 'APPLE'], here is a sample stylesheet

< xml version="1.0" encoding="UTF-8" >
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" indent="yes"/>

<xsl:strip-space elements="*"/>

<xsl:template match="DataSet">
<Body>
<xsl:apply-templates select="Tbl1"/>
<Tropical>
<xsl:apply-templates select="Tbl2[a2 = 'APPLE']"/>
</Tropical>
<Organic>
<xsl:apply-templates select="Tbl2[a2 = 'PEACH']"/>
</Organic>
</Body>
</xsl:template>

<xsl:template match="Tbl1">
<Retail>
<xsl:apply-templates/>
</Retail>
</xsl:template>

<xsl:template match="A">
<book>
<xsl:apply-templates/>
</book>
</xsl:template>

<xsl:template match="B">
<Pen>
<xsl:apply-templates/>
</Pen>
</xsl:template>

<xsl:template match="Tbl2[a2 = 'APPLE']">
<Grade>
<xsl:apply-templates/>
</Grade>
</xsl:template>

<xsl:template match="Tbl2[a2 = 'PEACH']">
<Sample>
<xsl:apply-templates/>
</Sample>
</xsl:template>

<xsl:template match="a1">
<No>
<xsl:apply-templates/>
</No>
</xsl:template>

<xsl:template match="a2">
<Fruit>
<xsl:apply-templates/>
</Fruit>
</xsl:template>

<xsl:template match="a3">
<Qty>
<xsl:apply-templates/>
</Qty>
</xsl:template>

</xsl:stylesheet>



 
 
Vaish





PostPosted: XML and the .NET Framework, Need XSL code to convert dataset to xml file? Top

Hi

Thanks a lot.I realy appreciate. Thannnkkkk you.

Vaish