Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can't I obtain a list of linked CSS files from this XSL script using C#?

Here is a bit of my XSL file:

<?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"/>&#160;<xsl:value-of select="//Labels/Title" />
                </title>
            </head>
            <body>
            </body>
        </html>
    </xsl:template>

It has CSS links, eg:

<link rel="stylesheet" type="text/css" href="Workbook-S-140-Legacy.css"/>

I tried to write a C# routine to give me a list of all linked CSS files:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   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();
   }

But the resulting list is empty. Why?

>Solution :

Notice the default xml namespace declaration xmlns="http://www.w3.org/1999/xhtml" in the XSL.

That means that the link elements belong to that http://www.w3.org/1999/xhtml xml namespace.

You need to include that one when retrieving them.

XNamespace ns = "http://www.w3.org/1999/xhtml";
var cssLinks = document.Descendants(ns + "link"); 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading