开发者

How can I transform XML to invalid XML using XSLT?

开发者 https://www.devze.com 2022-12-24 13:29 出处:网络
I need to transform a valid XML document to the OFX v1.0.2 format.This format is more or less XML, but it\'s technically invalid and therefore cannot be parsed as XML.

I need to transform a valid XML document to the OFX v1.0.2 format. This format is more or less XML, but it's technically invalid and therefore cannot be parsed as XML.

I'm having trouble getting my Xml transformation working because开发者_运维技巧 the .Net XslCompiledTransform object insists on interpreting the output as an XML document (which is fair enough).

**Here's my function to transform the 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;
}

If I escape the xml-ish tags using &lt; and &gt, they get removed when I download the file.

Here's the start of my 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

So can I transform my XML to a plain text string?

UPDATE:

I've changed this question. I just realised the obvious answer to the original question. Using the XslCompiledTransform object requires me to write the output to an Xml document using an XmlTextWriter. Obviously it won't parse. Apologies.


Xslt can output text; just make sure that you set the output-mode of the xslt to text, and wriet to a TextWriter (there are multiple overloads available). Writing something that is nearly xml in xslt is painful, but possible with disabling the escape rules.

Here's an example (albeit ugly) xslt for writing 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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号