Removing unwanted fields from imported List Instances using XSLT


In my previous article I showed you how you can import List Instances and their data using the Mavention Import List Instance extension. In this article I will show you how to clean the imported List Instance by removing unnecessary fields.

Previously you have seen how you can import List Instances including the data using the Mavention Import List Instance extension. With a single mouse click the extension imported the selected List including all its data into your SharePoint Project. As I mentioned before, including all fields in the test set may be difficult to maintain and complex to keep track of. Because removing all unwanted fields for every record might get very time consuming if you have more than a few rows, I will show you how to turn this:

All fields in List Instance imported using the Mavention Import List Instance extension

into this:

Only a few selected fields in a List Instance

using a simple XSL transformation.

Removing unwanted fields using XSLT

To remove unwanted fields from the imported List Instance all you need is the following XSLT snippet:

<?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:sp="http://schemas.microsoft.com/sharepoint/"
                exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="IncludeFields" select="'Title FirstName'"/>

  <xsl:variable name="IncludeFieldsSet">
    <xsl:call-template name="GetIncludeFieldsSet">
      <xsl:with-param name="fieldList" select="$IncludeFields"/>
    </xsl:call-template>
  </xsl:variable>
  
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="text()">
    <xsl:choose>
      <xsl:when test="string-length(normalize-space(self::node())) &gt; 0">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:when>
      <xsl:otherwise />
    </xsl:choose>
  </xsl:template>

  <xsl:template match="sp:Field">
    <xsl:variable name="name" select="@Name"/>
    <xsl:choose>
      <xsl:when test="count(msxsl:node-set($IncludeFieldsSet)/field[@name = $name]) &gt; 0">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:when>
      <xsl:otherwise />
    </xsl:choose>
  </xsl:template>

  <xsl:template name="GetIncludeFieldsSet">
    <xsl:param name="fieldList"/>
    <xsl:variable name="trimmedFieldList" select="concat(normalize-space($fieldList), ' ')"/>
    <xsl:variable name="field" select="substring-before($trimmedFieldList, ' ')"/>
    <xsl:variable name="remaining" select="substring-after($trimmedFieldList, ' ')"/>
    <field>
      <xsl:attribute name="name">
        <xsl:value-of select="$field"/>
      </xsl:attribute>
    </field>
    <xsl:if test="$remaining">
      <xsl:call-template name="GetIncludeFieldsSet">
        <xsl:with-param name="fieldList" select="$remaining"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

Once you save it as an XSLT file, open it in Visual Studio and then from the XML menu choose the Start XSLT Without Debugging option.

The ‘Start XSLT Without Debugging’ option highlighted in the menu in Visual Studio 2010

When the Choose Input XML Document dialog window appears, select the imported Elements.xml file and click the Open button.

The ‘Elements.xml’ file selected in the ‘Choose Input XML Document’ dialog window

Once the XSL transformation completes, you should see another XML document open in Visual Studio this time containing only the fields you want.

Visual Studio with a new version of the Elements.xml file with unwanted fields removed

That’s all. You’re done.

How it works

The XSLT template showed above copies by default all nodes from the selected XML file (lines 16-20). When copying Field nodes additional logic is being executed what allows us to skip copying all unwanted fields (lines 33-43).

The list of fields that should be included in the output is defined as a space-separated list (line 8). Because all field names are Internal Names it is safe for us to assume that none of the names will contain a space.

Because XSLT doesn’t have a string split function we have to pre-process the list of fields to include before we start processing the List Instance XML file. To do this we call the GetIncludeFieldsSet template (lines 11-13) which converts the space-separated string into an XML node set which we can use for matching field names (line 36).

The last thing that our XSLT template does is to remove all empty text nodes. As mentioned before the template matches all nodes and by default copies them to the output. Because we skip some of the field nodes (unwanted fields) a lot of white spaces are being included in the output. By adding another template to match text nodes we can check if the specific text node is empty and if so remove it from the output (lines 22-31).

And that’s all! By modifying one line of code you can clear your imported List Instance of all unwanted data with a single mouse click!

Others found also helpful: