Search code examples
xmlxsltssisinfopath

using xslt to remove info path declarations form info path XML ? <? mso .......>


Using xslt I was able to remove the namespaces, but how do I remove the <?mso...?> tags?

I am trying to convert the info path XML to XML and store it in a database.

  <?xml version="1.0"?>

    <?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:Zone-Zero-Observation-Card-with-form-audit-no-approval-mobile-view-Test-1:-myXSD-2012-09-05T20-51-15" solutionVersion="1.0.0.873" productVersion="14.0.0.0" PIVersion="1.0.0.0" href="test.xsn"?>
    <?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.3"?>
    <?mso-infoPath-file-attachment-present?>

Below is the xslt I tried but that did not help.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml"  indent="no"/>

  <xsl:template match="/|comment()|processing-instruction()[starts-with(name(), 'mso-')]">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

Solution

  • The type of node is called processing-instruction so use

    <xsl:template match="processing-instruction()[starts-with(name(), 'mso-')]"/>
    

    together with the identity transformation template

    <xsl:template match="@* | node()">
      <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
    </xsl:template>
    

    That way the processing instructions where the name starts with mso- will not be copied.

    I will show a complete stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:template match="text() | comment()">
        <xsl:copy/>
      </xsl:template>
    
      <xsl:template match="processing-instruction()[starts-with(name(), 'mso-')]"/>
    
      <xsl:template match="*">
        <xsl:element name="{local-name()}">
          <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
      </xsl:template>
    
      <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:template>
    
    </xsl:stylesheet>