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:
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" >
When the Choose Input XML Document dialog window appears, select the imported Elements.xml file and click the Open button.
Once the XSL transformation completes, you should see another XML document open in Visual Studio this time containing only the fields you want.
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!
Originally posted at https://blog.mastykarz.nl/removing-unwanted-fields-imported-list-instances-xslt/