XML Literal with Linq Expression

I am going to do an XSLT transformation by converting XML to HTML table. This is tabular data, so I am not using a div.;)

Anyway, I need to repeat one piece of XSLT for the size of one of my collections. Here is a code snippet ...

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, since the XML inside the LINQ query is referencing the xsl namespace, I get:

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

Anyone have any clever ideas?

  • The inner workings of XML within a LINQ query are not yet complete, so don't worry about how it looks.
+2


source to share


3 answers


I was a little surprised that this is possible, but then I noticed that this only works in VB.NET, not C #. :-) Anyway, I took a look at MSDN to find out more about it, and it's a bit wild guess, but I think you need to use a separate Imports statement to add these 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">

      

However, this is a bit of an educated guess. You asked for a smart idea, this is mine.
+5


source


I think you should use Import . Something like that:



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

      

+3


source


This is the standard behavior for any XML query technology, including XLINQ. Any namespaces that you declare within a document do not affect the namespaces used by the Query API. You should always report the request API separately from the namespaces you want to recognize. For inline XML VB.NET, you use the operator Imports

. In C #, you instantiate an object XNamespace

instead, as VB.NET special syntax is just syntactic sugar over various constructors XObject

.

IIRC's reasoning for this is that the document may not have been prepared by you, so you have no way to predict ahead of time which prefix space the author chose to use. The only safe thing you should do is tell your request API which namespace prefixes to use for requests.

+1


source







All Articles