tags:

views:

301

answers:

7

Hi,

I have XML:

<results>
    <Countries country="Albania">
        <Regions region="Centralna Albania">
            <Provinces province="Durres i okolice">
                <Cities city="Durres"
                            cityCode="2B66E0ACFAEF78734E3AF1194BFA6F8DEC4C5760">
                    <IndividualFlagsWithForObjects Status="1" />
                    <IndividualFlagsWithForObjects  Status="0" />
                    <IndividualFlagsWithForObjects status="2" />
                 </Cities>
             </Provinces>
        </Regions>
    </Countries>
    <Countries .... 

Which is result of this part of query:

SELECT Countries.FileSystemName as country,
       Regions.DefaultName as region ,
       Provinces.DefaultName as province,
       cities.defaultname as city,
       cities.code as cityCode, 
       IndividualFlagsWithForObjects.value as Status

I have xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <xsl:output method="text" encoding="iso-8859-1"/>

  <xsl:param name="delim" select="string(',')" />
  <xsl:param name="quote" select="string('&quot;')" />
  <xsl:param name="break" select="string('&#xD;')" />

  <xsl:template match="/">
    <xsl:apply-templates select="results/countries" />
  </xsl:template>

  <xsl:template match="countries">
    <xsl:apply-templates />
    <xsl:if test="following-sibling::*">
      <xsl:value-of select="$break" />
    </xsl:if>
  </xsl:template>

  <xsl:template match="*">
    <!-- remove normalize-space() if you want keep white-space at it is -->
    <xsl:value-of select="concat($quote, normalize-space(.), $quote)" />
    <xsl:if test="following-sibling::*">
      <xsl:value-of select="$delim" />
    </xsl:if>
  </xsl:template>

  <xsl:template match="text()" />
</xsl:stylesheet>

And is part of code

XmlReader reader = cmd.ExecuteXmlReader();

doc.LoadXml("<results></results>");
XmlNode newNode = doc.ReadNode(reader);

while (newNode != null)
{
    doc.DocumentElement.AppendChild(newNode);
    newNode = doc.ReadNode(reader);
}                    

doc.Save(@"c:\listOfCities.xml");

XslCompiledTransform XSLT = new XslCompiledTransform();    
XsltSettings settings = new XsltSettings();    


XSLT.Load(@"c:\xsltfile1.xslt", settings, new XmlUrlResolver());

XSLT.Transform(doc.OuterXml,@"c:\myCities.csv");

Why now I have in my csv only one cell with value : 

+1  A: 

XML elements are named Countries and XSLT uses countries instead.

I'd also make processing of elements and attributes explicit:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; 

  <xsl:output method="text" encoding="iso-8859-1"/> 

  <xsl:param name="delim" select="string(',')" /> 
  <xsl:param name="quote" select="string('&quot;')" /> 
  <xsl:param name="break" select="string('&#xD;')" /> 

  <xsl:template match="/"> 
    <xsl:apply-templates select="results/Countries" /> 
  </xsl:template> 

  <xsl:template match="Countries"> 
    <xsl:apply-templates select="@*|*"/> 
    <xsl:if test="following-sibling::*"> 
      <xsl:value-of select="$break" /> 
    </xsl:if> 
  </xsl:template> 

  <xsl:template match="*"> 
    <xsl:apply-templates select="@*"/> 
    <xsl:if test="*|following-sibling::*"> 
      <xsl:value-of select="$delim" /> 
      <xsl:apply-templates select="*"/> 
    </xsl:if> 
  </xsl:template> 

  <xsl:template match="@*"> 
    <!-- remove normalize-space() if you want keep white-space at it is --> 
    <xsl:value-of select="concat($quote, normalize-space(.), $quote)" /> 
    <xsl:if test="position() != last ()"> 
      <xsl:value-of select="$delim" /> 
    </xsl:if> 
  </xsl:template> 

</xsl:stylesheet>
kwaxer
unortunately this doesn't help
Maybe, a modified stylesheet will help.
kwaxer
A: 

I cannot understand what that stylesheet is supposed to do. EnableScript = true makes no sense either. Try this:

string folderPath = @"path\to\your\folder";
using (XmlReader reader = XmlReader.Create(Path.Combine(folderPath, "listOfCities.xml")))
using (TextWriter writer = File.CreateText(Path.Combine(folderPath, "myCities.csv")))
{
    var xslt = new XslCompiledTransform();
    xslt.Load(Path.Combine(folderPath, "citiesCsv.xslt"));
    xslt.Transform(reader, null, writer);
}

Where citiesCsv.xslt is:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:for-each select="//Countries">
      <xsl:value-of
        select="@country"/>,<xsl:value-of
        select="Regions/@region"/>,<xsl:value-of
        select="Regions/Provinces/@province"/>,<xsl:value-of
        select="Regions/Provinces/Cities/@city"/>
      <xsl:text>&#13;&#10;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Dour High Arch
+1  A: 

Looks like you're using the wrong character set. This a great link you should really read: http://www.joelonsoftware.com/articles/Unicode.html

Like the title says, it's "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)".

GuidoH
+1  A: 

The input xml (or xslt) probably starts with Unicode's Byte Order Mark, and is badly parsed by c# (for no obvious reason I can find..).

Open it in some hexeditor, look at first 3 bytes, and delete them.

Yossarian
A: 

I think this is what you meant to do. I do recommend that you look into Dour High Arch's xslt suggestion. It would make things a bit cleaner.

C# code

XslCompiledTransform style = new XslCompiledTransform();
style.Load(@"c:\xsltfile1.xslt");
style.Transform(@"c:\listOfCities.xml", @"c:\myCities.csv");

xsltfile1.xslt

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <xsl:output method="text" encoding="iso-8859-1"/>

  <xsl:param name="delim" select="string(',')" />
  <xsl:param name="quote" select="string('&quot;')" />
  <xsl:param name="break" select="string('&#xD;')" />

  <xsl:template match="/">
    <xsl:apply-templates select="results/Countries" />
  </xsl:template>

  <xsl:template match="Countries">
    <xsl:apply-templates select="//@*" />
    <xsl:if test="not(position()=last())">
      <xsl:value-of select="$break" />
    </xsl:if>
  </xsl:template>

  <xsl:template match="@*">
    <xsl:value-of select="concat($quote, normalize-space(.), $quote)" />
    <xsl:if test="not(position()=last())">
      <xsl:value-of select="$delim" />
    </xsl:if>
  </xsl:template>

  <xsl:template match="text()" />
</xsl:stylesheet>

listOfCities.csv:

"Albania","Centralna Albania","Durres i okolice","Durres","2B66E0ACFAEF78734E3AF1194BFA6F8DEC4C5760","1","0","2"
Kristof Neirynck
A: 

Your output encoding is set to encoding="iso-8859-1", but this ISO code page does not contain the characters you mention. For example, ď is only defined in iso-8859-2.

My guess is therefore, that the problem is caused by a mismatch of encodings. Have a look at the generated file in a hex editor.

I am not sure whether the XslCompiledTransform.Transform method uses the encoding given in the XSLT to write the file.

Use the Transform() method which writes to an XmlWriter based on a StringBuilder, and save the resulting string to a file controlling its encoding.

devio
A: 

 is what UTF8 files seem to start with for non UTF8 aware apps. Your Xml writer is outputting unicode, but the result wants to be ISO-8859-1 (as per <xsl:output method="text" encoding="iso-8859-1"/>

when calling XSLT.Transform(...) in your code, use the overload Transform(String, XmlWriter) and create that XmlWriter with ISO-8859-1 encoding:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;

XmlWriter writer = XmlWriter.Create("c:\myCities.csv",settings);

and then use it for the transform

XSLT.Transform(doc.OuterXml,writer);
Axarydax