vb.netlinqxsltxml-literals

XML Literal with Linq statement


I am going to be performing an XSLT transformation, transforming XML to an HTML table. It's tabular data, so that's why I'm not using div's. ;)

Anyway, I need to repeat one part of the XSLT for the size of one of my collections. Here's a snippet of the code...

Dim styleSheet = <?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" 
  xmlns:rh="ReportHub"
  exclude-result-prefixes="msxsl"
>
  <xsl:output method="html" indent="yes" />
  <xsl:template match="rh:Report/rh:Tablix1/rh:Details_Collection">
    <xsl:variable name="alternating-row" select="position() mod 2" />
    <table class=<%= dataFormatter.formattingTableClass %>>
      <xsl:choose>
        <xsl:when test="count(rh:Details)=0">
          <tr>
            <td>There are no items listed for this client</td>
          </tr>
        </xsl:when>
        <xsl:otherwise>
          <xsl:for-each select="rh:Details">
            <tr class=<%= dataFormatter.formattingTRClass %>>
              <xsl:variable name="mainrow-position" select="position()" />
              <xsl:for-each select="@*">
                <%= From x In dataFormatter.dataColumnSettings Select 
                  <xsl:if test="name() != 'colName'">
                    <xsl:choose>
                      <xsl:when test="$mainrow-position=1">
                        <th>
                          <xsl:value-of select="name()"/>
                        </th>
                      </xsl:when>
                      <xsl:otherwise>
                        <td>
                          <xsl:value-of select="."/>
                        </td>
                      </xsl:otherwise>
                    </xsl:choose>
                  </xsl:if> 
                %>
              </xsl:for-each>
            </tr>
          </xsl:for-each>
        </xsl:otherwise>
      </xsl:choose>
    </table>
  </xsl:template>
</xsl:stylesheet>

The problem is that because the XML inside the LINQ query references the xsl namespace, I get:

Error   9   XML namespace prefix 'xsl' is not defined.

Anyone got any clever ideas?


Solution

  • I was a bit amazed that this was possible but then I noticed this only works in VB.NET, not C#. :-) Anyway, I took a look at MSDN to learn more about this and it's a bit of a wild guess, but I think you need to use a separate Imports statement to add those namespaces. Something like:

    Imports <xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    Imports <xmlns:msxsl="urn:schemas-microsoft-com:xslt">
    Imports <xmlns:rh="ReportHub">
    

    It is a bit of an educated guess, though. You asked for a clever idea, this is mine.