我有以下测试代码尝试将cvs文件转换为xml。我遇到的问题是,有时它会运行,有时它会因以下错误而失败:
The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type.
以下是代码:
XsltCompiler compiler = null;
try
{
Processor processor = new Processor();
compiler = processor.NewXsltCompiler();
var sr = new StreamReader(@"c:\files\drop\csv-to-xml_v2.xslt");
var xslt = sr.ReadToEnd();
sr.Close();
StringReader reader = new StringReader(xslt);
XsltExecutable exec = compiler.Compile(reader);
XsltTransformer transformer = exec.Load();
transformer.InitialTemplate = new QName("", "main");
var XmlResult = new DomDestination();
transformer.Run(XmlResult);
reader.Close();
}
catch (Exception ex)
{
var errMsg = ex.Message;
var errList = compiler != null ? compiler.ErrorList : null;
}
XSLT是:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="fn"
exclude-result-prefixes="xs fn">
<xsl:output indent="yes" encoding="US-ASCII"/>
<xsl:param name="pathToCSV" select="'file:///C:/Files/Drop/inputcsv1.csv'"/>
<xsl:function name="fn:getTokens" as="xs:string+">
<xsl:param name="str" as="xs:string"/>
<xsl:analyze-string select="concat($str, ',')" regex='(("[^"]*")+|[^,]*),'>
<xsl:matching-substring>
<xsl:sequence select='replace(regex-group(1), "^""|""$|("")""", "$1")'/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:function>
<xsl:template match="/" name="main">
<xsl:choose>
<xsl:when test="unparsed-text-available($pathToCSV)">
<xsl:variable name="csv" select="unparsed-text($pathToCSV)"/>
<xsl:variable name="lines" select="tokenize($csv, '
')" as="xs:string+"/>
<xsl:variable name="elemNames" select="fn:getTokens($lines[1])" as="xs:string+"/>
<root>
<xsl:for-each select="$lines[position() > 0]">
<row>
<xsl:variable name="lineItems" select="fn:getTokens(.)" as="xs:string+"/>
<xsl:for-each select="$elemNames">
<xsl:variable name="pos" select="position()"/>
<column>
<xsl:value-of select="$lineItems[$pos]"/>
</column>
</xsl:for-each>
</row>
</xsl:for-each>
</root>
</xsl:when>
<xsl:otherwise>
<xsl:text>Cannot locate : </xsl:text><xsl:value-of select="$pathToCSV"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
任何人都可以看到为什么它有时会起作用然后无缘无故它在“transformer.Run(XmlResult)”行上失败了?我正在使用c#4,Visual Studio 2010和Saxon 9 HE。
答案 0 :(得分:1)
在“otherwise”分支中,您将创建一个文本节点作为文档节点的子节点。由于您的目标是DOM目标,因此不允许这样做。 (它在XDM数据模型中是允许的,但是无法在DOM中表示这一点。)