Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#例程中的XML/XSL转换问题,用于输出带有嵌入CSS内容的HTML_Xml_Xslt - Fatal编程技术网

C#例程中的XML/XSL转换问题,用于输出带有嵌入CSS内容的HTML

C#例程中的XML/XSL转换问题,用于输出带有嵌入CSS内容的HTML,xml,xslt,Xml,Xslt,我有一个XSL脚本,上面有: <head> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <xsl:choose> <x

我有一个XSL脚本,上面有:

<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <xsl:choose>
        <xsl:when test="$CSSFile1 !=''">
            <style type="text/css">
                <xsl:value-of select="$CSSFile1"/>
            </style>
        </xsl:when>
        <xsl:otherwise>
            <link rel="stylesheet" type="text/css" href="Workbook-S-140-PublicTalk-WatchtowerStudy-ServiceTalk-Videoconference2.css"/>
            <link rel="stylesheet" type="text/css" href="Workbook-S-140-PublicTalk-WatchtowerStudy-ServiceTalk-Zoom.css"/>
        </xsl:otherwise>
    </xsl:choose>
    <title>
        <xsl:value-of select="//Labels/Congregation"/>&#160;<xsl:value-of select="//Labels/Title" />
    </title>
</head>

有些地方出了问题,因为转换应该同时嵌入两个CSS文件,但最终只嵌入了第一个CSS文件。没有出现错误。

首先,我同意w/Martin re:XPath。手工解析XML/XSL对我来说是个危险信号。使用XML/XSL和XPath中提供的所有工具。查看
XmlDocument。选择nodes
查找节点并进行快速DOM操作。我知道这是“不可能的”,但几分钟掌握XPath和DOM操作将在很长一段时间内获得回报

回复:你的问题

“转换应该同时嵌入CSS文件,并且只嵌入CSS文件 最后是第一个。”

当存在
$CSSFile1
时,XSL仅输出
$CSSFile1
。但是,第二个文件内容将作为第二个参数输出
$CSSFile2
(您没有输出)。您可以通过两种方式解决问题

  • CSSFile2
    中的内容附加到
    strcsfilecontent
    变量中。然后,“一切”都将包含在
    $CSSFile1
    • 或者-
  • 在XSL输出中,CSSFile1和CSSFile2都是

  • 祝你好运,希望这对你有帮助。

    首先,我同意w/Martin re:XPath。手工解析XML/XSL对我来说是个危险信号。使用XML/XSL和XPath中提供的所有工具。查看
    XmlDocument。选择nodes
    查找节点并进行快速DOM操作。我知道这是“不可能的”,但几分钟掌握XPath和DOM操作将在很长一段时间内获得回报

    回复:你的问题

    “转换应该同时嵌入CSS文件,并且只嵌入CSS文件 最后是第一个。”

    当存在
    $CSSFile1
    时,XSL仅输出
    $CSSFile1
    。但是,第二个文件内容将作为第二个参数输出
    $CSSFile2
    (您没有输出)。您可以通过两种方式解决问题

  • CSSFile2
    中的内容附加到
    strcsfilecontent
    变量中。然后,“一切”都将包含在
    $CSSFile1
    • 或者-
  • 在XSL输出中,CSSFile1和CSSFile2都是

  • 祝您好运,希望这对您有所帮助。

    您是否考虑过使用XPath来识别XSLT中作为XPathDocument或XmlDocument加载的
    链接[@rel='stylesheet'][@type='text/css']
    元素,而不是将其作为纯文本解析?也许这样可以更容易地读取
    href
    属性值,然后检查文件。@MartinHonnen我没有考虑过这种方法。您是否考虑过使用XPath来识别
    链接[@rel='stylesheet'][@type='text/css']
    XSLT中的元素是否作为XPathDocument或XmlDocument加载,而不是作为纯文本解析?也许这样可以更容易地读取
    href
    属性值,然后检查文件。@MartinHonnen我没有考虑过这种方法。谢谢你的回答。我肯定会考虑使用替代方法。根据您的回答,我可以根据需要调整XSL逻辑。非常感谢。谢谢你的回答。我肯定会考虑使用替代方法。根据您的回答,我可以根据需要调整XSL逻辑。非常感谢。
    public bool TransformXMLToHTML(string strTransformXSLPath, string strScheduleXMLPath, string strScheduleHTMLPath)
    {
        try
        {
            var xmlResolver = new XmlUrlResolver();
            XsltArgumentList argsList = new XsltArgumentList();
    
            string strRootPath = Path.GetDirectoryName(strTransformXSLPath);
    
            // Read the XSL file and locate all the CSS documents that are used
            int iFileCount = 0;
            string[] lines = File.ReadAllLines(strTransformXSLPath);
            foreach (string line in lines)
            {
                if ((line).Trim().Contains("text/css"))
                {
                    int iHREFIndex = line.IndexOf("href=\"");
                    if (iHREFIndex != -1)
                    {
                        string strCSSFile = line.Substring(iHREFIndex + 6);
    
                        int iQuoteIndex = strCSSFile.IndexOf("\"");
                        if (iQuoteIndex != -1)
                        {
                            strCSSFile = strCSSFile.Substring(0, iQuoteIndex);
    
                            // Build full path and make sure the file exists
                            string strCSSFilePath = Path.Combine(strRootPath, strCSSFile);
                            if(File.Exists(strCSSFilePath))
                            {
                                // Establish the parameter name
                                iFileCount++;
                                string strParamName = "CSSFile" + iFileCount.ToString();
    
                                // Read the content and attach
                                string strCSSFileContent = File.ReadAllText(strCSSFilePath);
                                argsList.AddParam(strParamName, "", strCSSFileContent);
                            }
                        }
                    }
                }
            }
    
            // Now perform the transformation
            XslCompiledTransform transformer = new XslCompiledTransform();
            transformer.Load(strTransformXSLPath, new XsltSettings { EnableDocumentFunction = true }, xmlResolver);
    
            using (StreamWriter sw = new StreamWriter(strScheduleHTMLPath))
            {
                transformer.Transform(strScheduleXMLPath, argsList, sw);
            }
        }
        catch (Exception ex)
        {
            SimpleLog.Log(ex);
            return false;
        }
    
        return true;
    }