Domanda

Devo trasformare un documento XML valido in OFX V1.0.2 Formato. Questo formato è più o meno XML, ma è tecnicamente non valido e quindi non può essere analizzato come XML.

Ho difficoltà a far funzionare la mia trasformazione XML perché il .net XslCompiledTransform L'oggetto insiste sull'interpretazione dell'output come un documento XML (che è abbastanza giusto).

** Ecco la mia funzione per trasformare l'XML

public string Transform(XmlElement xmlElement, Dictionary<string, object> parameters)
{
    string strReturn = "";

    // Set the settings to allow scripts to executed.
    XsltSettings settings = new XsltSettings(false, true);

    // Load the XSLT Document
    XslCompiledTransform xslt = new XslCompiledTransform();

    xslt.Load(xsltFileName, settings, new XmlUrlResolver());

    // arguments
    XsltArgumentList args = new XsltArgumentList();
    if (parameters != null && parameters.Count > 0)
    {
        foreach (string key in parameters.Keys)
        {
            args.AddParam(key, "", parameters[key]);
        }
    }

    //Create a memory stream to write to
    Stream objStream = new MemoryStream();

    // Transform the xml/xslt into a Writer
    XmlTextWriter xmlWriter = new XmlTextWriter(objStream, Encoding.UTF8);

    // Apply the transform
    xslt.Transform(xmlElement, args, xmlWriter);

    objStream.Seek(0, SeekOrigin.Begin);

    // Read the contents of the stream
    StreamReader objSR = new StreamReader(objStream);

    strReturn = objSR.ReadToEnd();

    return strReturn;
}

Se sfugge ai tag XML-ish &lt; e &gt, vengono rimossi quando scarico il file.

Ecco l'inizio della mia XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text"></xsl:output>
  <xsl:param name="currentdate"></xsl:param>
  <xsl:template match="Transactions">
OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE
<OFX>
    <SIGNONMSGSRSV1>
        <SONRS>
            <STATUS>
                <CODE>0
                <SEVERITY>INFO
            </STATUS>
            <DTSERVER><xsl:value-of select="$currentdate" />
            <LANGUAGE>ENG

Quindi posso trasformare il mio XML in una stringa di testo semplice?

AGGIORNARE:

Ho cambiato questa domanda. Ho appena realizzato l'ovvia risposta alla domanda originale. Usando il XslCompiledTransform L'oggetto mi richiede di scrivere l'output su un documento XML usando un XMLTextWriter. Ovviamente non analizzerà. Scuse.

È stato utile?

Soluzione

XSLT può output di testo; Assicurati solo di impostare la modalità di uscita di XSLT su testo e WRIET su a TextWriter (Sono disponibili più sovraccarichi). Scrivere qualcosa che è quasi XML in XSLT è doloroso, ma possibile con disabilitazione delle regole di fuga.

Ecco un esempio (anche se brutto) XSLT per scrivere non xml:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="text" indent="yes"/>

    <xsl:template match="@* | node()">
      <xsl:call-template name="startElement">
        <xsl:with-param name="name" select="'SONRS'"/>
      </xsl:call-template>        
      <xsl:call-template name="startElement">
        <xsl:with-param name="name" select="'STATUS'"/>
        <xsl:with-param name="value" select="0"/>
      </xsl:call-template>
      <xsl:call-template name="startElement">
        <xsl:with-param name="name" select="'SEVERITY'"/>
        <xsl:with-param name="value" select="'INFO'"/>
      </xsl:call-template>
      <xsl:call-template name="endElement">
        <xsl:with-param name="name" select="'SONRS'"/>
      </xsl:call-template>
    </xsl:template>

  <xsl:template name="startElement">
    <xsl:param name="name"/>
    <xsl:param name="value"/>
    <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
    <xsl:value-of select="$name"/>
    <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
    <xsl:value-of select="$value"/>
  </xsl:template>
  <xsl:template name="endElement">
    <xsl:param name="name"/>
    <xsl:text disable-output-escaping="yes">&lt;/</xsl:text>
    <xsl:value-of select="$name"/>
    <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
  </xsl:template>
</xsl:stylesheet>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top