XSLt.transform gives me "ď" ż "

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 .... 

What is the result of this part of the 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">
  <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 the 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 the value: ď "ż

+3
source share
7 answers

XML elements are called Countries, and instead of XSLT is used Countries.

I would also handle the elements and attributes explicitly:

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

  <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>
+1
source

It looks like you are using the wrong character set. This is a great link that you really should read: http://www.joelonsoftware.com/articles/Unicode.html

, " , , , Unicode ( !)".

+1

xml ( xslt), , # ( ..).

- , 3 .

+1

​​ = "iso-8859-1", ISO , . , ď iso-8859-2.

, . .

, XslCompiledTransform.Transform , XSLT, .

Transform(), XmlWriter StringBuilder , .

+1

ď "ż - , , -, UTF8 , UTF8. Xml-writer unicode, ISO-8859-1 ( <xsl:output method="text" encoding="iso-8859-1"/>

XSLT.Transform(...) , Transform(String, XmlWriter) XmlWriter ISO-8859-1:

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

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

XSLT.Transform(doc.OuterXml,writer);
+1

, . EnableScript = true . :

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);
}

Csv.xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <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>
0

I think this is what you wanted to do. I recommend that you take a look at the Dour High Arch xslt offer. That would make things a little 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">
  <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"
0
source

Source: https://habr.com/ru/post/1744482/


All Articles