0

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!

4

1 回答 1

1

正如另一个 SO answer中提到的那样。问题是您在源 XML(“ http://www.xxxxx.com ”)中定义了一个名称空间,但您没有在 xslt 中使用该名称空间。

您可以从源 XML 中删除名称空间,也可以在 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>
于 2012-11-29T04:32:28.513 回答