0

私は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, '&#xD;')" 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、Saxon9HEを使用しています。

4

1 に答える 1

1

「その他」のブランチでは、ドキュメントノードの子としてテキストノードを作成しています。宛先はDOM宛先であるため、これは許可されていません。(XDMデータモデルでは許可されていますが、これをDOMで表す方法はありません)。

于 2012-09-08T17:43:34.670 に答える