Question

I want to create a html list () from an xml using xslt. I was able to do so with an xml file and a xslt file. Now I want to do it with an xml string or XmlDocument and and xslt file.

This is my current code:

private String toHTML(XmlDocument xmlDocument)
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            XslCompiledTransform xslTrans = new XslCompiledTransform();
            xslTrans.Load(Server.MapPath("Xslt/Permisos.xslt"));
            xslTrans.Transform(xmlDocument.CreateNavigator(), new XsltArgumentList(), sw);
            return sw.ToString(); 
        }

It is on a single web application assembly. No exception is thrown. Just returns "".

My XSLT

 <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

      <xsl:output method="html" indent="yes"/>
      <xsl:template match="/">
        <xsl:for-each select="menus">
          <ul>
            <xsl:for-each select="menu">
              <li>
                <a href="{url}">
                  <xsl:value-of select="nombre" />
                </a>
                <ul>
                  <xsl:for-each select="submenu">
                    <li>
                      <a href="{url}">
                        <xsl:value-of select="nombre" />
                      </a>
                    </li>
                  </xsl:for-each>
                </ul>
              </li>
            </xsl:for-each>
          </ul>
        </xsl:for-each>
      </xsl:template>
    </xsl:stylesheet>

MY XML

<?xml version="1.0" encoding="UTF-8"?><menus xmlns="http://www.xxxxx.com"><menu><nombre>Main</nombre><url>#</url><submenu><nombre>Sub</nombre><url>#</url></submenu><submenu><nombre>Sub</nombre><url>#</url></submenu><submenu><nombre>Sub</nombre><url>#</url></submenu><submenu><nombre>Sub</nombre><url>#</url></submenu><submenu><nombre>Sub</nombre><url>#</url></submenu><submenu><nombre>Sub</nombre><url>#</url></submenu><submenu><nombre>Sub</nombre><url>#</url></submenu><submenu><nombre>Sub</nombre><url>#</url></submenu><submenu><nombre>Sub</nombre><url>#</url></submenu><submenu><nombre>Sub</nombre><url>#</url></submenu></menu></menus>

Thanks!

Was it helpful?

Solution

As mentioned in another SO answer. The issue is that you define a namespace in your source XML ("http://www.xxxxx.com") but you do not use the namespace in your xslt.

You can either remove the namespace from the source XML or specify it in your xslt:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
  exclude-result-prefixes="msxsl" 
  xmlns:my="http://www.xxxxx.com">

  <xsl:output method="html" indent="yes"/>
  <xsl:template match="/">
    <xsl:for-each select="my:menus">
      <ul>
        <xsl:for-each select="my:menu">
          <li>
            <a href="{my:url}">
              <xsl:value-of select="my:nombre" />
            </a>
            <ul>
              <xsl:for-each select="my:submenu">
                <li>
                  <a href="{my:url}">
                    <xsl:value-of select="my:nombre" />
                  </a>
                </li>
              </xsl:for-each>
            </ul>
          </li>
        </xsl:for-each>
      </ul>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top