 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

November 4th, 2011, 12:50 PM
|
|
Authorized User
|
|
Join Date: Oct 2011
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Multiple xml reposnses in one transform
I have been given a task to write an xml transformation for trades coming onto a system. However, sometimes there will be multiple trades in one xml document.
I have created a transform that will seperate them into multiple output xml trades but within the same document.. as xslt can only outrput into one document.
the content of the document is fine (i think ) but when i run the transform, within a c# exe, which is the final goal of the project, i get the following error
System.InvalidOperationException: Token StartElement in state EndRootElement would result in an invalid XML document
Basically the teransfom cannot handle multiple 'trades' withon one transform....
Is there any way round this to get all the output trades in one document as seperate xml nodes but returned from a single transfom
thanks for yuor help
|
|

November 4th, 2011, 01:00 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Show us the relevant code such as the XSLT processor class you use, the way you generate the transformation result with C# and of course the XML input and XSLT.
XSLT output can be an XML fragment (external parsed entity) so multiple top level elements are allowed. But I guess you somehow feed it to an XmlWriter with the wrong XmlWriterSettings. If the XSLT processor is XslCompiledTransform, then make sure if you transform to an XmlWriter you create explicitly that you use the OutputSettings property of the XslCompiledTransform instance.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

November 4th, 2011, 01:04 PM
|
|
Authorized User
|
|
Join Date: Oct 2011
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
public static void Transform(string sXmlPath, string sXslPath, string outputFile)
{
//load the Xml doc
XPathDocument myXPathDoc = new XPathDocument(sXmlPath);
XmlDocument xmlReader = new XmlDocument();
xmlReader.Load(sXmlPath);
string xml = xmlReader.InnerXml;
StringWriter sw = new StringWriter();
using (XmlReader xri = XmlReader.Create(new StringReader(xml)))
using (XmlWriter xwo = XmlWriter.Create(sw))
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(sXslPath);
xslt.Transform(xri, xwo);
}
string outputXML = sw.ToString();
.......
.......
}
The output xml from the transform i know is sound, as whwen i run it through viual studio XSLT debugger.. it outputs correct xml with multiple output trades.......
But when i run it through the above code I get an exception (the exception handler is on the lwevel of the calling function.
|
|

November 4th, 2011, 01:05 PM
|
|
Authorized User
|
|
Join Date: Oct 2011
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
i uploaded a response but waiting for the moderator to ok it.. apparently..
thanks
|
|

November 4th, 2011, 01:07 PM
|
|
Authorized User
|
|
Join Date: Oct 2011
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
public static void Transform(string sXmlPath, string sXslPath, string outputFile)
{
//load the Xml doc
XPathDocument myXPathDoc = new XPathDocument(sXmlPath);
XmlDocument xmlReader = new XmlDocument();
xmlReader.Load(sXmlPath);
string xml = xmlReader.InnerXml;
StringWriter sw = new StringWriter();
using (XmlReader xri = XmlReader.Create(new StringReader(xml)))
using (XmlWriter xwo = XmlWriter.Create(sw))
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(sXslPath);
xslt.Transform(xri, xwo);
}
string txt = sw.ToString();
|
|

November 4th, 2011, 01:17 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Use
Code:
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(sXslPath);
using (XmlWriter xwo= XmlWriter.Create(sw, xslt.OutputSettings)) {
xslt.Transform(xri, xwo);
}
or don't create an XmlWriter at all, instead directly transform to your StringWriter
Code:
using (StringWriter sw = new StringWriter()) {
xslt.Transform(xri, null, sw);
}
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Last edited by Martin Honnen; November 4th, 2011 at 01:28 PM..
Reason: correcting typo
|
|

November 4th, 2011, 01:59 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Also the code you have is taking unnecessary steps, if you have a variable with the file path or URL of the XML input document then the Transform method has overloads which take that path or URL as its first argument, so there is no need to create an XPathDocument or XmlDocument and of course no need to then take the InnerXml to parse that again, you can simply use http://msdn.microsoft.com/en-us/library/ms163431.aspx with e.g.
Code:
public static void Transform(string sXmlPath, string sXslPath, string outputFile)
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(sXslPath);
xslt.Transform(sXmlPath, outputFile);
}
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|
 |