473,395 Members | 1,539 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,395 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 1533
-----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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.