如何使用 XSLT 将 XML 转换为无效 XML?
我需要将有效的 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;
}
如果我使用 <
和 >
转义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Xslt可以输出文本;只需确保将 xslt 的输出模式设置为文本,并写入
TextWriter
(有多个可用的重载)。在 xslt 中编写接近 xml 的内容是很痛苦的,但是可以通过禁用转义规则来实现。下面是一个用于编写非 xml 的 xslt 示例(虽然丑陋):
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: