Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old May 21st, 2009, 12:19 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I doubt very much that 'Indeed' requires the xml to contain CData, it is just using that as an example. No XML parser in the world (assuming it is a valid XML parser) should process a CData element any different from the encoded text (which your .Net code should write out perfectly).

I am assuming you are going by this web site: http://www.indeed.com/intl/en/xmlinfo.html

While that link does state that all element should be in CData you should be fine provided you provide them with valid XML. At least I would 'hope' you would be fine. Have you tried it, or emailed them to check - I would before trying to get the CData into your XML the hard way.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old May 21st, 2009, 12:32 PM
Registered User
 
Join Date: May 2009
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by samjudson View Post
I doubt very much that 'Indeed' requires the xml to contain CData, it is just using that as an example. No XML parser in the world (assuming it is a valid XML parser) should process a CData element any different from the encoded text (which your .Net code should write out perfectly).

I am assuming you are going by this web site: http://www.indeed.com/intl/en/xmlinfo.html

While that link does state that all element should be in CData you should be fine provided you provide them with valid XML. At least I would 'hope' you would be fine. Have you tried it, or emailed them to check - I would before trying to get the CData into your XML the hard way.
You know it always helps to think outside of the box... Indeed does not have a system that you can "upload' your feed and then check the status like google base does. So testing these things would mean putting the feed up, waiting for indeed to pick it up and then waiting to see if they jobs were included. However there is nothing to tell you that the feed has been picked up OR when the jobs are active to its kinda hard to test.

Now if you read that page you will see that it does not say the elements "should" be in CData it says:

All job content must be inside CDATA sections to avoid issues processing your XML feed

I do like to follow directions. And there is some reason so this. CData is not completly useless or else it would not exist. (that is not entirely true)

The biggest reason for it is that some of these fields are user entered text that can include html elements and Indeed wants to make sure nothing gets broken if html tags and such are used.
 
Old May 21st, 2009, 12:50 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Yes, I realise why Indeed are saying what they are saying, and from a point of view of getting people to ensure there data is always valid XML enclosing everything in CData elements is one way to go - I'm just saying that it probably wouldn't hurt to email them and ask them if indeed that has to be followed, or if ensuring it is valid XML will be enough.

The top of the page does say 'contact us' if you have any questions, so that would have been my first port of call.

CData does have its uses, especially when manually typing data in and making things more human readable, but seeing as to a valid XMLParser there should be no difference between <![CDATA[<tag>]!> and &lt;tag&gt; (unless you are specifically looking for that difference) trying to enforce that all text is wrapped in CData seems a bit like hitting a nail with a hammer the size of a skyscraper.

Just my opinions.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old May 21st, 2009, 01:31 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

As already suggested, if you want to transform to an XmlWriter and want it to honour the xsl:output settings like cdata-section-elements your stylesheet has then you need to create the XmlWriter with the OutputSettings of the XslCompiledTransform instance.
That is also documented in http://msdn.microsoft.com/en-us/library/ms163436.aspx
So either transform to a Stream or TextWriter or if you want to transform to an XmlWriter then create it with
Code:
XslCompiledTransform xsltProc = new XslCompiledTransform();
xsltProc.Load("file.xml");
using (XmlWriter writer = XmlWriter.Create("result.xml", xsltProc.OutputSettings))
{
  xsltProc.Transform(xmlDataDocumentInstance, null, writer);
  writer.Close();
}
That way any xsl:output cdata-section-elements="title data company" will ensure the contents of those elements is wrapped into CDATA sections.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
johnw182 (May 21st, 2009)
 
Old May 21st, 2009, 10:09 PM
Registered User
 
Join Date: May 2009
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Michael and Sam, thanks for the grateful attempts but in the end there was an answer to the problem at hand and I did not need to circle around it.

Martin, had the answer and it seems it was in the C# code. The problem is apparently the XmlTextWriter. It seems I need to use the XmlWriter instead. I started down this path but got nowhere until now. Thank You Martin!

Here is the complete solution with my code.

the cdata-section-elements must be added.

XmlDataDocument xmlDoc = new XmlDataDocument(data_set);
XslCompiledTransform xslTran = new XslCompiledTransform();
xslTran.Load("indeed.xslt");

XmlWriter writer = XmlWriter.Create("result.xml", xslTran.OutputSettings);
xslTran.Transform(xmlDoc, null, writer);
writer.Close();
 
Old May 22nd, 2009, 12:11 AM
Registered User
 
Join Date: May 2009
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Another problem...

The XmlWriter has a bug where it DOES NOT close the underlying file stream that it creates.

The solution is to create a separate FileStream object, pass that to the XmlWriter.Create method and then close that file stream object after closing the XmlWriter object.

To duplicate try the following code.
Code:
XmlDataDocument xmlDoc = new XmlDataDocument(data_set);
XslCompiledTransform xslTran = new XslCompiledTransform();
xslTran.Load("indeed.xslt");

XmlWriter writer = XmlWriter.Create("result.xml", xslTran.OutputSettings);
xslTran.Transform(xmlDoc, null, writer);
writer.Close();
 
FileInfo feed_file = newFileInfo("result.xml");
feed_file.MoveTo("C:\\testing_move.xml");
You will get an exception that the file is locked by another process or such.

The solution is:
Code:
FileStream fs = newFileStream("result.xml", FileMode.Create);
XmlDataDocument xmlDoc = new XmlDataDocument(data_set);
XslCompiledTransform xslTran = new XslCompiledTransform();
xslTran.Load("indeed.xslt");

XmlWriter writer = XmlWriter.Create(fs, xslTran.OutputSettings);
xslTran.Transform(xmlDoc, null, writer);
writer.Close();
fs.Close();
 
FileInfo feed_file = newFileInfo("result.xml");
feed_file.MoveTo("C:\\testing_move.xml");
Should work fine.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Transforming XML From One Namespace to Another mail4kaja XSLT 1 November 10th, 2008 10:14 AM
Add a CDATA section; add nodeset to CDATA section kssudhish XSLT 3 January 3rd, 2008 07:13 AM
XSL transforming to TEXT suri_1811 XSLT 8 October 30th, 2006 05:02 AM
Exception when transforming using XSLT ksskumar XSLT 5 October 10th, 2006 06:20 AM
Exception when transforming using ksskumar XSLT 1 October 10th, 2006 02:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.