472,789 Members | 1,228 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

XSL Transform - can't get it to work

I'm learning how to use the XSL transform functionality and can't get
it to work. In a book I'm reading on it, it says that I can do like I
did below and just add a value like <root_node/> in there and I should
be able to transform any XML source document I have so that the
results would simply contain <root_node/>.

I wanted to try that, just so I could see if I understand how this is
working and wrote the code and XSL file below. However, my value for
szTransformedXML is always an empty string. Does anyone see what I'm
doing wrong?

Here's a method I wrote to use it:

private void TransformData(string szXML, out string szTransformedXML)
{
XslTransform oXsl = new XslTransform();
XmlDocument oXMLDoc = new XmlDocument();

oXsl.Load(@"C:\\Srclib\\Test.xsl");
oXMLDoc.LoadXml(szXML);
XmlReader oRead = oXsl.Transform(oXMLDoc.CreateNavigator(),null);
oXMLDoc = null;
szTransformedXML = oRead.ReadOuterXml();

}
Here's what my Test.xsl file looks like:

<?xml version="1.0" ?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<root_node/>
</xsl:template>
</xsl:stylesheet>
Nov 12 '05 #1
4 1502
-----Original Message-----
I'm learning how to use the XSL transform functionality and can't getit to work. In a book I'm reading on it, it says that I can do like Idid below and just add a value like <root_node/> in there and I shouldbe able to transform any XML source document I have so that theresults would simply contain <root_node/>.

I wanted to try that, just so I could see if I understand how this isworking and wrote the code and XSL file below. However, my value forszTransformedXML is always an empty string. Does anyone see what I'mdoing wrong?

Here's a method I wrote to use it:

private void TransformData(string szXML, out string szTransformedXML){
XslTransform oXsl = new XslTransform();
XmlDocument oXMLDoc = new XmlDocument();

oXsl.Load(@"C:\\Srclib\\Test.xsl");
oXMLDoc.LoadXml(szXML);
XmlReader oRead = oXsl.Transform (oXMLDoc.CreateNavigator(),null); oXMLDoc = null;
szTransformedXML = oRead.ReadOuterXml();

}
Here's what my Test.xsl file looks like:

<?xml version="1.0" ?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0"> <xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<root_node/>
</xsl:template>
</xsl:stylesheet>
.


You should use <xsl:value-of select="root_node"/>

See my example:
http://weblogs.asp.net/sonukapoor/articles/162473.aspx

Sonu Kapoor
My Xml HowTo's:
http://weblogs.asp.net/sonukapoor/articles/159790.aspx
Nov 12 '05 #2
-----Original Message-----
I'm learning how to use the XSL transform functionality and can't getit to work. In a book I'm reading on it, it says that I can do like Idid below and just add a value like <root_node/> in there and I shouldbe able to transform any XML source document I have so that theresults would simply contain <root_node/>.

I wanted to try that, just so I could see if I understand how this isworking and wrote the code and XSL file below. However, my value forszTransformedXML is always an empty string. Does anyone see what I'mdoing wrong?

Here's a method I wrote to use it:

private void TransformData(string szXML, out string szTransformedXML){
XslTransform oXsl = new XslTransform();
XmlDocument oXMLDoc = new XmlDocument();

oXsl.Load(@"C:\\Srclib\\Test.xsl");
oXMLDoc.LoadXml(szXML);
XmlReader oRead = oXsl.Transform (oXMLDoc.CreateNavigator(),null); oXMLDoc = null;
szTransformedXML = oRead.ReadOuterXml();

}
Here's what my Test.xsl file looks like:

<?xml version="1.0" ?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0"> <xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<root_node/>
</xsl:template>
</xsl:stylesheet>
.


You should use <xsl:value-of select="root_node"/>

See my example:
http://weblogs.asp.net/sonukapoor/articles/162473.aspx

Sonu Kapoor
My Xml HowTo's:
http://weblogs.asp.net/sonukapoor/articles/159790.aspx
Nov 12 '05 #3
"Doug" <dn******@dtgnet.com> wrote in message news:ff**************************@posting.google.c om...
However, my value for szTransformedXML is always an empty string.
Does anyone see what I'm doing wrong? : : XmlReader oRead = oXsl.Transform(oXMLDoc.CreateNavigator(),null);
szTransformedXML = oRead.ReadOuterXml();


Your stylesheet is fine; it's how you're using the XmlReader that the
Transform() method is returning to you.

Initially, an XmlReader is in ReadState.Initial. You have to call Read()
on it to cause it to become interactive (i.e., "wake up"). Usually, when
you see an XmlReader used in .NET the code resembles an Iterator
pattern:

while ( oXmlReader.Read( ) )
{
// Do something with Current node.
}

Obviously, this doesn't "throw out" the first node ... it's that the XmlReader
assumes it's one step before the beginning of the XML document. When
you call Read( ), the XmlReader steps up and is positioned on node #0.

The solution then, is to put a call to oRead.Read( ) prior to ReadOuterXml();
although for greater clarity, I'd recommend:

XmlReader oRead = oXsl.Transform(oXMLDoc.CreateNavigator(),null);
oRead.MoveToContent();
szTransformedXML = oRead.ReadOuterXml();

The effect is the same whether you call Read() once or MoveToContent(),
I prefer MoveToContent() when I'm not in a looping situation because it
makes it entirely evident that my intention is to position the XmlReader
onto some content that interests me.
Derek Harmon
Nov 12 '05 #4
"Doug" <dn******@dtgnet.com> wrote in message news:ff**************************@posting.google.c om...
However, my value for szTransformedXML is always an empty string.
Does anyone see what I'm doing wrong? : : XmlReader oRead = oXsl.Transform(oXMLDoc.CreateNavigator(),null);
szTransformedXML = oRead.ReadOuterXml();


Your stylesheet is fine; it's how you're using the XmlReader that the
Transform() method is returning to you.

Initially, an XmlReader is in ReadState.Initial. You have to call Read()
on it to cause it to become interactive (i.e., "wake up"). Usually, when
you see an XmlReader used in .NET the code resembles an Iterator
pattern:

while ( oXmlReader.Read( ) )
{
// Do something with Current node.
}

Obviously, this doesn't "throw out" the first node ... it's that the XmlReader
assumes it's one step before the beginning of the XML document. When
you call Read( ), the XmlReader steps up and is positioned on node #0.

The solution then, is to put a call to oRead.Read( ) prior to ReadOuterXml();
although for greater clarity, I'd recommend:

XmlReader oRead = oXsl.Transform(oXMLDoc.CreateNavigator(),null);
oRead.MoveToContent();
szTransformedXML = oRead.ReadOuterXml();

The effect is the same whether you call Read() once or MoveToContent(),
I prefer MoveToContent() when I'm not in a looping situation because it
makes it entirely evident that my intention is to position the XmlReader
onto some content that interests me.
Derek Harmon
Nov 12 '05 #5

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

Similar topics

3
by: qazmlp | last post by:
I was using the following code to convert the string to lowercase. string foo = "Some Mixed Case Text"; transform(foo.begin(), foo.end(), foo.begin(), tolower); I thought the above code is...
4
by: Luther Baker | last post by:
My team is using the FO library to generate PDFs. We are also required to use https. The XSL transform page fed into javax.xml.transform.Transformer starts with <?xml version="1.0"?>...
0
by: Xiaolei Li | last post by:
first off, i'm a total newbie at this stuff so excuse any wrong usage of terminology or whatever else. i have a XSL to transform a Document such that all "text" nodes will have a "SPAN" inserted...
2
by: John Lehmann | last post by:
I have an interesting problem. I am performing an XSL transform using the System.Xml.Xsl.Transform class. I have a database that contains the XSL style sheet string. And it seems to work pretty...
9
by: Patrick Guio | last post by:
Dear all, I am trying to use the std::transform algorithm to to the following vector< vector<char> >::iterator ik = keys.begin(); // key list iterator vector< vector<char> >::iterator is = ik;...
5
by: KathyB | last post by:
If someone could just explain this to me...I just don't get it! I have an aspx page where I retrieve several session variables and use xmlDocument to transform xml file with xsl file into an...
0
by: Doug | last post by:
I'm learning how to use the XSL transform functionality and can't get it to work. In a book I'm reading on it, it says that I can do like I did below and just add a value like <root_node/> in...
4
by: Dean Card | last post by:
Okay, so here is the situation. I have need to do some on-the-fly image creation. I have everything working great except for the last part of it, applying a perspective type transform to the...
4
by: benben | last post by:
I have the following code snippet: class converter { // ... public: virtual polar_point convert_to_polar(const point&) = 0; }; void f(const std::vector<point>& points, converter& cvt)
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.