Windows Phone 中的 XSL
我需要在 Windows Phone 7 平台上对 xml 文件进行 XSL 转换。问题在于 Microsoft 从 WP7 SDK 中排除了 System.Xml.Xsl 命名空间。所以没有 XslTransform 类。 有人能提出任何解决方法吗?
更新: 我有 xml 格式的文章,需要以人类可读的形式向用户表示。使用为我完成这项工作的网络服务是不可能的,因为应用程序应该完全与互联网无关。 WebBrowser 控件曾经运行良好,但上次 Mango 更新打破了这种方法。
I need to make XSL transformation of my xml-file on windows phone 7 platform. The problem is that Microsoft excluded System.Xml.Xsl namespace from WP7 SDK. So there is no XslTransform class.
Could anyone propose any workaround of this?
UPDATE:
I have articles in xml format that I need to represent to the user in human readable form. Using of web-service which does this work for me is impossible because application should be completely Internet-agnostic. WebBrowser control used to work well but last Mango update broke this approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于 Microsoft 从 WP7 中排除了 XslTransform 类,因此您将需要另一个实现。
由于 XslTransform 始终可用于 .NET 中的其他平台,因此在 C# 中找到可以使用的开源实现可能并不容易。也许您可以考虑从另一个框架(例如 Java)改编 Xsl 处理器。不幸的是,这可能是一项相当大的工作量。
如果你找不到任何可以轻松用于 WP7 的东西,也许你应该问问自己,你是否真的需要它?你能改变你的架构来解决这个问题吗?
作为最后的手段,您可以构建一个 Web 服务来为您执行 Xsl 转换。这样做的明显缺点是手机必须连接到互联网才能使用 - 并且服务必须启动。换句话说,它给解决方案增加了很多复杂性(除非您已经从服务器提取数据,在这种情况下,我只需在返回数据之前在服务器端进行转换)。
Since Microsoft excluded the XslTransform class from WP7, you will need another implementation.
Since the XslTransform has always been available for other platforms in .NET, it might not be easy to find an open source implementation in C# which you could use. Perhaps you could look into adapting a Xsl processor from another framework such as Java. Unfortunately, that might be a considerable amount of work.
If you can't find anything that can be readily used for WP7, perhaps you should ask yourself, if you really need it ? Can you change your architecture to work around this ?
As a last resort, you could build a web service that does the Xsl Transformation for you. The obvious downside to this is that the phone has to be connected to the internet to use it - and the service has to be up. In other words, it's adding a lot of complexity to the solution (unless you are already pulling the data from the server, in which case I would simply do the transform on the server side before returning the data).
@Pashec,为了提出替代方案,如果您能解释您想要实施的转换的性质,将会有所帮助。毕竟,XSLT 是一种通用转换语言,您几乎可以用它做任何事情。两种常见的转换场景是: (1) 复制输入 XML 文档,除了添加/修改/删除一些选定的元素或属性外,几乎原封不动; (2) 转换输入 XML 文档的基本结构,例如对元素进行分组、两个不同层次结构之间的映射等。您能否告诉我们您想要完成的转换范围?
除了了解您的目标是什么以便我们可以建议实现该目标的编程方法之外,还有两种途径似乎值得进行一些研究。首先,在您的场景中,将 XML 发送到托管服务进行转换是否有意义?如果您已经将服务用于其他目的,这可能是合理的;如果没有,那可能有点多了。
其次,在 .NET Framework 中,XSLT 编译为内存中程序集。 xsltc.exe 实用程序可用于将 XSLT 转换编译为常规(即基于文件的).NET 程序集。 阅读有关xsltc 在这里 和其他地方。我还没有尝试调整 xsltc 生成的程序集以使其可在 Windows Phone 应用程序中使用,但这可能是可行的技术。
@Pashec, in order to suggest alternatives, it would help if you could explain the nature of the transformation you're looking to implement. After all, XSLT is such a general purpose transformation language, you could be doing just about anything with it. Two common transformation scenarios are: (1) copy an input XML document mostly intact, except for adding/modifying/deleting a few selected elements or attributes; and (2) transform the fundamental structure of the input XML document such as grouping elements, mapping between two disparate hierarchies, etc. Can you give us an idea of the range of transformations you're trying to accomplish?
In addition to understanding what you're goal is so we can suggest programming approaches to get ther, there are two avenues that seem worth a little investigation. First, in your scenario would it make sense to send the XML to a hosted service to transform? If you're already using services for other purposes, that might be reasonable; if not, then that might be a bit much.
Second, in the .NET Framework, XSLT compiles to an in-memory assembly. The xsltc.exe utility can be used to compile XSLT transformations to a regular (i.e., file-based) .NET assembly. Read about xsltc here and elsewhere. I haven't tried adapting an xsltc-generated assembly to be usable in a Windows Phone application, but that could be viable technique.
@Pashec 在问题的评论中回答说,他想要创建的转换不会显着重组文档,那么另一个可行的解决方案是使用 XmlReader 和 XmlWiter 对,从读取器读取元素和属性并写入相应的所需元素和与作者的属性。
如何:使用 XmlReader 解析 XML 中的第一个示例 准确地说明了这种方法。
与 XSLT 相比,此方法具有明显的优势,因为它不会为源文档或目标文档创建 XML DOM。 XML DOM 通常是 XML 序列化(尖括号形式)大小的 10 倍左右。
@Pashec replied in the question's comments that the transformation he wants to create doesn't significantly restructure the document, then another viable solution would be to use and XmlReader and XmlWiter pair, reading elements and attributes from the reader and writing the corresponding desired elements and attributes with the writer.
The first example in How to: Parse XML with XmlReader illustrates this approach exactly.
This approach has a distinct advantage over XSLT, as this does not create XML DOMs for either source or target document. An XML DOM is typically on the order of 10x the size of the serialized (angle-bracket form) of the XML.