Вот немного моего 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"/> <xsl:value-of select = "//Labels/Title" />
</title>
</head>
<body>
</body>
</html>
</xsl:template>
Он имеет ссылки CSS, например:
<link rel = "stylesheet" type = "text/css" href = "Workbook-S-140-Legacy.css"/>
Я попытался написать процедуру C#, которая выдавала бы мне список всех связанных файлов CSS:
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 по умолчанию xmlns = "http://www.w3.org/1999/xhtml"
в XSL.
Это означает, что элементы link
принадлежат этому пространству имен http://www.w3.org/1999/xhtml
xml.
Вам необходимо включить его при их получении.
XNamespace ns = "http://www.w3.org/1999/xhtml";
var cssLinks = document.Descendants(ns + "link");