GordonBy
GordonBy

Reputation: 3407

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?

Upvotes: 2

Views: 1126

Answers (3)

Santiago Cepas
Santiago Cepas

Reputation: 4094

I think you have to use the Imports statment. Something like this:

Imports <xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

Upvotes: 3

Christian Hayter
Christian Hayter

Reputation: 31071

This is standard behaviour for any XML querying technology, XLINQ included. Any namespaces you declare inside your document have no effect on the namespaces used by the querying API. You always have to inform the querying API separately of the namespaces that you want it to recognise. For VB.NET inline XML, you use the Imports statement. In C#, you instantiate a XNamespace object instead, since the VB.NET special syntax is just syntactic sugar over the various XObject constructors.

IIRC the reasoning behind this is that the document may not have been produced by you, so you have no way of predicting in advance what namespace prefixes the document author may have chosen to use. The only safe thing to do is tell your querying API what namespace prefixes to use for querying.

Upvotes: 1

Wim ten Brink
Wim ten Brink

Reputation: 26682

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.

Upvotes: 5

Related Questions