views:

515

answers:

3

I had some classes I was serializing out using XMLSerializer, then transforming using XSLT into various formatted emails or web service calls. All was well.

We started using Linq in some parts of the data layer, and I needed to serialize out some of the Linq objects to be consumed by the XSL Stylesheets. I decided it was probably best to move towards using the DataContractSerializer instead of XMLSerializer.

I got the DataContracts and everything set up, and things serialize out nicely with a couple minor changes to the entity names... BUT now the stylesheets won't process the XML at all. I really have no idea why I'm not getting at least something... basically all that comes out is the data stripped of the xsl tags though.

Any ideas what would cause that?

EDIT:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:template match="CallTicket">

Here is the XML generated from DataContractSerializer before I modified the DataContract declaration:

<CallTicket xmlns="http://schemas.datacontract.org/2004/07/CRMInterface.CRMData" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
+2  A: 

Could it be that your new XML has XML namespaces in it, that your XSLT dosen't deal with properly?

Can you show us relevant parts of your XML and XSLT files?

Marc

marc_s
Yes, probably. How do I _properly_ deal with the namespaces? I'll edit in the xsl declarations...
Telos
If you have XML namespaces in your XML, you'll need the same declarations in your XSLT, and you'll need to qualify your elements with the xmlns:xy prefixes.
marc_s
I don't suppose you can control what namespace LINQ entities use? It appears to be hard coded in the .designer.cs file with no way to access it from the designer...
Telos
No, you can't change it - the XML namespace is fixed - it's the one describing the XML for the LINQ dbml files. It's always xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007" - which makes it easy - you can just add this namespace to your XSLT and that's it :-)
marc_s
A: 

I think I figured it out, the namespaces are more important than I realized. It works if I set DataContractAttribute(Namespace="") for each class being serialized. I have some more work to do with the linq entities... but on the right track.

The only thing that gets me is that there was no reference to the namespace in the xslt file...

Telos
if there is no reference then you need to add it as an attribute in your xsl:stylesheet element. See my example in the answer I posed
mjmarsh
Don't clear out the namespace, instead set it to something real, and then use that namespace in your XSL. XML elements are identified by the combination of local name + namespace. By clearing the namespace, you make name collisions more likely.
John Saunders
By setting it to something, I have to update dozens of pre-existing stylesheets. :P I doubt any of this is going to be consumed outside of my own code anyway...
Telos
+2  A: 

You most likely have a namespace issue. For example:

If the XML looks like this

<Root xmls="http://www.example.org/1" ....

<test>one</test>

Then you'll have to do one of two things in the XSLT

Specifically reference the namespace and use the prefix accordingly

<xsl:stylesheet xmlns:ex="http://www.example.org/1" ....>`
   ...

    <myTag><xsl:select value-of="//ex:test"/></myTag>`

or

If there is only one namespace make it the default namespace of the XSLT file:

<xsl:stylesheet xmlns="http://www.example.com/1" ...`

...
   <myTag><xsl:select value-of="//test"/></myTag>`
mjmarsh