现在的位置: 首页 > 综合 > 正文

XSLT解析InfoPath生成的XML文件并去掉文件中的InfoPath额外信息

2012年11月29日 ⁄ 综合 ⁄ 共 2346字 ⁄ 字号 评论关闭

  InfoPath表单用来生成模板所规定格式的XML文档,但是在生成的XML文件中常常会包含一些额外信息,这些信息也是以节点的形式出现在XML文件中,并且在根节点之外,主要用于在IE中识别该XML文件是用InfoPath表单生成的可以直接用InfoPath打开,如果你在其它的浏览器中打开这些文件,则不会出现文件打开的提示。这些额外信息在程序中处理的时候难免会带来一些麻烦,影响我们对XML节点的处理和操作。这些额外信息可以在C#中找到相应的对象,看我的另一篇文章http://www.cnblogs.com/jaxu/archive/2011/10/04/2198752.html。

  例如我有一个非常简单的页面,功能就是把指定的XML文件原样输出到页面上,我考虑用XSLT对象来解析这些XML文件,然后把结果输出到页面上。XSLT中的代码应该非常简单,只需要将所有的节点原样输出就行了。

<?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" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="*">
      <xsl:copy-of select="./node()"/>
    </xsl:template>
</xsl:stylesheet>

  然后在C#代码中使用XPathDocument和XslCompiledTransform来转换输出结果。但是我发现这样做在IE中打开页面的时候总是提示要打开文件,原因就是因为IE识别出了这些XML文件中的InfoPath的额外信息。当然,如果你用FireFox打开的话则不会出现提示,而且你还能看到那些额外信息也被解析出来了。最简单的解决办法就是在输出之前将这些额外信息去掉,你完全可以在C#里来实现,这里给出用XSLT的实现方法。

<?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" exclude-result-prefixes="msxsl"
>
  <xsl:output indent="yes" omit-xml-declaration="yes" method="xml" />

  <!--<xsl:template match="/">
        <xsl:copy-of select="./node()"/>
    </xsl:template>-->


  <xsl:template match="/">
    <!-- select all nodes and only attributes in the default namespace
-->
    <xsl:apply-templates select="@*[namespace-uri()='']|node()"/>
  </xsl:template>

  <!-- copy any other element type node in any other namespace -->
  <xsl:template match="node()">
    <!-- redefine the element: note that all other namespace declarations
are omitted -->
    <xsl:element name="{name()}" namespace="{namespace-uri()}">
      <!-- match any child attribute or node -->
      <xsl:apply-templates select="@*[namespace-uri()='']|node()"/>
    </xsl:element>
  </xsl:template>

  <!-- copy any other non-element type nodes -->
  <xsl:template match="@*|text()|comment()">
    <xsl:copy/>
  </xsl:template>

  <!-- delete InfoPath PI node -->
  <xsl:template match="processing-instruction('mso-infoPathSolution')">
    <!-- output nothing -->
  </xsl:template>

  <!-- delete Office ProgID PI node -->
  <xsl:template match="processing-instruction('mso-application')">
    <!-- output nothing -->
  </xsl:template>

  <!-- delete others -->
  <xsl:template match="processing-instruction('MicrosoftWindowsSharePointServices')">
    <!-- output nothing -->
  </xsl:template>

</xsl:stylesheet>

  代码中使用了XSLT的template匹配以及系统函数,在匹配到InfoPath额外信息节点时什么也不输出,从而在最终的输出结果中将InfoPath额外信息过滤掉。

抱歉!评论已关闭.