如何使用 XSLT 将 XML 转换为无效 XML?

发布于 2024-08-26 22:24:32 字数 2458 浏览 4 评论 0原文

我需要将有效的 XML 文档转换为 OFX v1.0.2 格式。这种格式或多或少是 XML,但它在技术上是无效的,因此不能解析为 XML。

我无法正常进行 Xml 转换,因为 .Net XslCompiledTransform 对象坚持将输出解释为 XML 文档(这很公平)。

**这是我转换 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;
}

如果我使用 &lt;&gt 转义 xml-ish 标签,那么当我下载文件时它们会被删除。

这是我的 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

那么我可以将 XML 转换为纯文本字符串吗?

更新:

我已经更改了这个问题。我刚刚意识到原来问题的明显答案。使用 XslCompiledTransform 对象需要我使用 XmlTextWriter 将输出写入 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 < and >, 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风和你 2024-09-02 22:24:32

Xslt可以输出文本;只需确保将 xslt 的输出模式设置为文本,并写入 TextWriter (有多个可用的重载)。在 xslt 中编写接近 xml 的内容是很痛苦的,但是可以通过禁用转义规则来实现。

下面是一个用于编写非 xml 的 xslt 示例(虽然丑陋):

<?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"><</xsl:text>
    <xsl:value-of select="$name"/>
    <xsl:text disable-output-escaping="yes">></xsl:text>
    <xsl:value-of select="$value"/>
  </xsl:template>
  <xsl:template name="endElement">
    <xsl:param name="name"/>
    <xsl:text disable-output-escaping="yes"></</xsl:text>
    <xsl:value-of select="$name"/>
    <xsl:text disable-output-escaping="yes">></xsl:text>
  </xsl:template>
</xsl:stylesheet>

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"><</xsl:text>
    <xsl:value-of select="$name"/>
    <xsl:text disable-output-escaping="yes">></xsl:text>
    <xsl:value-of select="$value"/>
  </xsl:template>
  <xsl:template name="endElement">
    <xsl:param name="name"/>
    <xsl:text disable-output-escaping="yes"></</xsl:text>
    <xsl:value-of select="$name"/>
    <xsl:text disable-output-escaping="yes">></xsl:text>
  </xsl:template>
</xsl:stylesheet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文