Showing posts with label xslt. Show all posts
Showing posts with label xslt. Show all posts

Thursday, May 6, 2021

How to make a quick parse a XLST to XML ( quick fix in C#) (public xml)?

Introduction: The principle of parsing a XLST is to know xpath a the core of several sistems so if can change the supersition of xml to xslt and doing marshalling correctly you can integrate a web service correctly.

Development The online xlst parser have a problem of copyright so instead of that you can vote for you own library, buy a license of altova, or use a language to do a quick parse, everythin is not lost so de copy right should be existing the web.
Body Part
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Xsl;
namespace XSLTtransformXML
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnValidarXSLT_Click(object sender, EventArgs e)
{
validarXSLT();
}
private Boolean validarXSLT()
{
String xml = rtboxXSLT.Text;
Boolean resultado = false;
using (XmlReader xr = XmlReader.Create(
new StringReader(xml)))
{
try
{
while (xr.Read()) { }
this.rtxtResultado.Text = ("Validacion de XSLT, Exito");
this.rtxtResultado.ForeColor = Color.Black;
resultado = true;
}
catch (Exception ex)
{
this.rtxtResultado.Text = ("Validacion de XSLT, Fallo: " + ex.Message);
this.rtxtResultado.ForeColor = Color.Red;
}
}
return resultado;
}
private void btnValidarXML_Click(object sender, EventArgs e)
{
validarXML();
}
private Boolean validarXML() {
String xml = rtboxXML.Text;
Boolean resultado = false;
using (XmlReader xr = XmlReader.Create(new StringReader(xml)))
{
try
{
while (xr.Read()) { }
this.rtxtResultado.Text = ("Validacion de XML, Exito");
this.rtxtResultado.ForeColor = Color.Black;
resultado = true;
}
catch (Exception ex)
{
this.rtxtResultado.Text = ("Validacion de XML, Fallo: " + ex.Message);
this.rtxtResultado.ForeColor = Color.Red;
}
}
return resultado;
}
private void btnTransformar_Click(object sender, EventArgs e)
{
try
{
if (validarXSLT() && validarXML()) {
String inputXML = rtboxXML.Text;
String transformXSL = rtboxXSLT.Text;
XslCompiledTransform proc = new XslCompiledTransform();
using (StringReader sr = new StringReader(transformXSL))
{
using (XmlReader xr = XmlReader.Create(sr))
{
proc.Load(xr);
}
}
String resultXML="";
using (StringReader sr = new StringReader(inputXML))
{
using (XmlReader xr = XmlReader.Create(sr))
{
using (StringWriter sw = new StringWriter())
{
proc.Transform(xr, null, sw);
resultXML = sw.ToString();
}
}
}
if (!String.IsNullOrEmpty(resultXML))
{
this.rtxtResultado.Text = (resultXML);
this.rtxtResultado.ForeColor = Color.Black;
}
else {
this.rtxtResultado.Text = ("Transformacion, no retorno ningun resultado ");
this.rtxtResultado.ForeColor = Color.Blue;
}
}
}
catch (Exception ex)
{
this.rtxtResultado.Text = ("Transformacion, Fallo: " + ex.Message+" , "+ex.StackTrace);
this.rtxtResultado.ForeColor = Color.Red;
}
}
private void llblXMLEjemplo_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><anagrafica><testata><nomemercato id=\"007\">Mercato di test</nomemercato><data>Giovedi 18 dicembre 2003 16.05.29</data></testata><record><codice_cliente>5</codice_cliente><rag_soc>Miami American Cafe</rag_soc><codice_fiscale>IT07654930130</codice_fiscale><indirizzo tipo=\"casa\">Viale Carlo Espinasse 5, Como</indirizzo><num_prodotti>13</num_prodotti></record></anagrafica>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
StringWriter sw = new StringWriter();
XmlTextWriter tx = new XmlTextWriter(sw);
tx.Formatting = Formatting.Indented;
doc.WriteTo(tx);
rtboxXML.Text = sw.ToString();
}
private void llblXSLTEjemplo_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns=\"http://www.w3.org/1999/xhtml\" version=\"1.0\"><xsl:output encoding=\"UTF-8\" indent=\"yes\" method=\"xml\" standalone=\"no\" omit-xml-declaration=\"no\"/><xsl:template match=\"anagrafica\"><html><body><h1><font color=\"red\"><xsl:value-of select=\"testata/nomemercato/@id\"/></font><xsl:value-of select=\"testata/nomemercato\"/></h1><h2><xsl:value-of select=\"testata/data\"/></h2><hr/><table bgcolor=\"yellow\" border=\"1\"><tbody><tr><th>Cod. Cliente</th><th>Ragione Sociale</th><th>Codice Fiscale/P.Iva</th><th>Indirizzo</th></tr><xsl:for-each select=\"record\"><tr><td><xsl:value-of select=\"codice_cliente\"/></td><td><xsl:value-of select=\"rag_soc\"/></td><td><xsl:value-of select=\"codice_fiscale\"/></td><td><xsl:value-of select=\"indirizzo\"/></td></tr></xsl:for-each></tbody></table></body></html></xsl:template></xsl:stylesheet>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
StringWriter sw = new StringWriter();
XmlTextWriter tx = new XmlTextWriter(sw);
tx.Formatting = Formatting.Indented;
doc.WriteTo(tx);
rtboxXSLT.Text = sw.ToString();
}
}
}
view raw form1.cs hosted with ❤ by GitHub
Main Part
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace XSLTtransformXML
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
view raw main.cs hosted with ❤ by GitHub
Example xlst:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" version="1.0">
<xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="no" omit-xml-declaration="no"/>
<xsl:template match="anagrafica">
<html>
<body>
<h1>
<font color="red">
<xsl:value-of select="testata/nomemercato/@id"/>
</font>
<xsl:value-of select="testata/nomemercato"/>
</h1>
<h2>
<xsl:value-of select="testata/data"/>
</h2>
<hr/>
<table bgcolor="yellow" border="1">
<tbody>
<tr>
<th>Cod. Cliente</th>
<th>Ragione Sociale</th>
<th>Codice Fiscale/P.Iva</th>
<th>Indirizzo</th>
</tr>
<xsl:for-each select="record">
<tr>
<td>
<xsl:value-of select="codice_cliente"/>
</td>
<td>
<xsl:value-of select="rag_soc"/>
</td>
<td>
<xsl:value-of select="codice_fiscale"/>
</td>
<td>
<xsl:value-of select="indirizzo"/>
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
view raw xslt1.xslt hosted with ❤ by GitHub
Example XML:
<?xml version="1.0" encoding="UTF-8"?>
<anagrafica>
<testata>
<nomemercato id="007">Mercato di test</nomemercato>
<data>Giovedi 18 dicembre 2003 16.05.29</data>
</testata>
<record>
<codice_cliente>5</codice_cliente>
<rag_soc>Miami American Cafe</rag_soc>
<codice_fiscale>IT07654930130</codice_fiscale>
<indirizzo tipo="casa">Viale Carlo Espinasse 5, Como</indirizzo>
<num_prodotti>13</num_prodotti>
</record>
<record>
<codice_cliente>302</codice_cliente>
<rag_soc>Filiberto Gilardi</rag_soc>
<codice_fiscale>IT87654770157</codice_fiscale>
<indirizzo tipo="ufficio">Via Biancospini 20, Messina</indirizzo>
<num_prodotti>8</num_prodotti>
</record>
<record>
<codice_cliente>1302</codice_cliente>
<rag_soc>Eidon</rag_soc>
<codice_fiscale>IT887511231</codice_fiscale>
<indirizzo tipo="ufficio">Via Bassini 17/2, Milano</indirizzo>
<num_prodotti>18</num_prodotti>
</record>
<record>
<codice_cliente>202</codice_cliente>
<rag_soc>SkillNet</rag_soc>
<codice_fiscale>IT887642131</codice_fiscale>
<indirizzo tipo="ufficio">Via Chiasserini 11A, Milano</indirizzo>
<num_prodotti>24</num_prodotti>
</record>
<record>
<codice_cliente>12</codice_cliente>
<rag_soc>Eidon</rag_soc>
<codice_fiscale>IT04835710965</codice_fiscale>
<indirizzo tipo="casa">Via Cignoli 17/2, Roma</indirizzo>
<num_prodotti>1112</num_prodotti>
</record>
<record>
<codice_cliente>5</codice_cliente>
<rag_soc>Miami American Cafe</rag_soc>
<codice_fiscale>IT07654930130</codice_fiscale>
<indirizzo tipo="casa">Viale Carlo Espinasse 5, Como</indirizzo>
<num_prodotti>13</num_prodotti>
</record>
<record>
<codice_cliente>302</codice_cliente>
<rag_soc>Filiberto Gilardi</rag_soc>
<codice_fiscale>IT87654770157</codice_fiscale>
<indirizzo tipo="ufficio">Via Biancospini 20, Messina</indirizzo>
<num_prodotti>8</num_prodotti>
</record>
<record>
<codice_cliente>1302</codice_cliente>
<rag_soc>Eidon</rag_soc>
<codice_fiscale>IT887511231</codice_fiscale>
<indirizzo tipo="ufficio">Via Bassini 17/2, Milano</indirizzo>
<num_prodotti>18</num_prodotti>
</record>
<record>
<codice_cliente>202</codice_cliente>
<rag_soc>SkillNet</rag_soc>
<codice_fiscale>IT887642131</codice_fiscale>
<indirizzo tipo="ufficio">Via Chiasserini 11A, Milano</indirizzo>
<num_prodotti>24</num_prodotti>
</record>
<record>
<codice_cliente>202</codice_cliente>
<rag_soc>SkillNet</rag_soc>
<codice_fiscale>IT887642131</codice_fiscale>
<indirizzo tipo="ufficio">Via Chiasserini 11A, Milano</indirizzo>
<num_prodotti>24</num_prodotti>
</record>
<record>
<codice_cliente>12</codice_cliente>
<rag_soc>Eidon</rag_soc>
<codice_fiscale>IT04835710965</codice_fiscale>
<indirizzo tipo="casa">Via Cignoli 17/2, Roma</indirizzo>
<num_prodotti>1112</num_prodotti>
</record>
</anagrafica>
view raw XML1.xml hosted with ❤ by GitHub
Online Hipotesis https://github.com/osanchezh/xsltransformutil-app



Conclusion We pray for the freedom of Java and Apache to regain the freedom of open source!!!
FREEDOM
To be CONTINUED...