You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何用C#无法从该XSL脚本中获取关联CSS文件列表?

为什么提取XSL中标签的C#方法返回空列表?

我有一段XSL脚本,其中通过标签引入了CSS文件,但编写的C#方法GetLinkedCssFilesList提取关联CSS文件路径时返回空列表,请问原因是什么?

XSL文件代码

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
    <xsl:output method="html" indent="yes" version="4.01"
      doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
      doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"/>
    <xsl:param name="CSSFile1"></xsl:param>
    <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <xsl:choose>
                <xsl:when test="//Settings/ForeignGroupMode=1">
                    <xsl:attribute name="lang">
                        <xsl:value-of select="//Settings/ForeignGroupLanguageCode"/>
                    </xsl:attribute>
                    <xsl:attribute name="dir">
                        <xsl:value-of select="//Settings/ForeignGroupDirection"/>
                    </xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:attribute name="lang">
                        <xsl:value-of select="//Settings/LanguageCode"/>
                    </xsl:attribute>
                    <xsl:attribute name="dir">
                        <xsl:value-of select="//Settings/Direction"/>
                    </xsl:attribute>
                </xsl:otherwise>
            </xsl:choose>
            <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-Legacy.css"/>
                    </xsl:otherwise>
                </xsl:choose>
                <title>
                    <xsl:value-of select="//Labels/Congregation"/>&amp;#160;<xsl:value-of select="//Labels/Title" />
                </title>
            </head>
            <body>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

C#方法代码

public string[] GetLinkedCssFilesList(string xslFilePath)
{
   XDocument document = XDocument.Load(xslFilePath);
   List<string> cssFiles = new List<string>();

   var cssLinks = document.Descendants("link");
   foreach(var css in  cssLinks)
   {
       cssFiles.Add(css.Attribute("href").Value);
   }

   return cssFiles.ToArray();
}

问题原因

核心问题是XML命名空间不匹配:

  • XSL文件的根元素<xsl:stylesheet>中定义了默认命名空间xmlns="http://www.w3.org/1999/xhtml",这意味着模板内的<link>标签属于这个XHTML命名空间,并非无命名空间的元素。
  • 你的C#代码中使用document.Descendants("link")时,没有指定对应的命名空间,只会查找无命名空间的<link>元素,自然找不到目标标签。

解决方案

修改C#代码,指定XHTML命名空间后再查找<link>元素:

public string[] GetLinkedCssFilesList(string xslFilePath)
{
   XDocument document = XDocument.Load(xslFilePath);
   List<string> cssFiles = new List<string>();
   // 定义XHTML命名空间
   XNamespace xhtmlNs = "http://www.w3.org/1999/xhtml";

   // 使用命名空间查找<link>元素
   var cssLinks = document.Descendants(xhtmlNs + "link");
   foreach(var css in cssLinks)
   {
       cssFiles.Add(css.Attribute("href").Value);
   }

   return cssFiles.ToArray();
}

内容的提问来源于stack exchange,提问作者Andrew Truckle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 19:47:33