解决 长话短说,不要在XSL转换中使用CDATA。请继续阅读。
背景 标准C#应用程序使用XmlSerializer将对象保存到XML文件。我需要获取保存的XML文件,添加对XSL转换的引用,应用所述转换,然后保存为.graphml
问题 目前我的输出似乎有不正确的编码。 <和>标志表示为"& lt"和"& gt"这是无效的graphml(或者xml)
详情 以下是输出文件目前的样子:
<?xml version="1.0" encoding="utf-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<graph id="G" edgedefault="undirected">
<node id="n1"/>
<edge source="n1" target="n2"/>
<edge source="n1" target="n3"/>
<edge source="n1" target="n5"/>
<edge source="n1" target="n7"/>
<edge source="n1" target="n9"/>
<node id="n2"/>
<edge source="n2" target="n1"/>
<edge source="n2" target="n9"/>
为了它的价值,我知道我正在转换的XML是有效的(并且格式正确),并且我知道xsl文件有效且工作正常,因为我可以使用它在其他文件上就好了。 C#应用XSL转换的方式导致此问题。 以下是相关代码:
// Create an XML writer
XmlSerializer writer = new XmlSerializer(typeof(formattedMeshNet));
var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\netTransform.xml";
var transformPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\GraphFormatTransform.xsl";
var savePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ExportedNetwork.graphml";
System.IO.FileStream file = System.IO.File.Create(path);
writer.Serialize(file, saveMe);
file.Close();
// Add style sheet association
XmlDocument xDoc = new XmlDocument();
var transformAssn = xDoc.CreateProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"GraphFormatTransform.xsl\"");
xDoc.AppendChild(transformAssn);
// Load xml just saved
System.Xml.Xsl.XslCompiledTransform myXslTransform;
myXslTransform = new System.Xml.Xsl.XslCompiledTransform();
myXslTransform.Load(transformPath);
myXslTransform.Transform(path, savePath);
任何想法将不胜感激!如果您发现我遗漏了任何内容,请随意提出详细信息,以便我们愉快地提供:)
潜在相关的问题并没有真正告诉我任何事情,但你可能会得到一些有用的东西: XSL transformation give me wrong encoding
我也尝试过做这里建议的事情: The XslCompiledTransform output encoding
谢谢!
**编辑**
根据请求,这是完整的XML文档和关联的XSL转换
<?xml version="1.0"?>
<formattedMeshNet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<contents>
<node>
<nodeID>1</nodeID>
<physNeighbors>
<int>4</int>
<int>6</int>
<int>7</int>
<int>8</int>
</physNeighbors>
<visited />
<neighbors />
</node>
<node>
<nodeID>2</nodeID>
<physNeighbors>
<int>4</int>
<int>11</int>
</physNeighbors>
<visited />
<neighbors />
</node>
<node>
<nodeID>3</nodeID>
<physNeighbors>
<int>4</int>
<int>9</int>
<int>10</int>
<int>11</int>
</physNeighbors>
<visited />
<neighbors />
</node>
<node>
<nodeID>4</nodeID>
<physNeighbors>
<int>1</int>
<int>2</int>
<int>3</int>
<int>8</int>
<int>11</int>
</physNeighbors>
<visited />
<neighbors />
</node>
<node>
<nodeID>5</nodeID>
<physNeighbors>
<int>8</int>
<int>11</int>
</physNeighbors>
<visited />
<neighbors />
</node>
<node>
<nodeID>6</nodeID>
<physNeighbors>
<int>1</int>
<int>7</int>
<int>8</int>
<int>10</int>
<int>12</int>
</physNeighbors>
<visited />
<neighbors />
</node>
<node>
<nodeID>7</nodeID>
<physNeighbors>
<int>1</int>
<int>6</int>
</physNeighbors>
<visited />
<neighbors />
</node>
<node>
<nodeID>8</nodeID>
<physNeighbors>
<int>1</int>
<int>4</int>
<int>5</int>
<int>6</int>
</physNeighbors>
<visited />
<neighbors />
</node>
<node>
<nodeID>9</nodeID>
<physNeighbors>
<int>3</int>
<int>11</int>
<int>12</int>
<int>13</int>
</physNeighbors>
<visited />
<neighbors />
</node>
<node>
<nodeID>10</nodeID>
<physNeighbors>
<int>3</int>
<int>6</int>
<int>13</int>
</physNeighbors>
<visited />
<neighbors />
</node>
<node>
<nodeID>11</nodeID>
<physNeighbors>
<int>2</int>
<int>3</int>
<int>4</int>
<int>5</int>
<int>9</int>
<int>13</int>
</physNeighbors>
<visited />
<neighbors />
</node>
<node>
<nodeID>12</nodeID>
<physNeighbors>
<int>6</int>
<int>9</int>
</physNeighbors>
<visited />
<neighbors />
</node>
<node>
<nodeID>13</nodeID>
<physNeighbors>
<int>9</int>
<int>10</int>
<int>11</int>
</physNeighbors>
<visited />
<neighbors />
</node>
</contents>
</formattedMeshNet>
<小时/> XSL
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<![CDATA[
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">]]>
<![CDATA[<graph id="G" edgedefault="undirected">]]>
<xsl:for-each select="/formattedMeshNet/contents/node">
<xsl:variable name="parentNodeID">
<xsl:value-of select="nodeID" />
</xsl:variable>
<![CDATA[<node id="n]]><xsl:copy-of select="$parentNodeID" /><![CDATA["/>]]>
<xsl:for-each select="physNeighbors/int">
<![CDATA[<edge source="n]]><xsl:copy-of select="$parentNodeID" /><![CDATA[" target="n]]><xsl:value-of select="." /><![CDATA["/>]]>
</xsl:for-each>
</xsl:for-each>
<![CDATA[</graph>
</graphml>]]>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:0)
如果我猜对了,你的样式表应该是这样的:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/formattedMeshNet">
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<graph id="G" edgedefault="undirected">
<xsl:for-each select="contents/node">
<xsl:variable name="parentNodeID" select="nodeID" />
<node id="n{$parentNodeID}" />
<xsl:for-each select="physNeighbors/int">
<edge source="n{$parentNodeID}" target="n{.}" />
</xsl:for-each>
</xsl:for-each>
</graph>
</graphml>
</xsl:template>
</xsl:stylesheet>
应用于您的输入示例,结果将是:
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<graph id="G" edgedefault="undirected">
<node id="n1"/>
<edge source="n1" target="n4"/>
<edge source="n1" target="n6"/>
<edge source="n1" target="n7"/>
<edge source="n1" target="n8"/>
<node id="n2"/>
<edge source="n2" target="n4"/>
<edge source="n2" target="n11"/>
<node id="n3"/>
<edge source="n3" target="n4"/>
<edge source="n3" target="n9"/>
<edge source="n3" target="n10"/>
<edge source="n3" target="n11"/>
<node id="n4"/>
<edge source="n4" target="n1"/>
<edge source="n4" target="n2"/>
<edge source="n4" target="n3"/>
<edge source="n4" target="n8"/>
<edge source="n4" target="n11"/>
<node id="n5"/>
<edge source="n5" target="n8"/>
<edge source="n5" target="n11"/>
<node id="n6"/>
<edge source="n6" target="n1"/>
<edge source="n6" target="n7"/>
<edge source="n6" target="n8"/>
<edge source="n6" target="n10"/>
<edge source="n6" target="n12"/>
<node id="n7"/>
<edge source="n7" target="n1"/>
<edge source="n7" target="n6"/>
<node id="n8"/>
<edge source="n8" target="n1"/>
<edge source="n8" target="n4"/>
<edge source="n8" target="n5"/>
<edge source="n8" target="n6"/>
<node id="n9"/>
<edge source="n9" target="n3"/>
<edge source="n9" target="n11"/>
<edge source="n9" target="n12"/>
<edge source="n9" target="n13"/>
<node id="n10"/>
<edge source="n10" target="n3"/>
<edge source="n10" target="n6"/>
<edge source="n10" target="n13"/>
<node id="n11"/>
<edge source="n11" target="n2"/>
<edge source="n11" target="n3"/>
<edge source="n11" target="n4"/>
<edge source="n11" target="n5"/>
<edge source="n11" target="n9"/>
<edge source="n11" target="n13"/>
<node id="n12"/>
<edge source="n12" target="n6"/>
<edge source="n12" target="n9"/>
<node id="n13"/>
<edge source="n13" target="n9"/>
<edge source="n13" target="n10"/>
<edge source="n13" target="n11"/>
</graph>
</graphml>