带有 Linq 语句的 XML 文字
我将执行 XSLT 转换,将 XML 转换为 HTML 表。它是表格数据,所以这就是我不使用 div 的原因。 ;)
无论如何,我需要根据我的集合之一的大小重复 XSLT 的一部分。这是代码片段...
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>
问题是因为 LINQ 查询中的 XML 引用了 xsl 命名空间,所以我得到:
Error 9 XML namespace prefix 'xsl' is not defined.
有人有什么聪明的想法吗?
- LINQ 查询中 XML 的内部工作尚未完成,因此不必担心它看起来是什么样子。
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?
- The inner workings of the XML inside the LINQ query isn't finished, so don't worry about what that looks like.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有点惊讶这是可能的,但后来我注意到这只适用于 VB.NET,不适用于 C#。 :-) 无论如何,我查看了 MSDN 以了解更多信息关于这一点,这有点疯狂的猜测,但我认为您需要使用单独的 Imports 语句来添加这些名称空间。类似这样的:
不过,这是一个有根据的猜测。你要求一个聪明的主意,这是我的。
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:
It is a bit of an educated guess, though. You asked for a clever idea, this is mine.
我认为您必须使用 Imports 语句。
像这样的东西:
I think you have to use the Imports statment.
Something like this:
这是任何 XML 查询技术(包括 XLINQ)的标准行为。您在文档中声明的任何命名空间都不会影响查询 API 使用的命名空间。您始终必须单独通知查询 API 您希望其识别的名称空间。对于 VB.NET 内联 XML,您可以使用
Imports
语句。在 C# 中,您可以实例化一个XNamespace
对象,因为 VB.NET 特殊语法只是各种XObject
构造函数的语法糖。IIRC 其背后的原因是该文档可能不是由您生成的,因此您无法提前预测文档作者可能选择使用的命名空间前缀。唯一安全的做法是告诉您的查询 API 用于查询的名称空间前缀。
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 aXNamespace
object instead, since the VB.NET special syntax is just syntactic sugar over the variousXObject
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.