我有以下尝试将 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。