473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

XML to CSV and having problems

I'm new to XSLT, but everything I've read says this is the best approach
to solving the XML->CSV problem. Based on some web reading, I came
across the following XSLT code which appears valid, even if it doesn't
work quite as expected.

XSLT:

<?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="text"/>
<xsl:template match="BlockTrade">
ID=<xsl:value-of select="normalize-space(Block_Trade_Id)"/>,
SIDE=<xsl:value-of select="normalize-space(Side)"/>
</xsl:template>
</xsl:stylesheet>
transforming the following XML: (incomplete but works for testing)

<BlockTrades xmlns="http://www.macgregor.com/pbws/eod">
<BlockTrade>
<Block_Trade_Id>1</Block_Trade_Id>
<Side>BUY</Side>
</BlockTrade>
</BlockTrades>

gives me the following output:

1BUY

when I would expect
ID=1, SIDE=BUY

Code snipped that executes the transformation is as follows (C#):

XmlDataDocument xmlDoc = new XmlDataDocument(Trades);
XslTransform xslt = new XslTransform();
xslt.Load(this.Service_Root_Path + @"\PBWS.EOD.xsl");

XmlTextWriter writer = new XmlTextWriter(@"c:\xslt_output.html",
System.Text.Encoding.UTF8);
xslt.Transform(xmlDoc, null, writer, null);
writer.Close();
What am I missing?

Thanks.
-Ben
Nov 12 '05 #1
5 3726
Ben Bloom wrote:
I'm new to XSLT, but everything I've read says this is the best approach
to solving the XML->CSV problem. Based on some web reading, I came
across the following XSLT code which appears valid, even if it doesn't
work quite as expected.

XSLT:

<?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="text"/>
<xsl:template match="BlockTrade">


Should be

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:foo="http://www.macgregor.com/pbws/eod">
<xsl:output method="text"/>
<xsl:template match="foo:BlockTrade">
ID=<xsl:value-of select="normalize-space(foo:Block_Trade_Id)"/>,
SIDE=<xsl:value-of select="normalize-space(foo:Side)"/>
</xsl:template>
</xsl:stylesheet>

Read "XML Namespaces and How They Affect XPath and XSLT" at
http://msdn.microsoft.com/library/en...asp?frame=true

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #2
Oleg Tkachenko [MVP] wrote:

Should be

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:foo="http://www.macgregor.com/pbws/eod">
<xsl:output method="text"/>
<xsl:template match="foo:BlockTrade">
ID=<xsl:value-of select="normalize-space(foo:Block_Trade_Id)"/>,
SIDE=<xsl:value-of select="normalize-space(foo:Side)"/>
</xsl:template>
</xsl:stylesheet>
I can see how this is more appropriate, however it doesn't quite solve
my problem. I still get

1BUY as an output from

<BlockTrades xmlns="http://www.macgregor.com/pbws/eod">
<BlockTrade>
<Block_Trade_Id>1</Block_Trade_Id>
<Side>BUY</Side>
</BlockTrade>
</BlockTrades>

Read "XML Namespaces and How They Affect XPath and XSLT" at
http://msdn.microsoft.com/library/en...asp?frame=true


Looks like a good article to read.

Out of curiosity, could I format my XSL like this:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.macgregor.com/pbws/eod">
<xsl:output method="text"/>
<xsl:template match="foo:BlockTrade">
ID=<xsl:value-of select="normalize-space(Block_Trade_Id)"/>,
SIDE=<xsl:value-of select="normalize-space(Side)"/>
</xsl:template>
</xsl:stylesheet>

setting a default namespace so I won't have to declare every element I use?

Thanks.
-Ben
Nov 12 '05 #3
Ben Bloom wrote:
I can see how this is more appropriate, however it doesn't quite solve
my problem. I still get

1BUY as an output from

<BlockTrades xmlns="http://www.macgregor.com/pbws/eod">
<BlockTrade>
<Block_Trade_Id>1</Block_Trade_Id>
<Side>BUY</Side>
</BlockTrade>
</BlockTrades>
I get ID=1, SIDE=BUY
Out of curiosity, could I format my XSL like this:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.macgregor.com/pbws/eod">
<xsl:output method="text"/>
<xsl:template match="foo:BlockTrade">
ID=<xsl:value-of select="normalize-space(Block_Trade_Id)"/>,
SIDE=<xsl:value-of select="normalize-space(Side)"/>
</xsl:template>
</xsl:stylesheet>

setting a default namespace so I won't have to declare every element I use?


No, you cannot. XPath 1.0 doesn't support default namespaces.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #4
Oleg Tkachenko [MVP] wrote:
Ben Bloom wrote:
I can see how this is more appropriate, however it doesn't quite solve
my problem. I still get

1BUY as an output from

<BlockTrades xmlns="http://www.macgregor.com/pbws/eod">
<BlockTrade>
<Block_Trade_Id>1</Block_Trade_Id>
<Side>BUY</Side>
</BlockTrade>
</BlockTrades>

I get ID=1, SIDE=BUY

Interesting.

I've attached my code, XSD and XSL files. I'm curious what's different
between either our code, or our environment.

I'm compiling with VS.NET 2003 on Win2kSP4


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.Schema;
using System.Xml.XPath;
using System.IO;

namespace xslt_test
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox input;
private System.Windows.Forms.TextBox output;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

input.Text = "<BlockTrades xmlns=\"http://www.macgregor.com/pbws/eod\">\r\n" +
"<BlockTrade>\r\n" +
"\t<Block_Trade_Id>1</Block_Trade_Id>\r\n" +
"\t<Side>BUY</Side>\r\n" +
"</BlockTrade>\r\n" +
"</BlockTrades>\r\n";
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.input = new System.Windows.Forms.TextBox();
this.output = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// input
//
this.input.Location = new System.Drawing.Point(8, 8);
this.input.Multiline = true;
this.input.Name = "input";
this.input.Size = new System.Drawing.Size(664, 168);
this.input.TabIndex = 0;
this.input.Text = "";
//
// output
//
this.output.Location = new System.Drawing.Point(8, 184);
this.output.Multiline = true;
this.output.Name = "output";
this.output.Size = new System.Drawing.Size(664, 192);
this.output.TabIndex = 1;
this.output.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 384);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(680, 677);
this.Controls.Add(this.button1);
this.Controls.Add(this.output);
this.Controls.Add(this.input);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
DataSet Trades = new DataSet();
Trades.ReadXmlSchema("BlockTrades.xsd");
Trades.ReadXml(new StringReader(input.Text));
XmlDataDocument xmlDoc = new XmlDataDocument(Trades);
XslTransform xslt = new XslTransform();
xslt.Load("PBWS.EOD.xsl");
StringWriter sw = new StringWriter();
xslt.Transform(xmlDoc, null, sw, null);
output.Text = sw.ToString();
}
}
}

Nov 12 '05 #5

Doh! Figures, as I hit send I realize my mistake.

Forgot the Namespace qualifier on the template tag.

Sorry to waste your time! :)
Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: George | last post by:
I have two tables and want to find the Maximum date for a given GIN. I have been able to produce the result in Sybase but I am having problems in Oracle. Example of my query in Sybase. create...
1
by: Aaron Ackerman | last post by:
I am having very odd problems with running a very simplie n-Tier Duamish-type Win Forms app. I will often encounter problems with me having to compile each tier, starting with the Data Access...
1
by: malcolm | last post by:
Hello, We have a small team building a project that involves some 30 or so c# assembly dlls. It is a client server application with 1 exe as the starting point. The dlls and exe are sharing an...
1
by: mr_burns | last post by:
hi there, Ive been having a few problems recently with this group. Im not sure if any of the other groups have been giving me trouble but recently this one has a couple of times, when attempting...
6
by: clintonG | last post by:
After the last six days trying to download VS2005 Professional it seems there may be a problem with my instance of the File Transfer Manager (FTM) or somewhere in the network in between. I can't...
8
by: Michael C | last post by:
Hi all, I'm still having problems with VS.NET 2003 running on my XP machine. I finally decided I might as well uninstall completely and reinstall from scratch. But lo and behold it won't let...
4
by: don | last post by:
I have two existing interfaces having methods with same names. Now I have to implement both intrfaces in one class. Is there any way I could implement methods with same names in both interfaces...
11
by: Rick Mogstad | last post by:
I know this is probably not the group for this, but i know that these groups are fairly active, and that the people are usually willing to help. Is anyone else experiencing this? The news...
7
by: Ron | last post by:
Hi All, It's taking a long, LONG time for me to see a post from here (and other NGs too, for that matter--which is why I think it may be me...). Sometimes it takes 2-3 minutes after clicking...
6
by: Pep | last post by:
Firstly, I'm not sure if this is the right group for this query, so please forgive me if I am wrong. My problem is that most users I distribute my software to cannot install it on their systems...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.