gpt4 book ai didi

C#/XSLT : Linearizing XML partially working code

转载 作者:数据小太阳 更新时间:2023-10-29 01:59:03 25 4
gpt4 key购买 nike

输入 XML:

<foobar>
<Comments>
Reported By: L &amp; A Q TESTING, TESTED
Date of TESTING: Available
TESTING unavailable to resolve Test issue.
Additional Comments: Comments

Had to go into Testing System and change to the correct notification group. Per sup.
</Comments>
</foobar>

XSLT 代码:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="no" omit-xml-declaration="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space()" />
</xsl:template>
<xsl:template match="text()[../*]"/>
</xsl:stylesheet>

预期输出:

<foobar><Comments>Reported By: L &amp; A Q TESTING, TESTED Date of TESTING: Available TESTING unavailable to resolve Test issue. Additional Comments: Comments Had to go into Testing System and change to the correct notification group. Per sup.</Comments></foobar>

我得到了什么:

<foobar>
<Comments>Reported By: L &amp; A Q TESTING, TESTED Date of TESTING: Available TESTING unavailable to resolve Test issue. Additional Comments: Comments Had to go into Testing System and change to the correct notification group. Per sup.</Comments>
</foobar>

观察:

虽然 text() 节点中不必要的空格已被纠正..输出 XML 中仍然存在缩进。

理想情况下 strip-space 应该处理它.. 在它之上我添加了下面的代码

<xsl:template match="text()[../*]"/>

仍然没有运气!

的使用

XPathDocument xpathXmlOrig = new XPathDocument(string_xmlInput); 在我的 C# 代码中出错说 .. strip-space cannot be applied to document which has already been loaded!!所以我正在使用 XMLReader ..

添加 C# 代码以供引用..

        XslCompiledTransform xslTransform = new XslCompiledTransform();
string xslinput = "<?xml version=\"1.0\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:strip-space elements=\"*\"/><xsl:output indent=\"no\" omit-xml-declaration=\"yes\"/><xsl:template match=\"@*|node()\"><xsl:copy><xsl:apply-templates select=\"@*|node()\"/></xsl:copy></xsl:template><xsl:template match=\"text()[not(../*)]\"><xsl:value-of select=\"normalize-space()\" /></xsl:template><xsl:template match=\"text()[../*]\"/></xsl:stylesheet>";

string strXmlOutput = string.Empty;
StringWriter swXmlOutput = null;
MemoryStream objMemoryStream = null;
swXmlOutput = new StringWriter();
objMemoryStream = new MemoryStream();

UTC_Calc obj = new UTC_Calc();
XsltArgumentList xslArg = new XsltArgumentList();
..........
........
XmlReader reader = XmlReader.Create(string_xmlInput, settings);

XsltSettings xslsettings = new XsltSettings(false, true);
MemoryStream memStream = new MemoryStream();

XmlReader rd = XmlReader.Create(new StringReader(xslinput));

xslTransform.Load(rd);
xslTransform.Transform(reader, xslArg, objMemoryStream);
objMemoryStream.Position = 0;
StreamReader objStreamReader = new StreamReader(objMemoryStream);
strXmlOutput = objStreamReader.ReadToEnd();
..........
........

XmlDocument outputxml = new XmlDocument();
outputxml.LoadXml(strXmlOutput);
outputxml.Save(outputfile.FileName);

最佳答案

您能否浏览一下您的代码,并查找您提供给写入流的任何 XmlWriterSettings?仔细检查它没有使用缩进输出选项。

如果没有类似的东西,也许显式传递声明不应发生格式化的 XmlWriterSettings 将解决此问题。

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = false;
/* .... */

var outWriter = XmlWriter.Create(outputstream, settings);

http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx

关于C#/XSLT : Linearizing XML partially working code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15131542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com