When working with an XML file, you may experience a situation where the file contains sensitive data, and your favorite XML processing tool has a problem, such as a bug. You need to provide the vendor with a sample file that causes a bug. Of course, you can't send an XML file randomly because it may be a special tag in the sample file that is causing the problem. You need a way to clear sensitive data in a file while maintaining the special structural characteristics of the file so that you can still explain the problem. As you can see in this article, a little XSLT technique solves it.
Eliminate content
The XSLT script in Listing 1 (kill-content.xslt) can delete all text nodes and attribute values, leaving only the skeleton of the node structure.
Listing 1 (KILL-CONTENT.XSLT). XSLT script that clears character data
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute namespace="{namespace-uri()}" name="{name()}"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
As you can see from the first template, as with many useful scripts, the identity transformation is also used here. The second template copies all the properties to the output, but omits the property values. The third template deletes all text nodes. All other node types, including elements, are processed by the first template, which copies the base structure of the node to the output. Listing 2 is a sample file that is processed with this script.
Listing 2 (patients.xml). Sample XML File
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<patients>
<patient id= ' EP ' admitted= "2003-06-10" >
<name>ezra pound</name>
<address>
<street>45 Usura place</street>
<city>Hailey</city>
<province>ID</province>
</address>
<condition>ore infectus</condition>
</patient>
<patient id= ' tse ' admitted= ' 2003-06-20 ' >
<name>thomas eliot</name>
<address>
<street>3 Prufrock lane</street>
<city>Stamford</city>
<province>CT</province>
</address>
<condition>sartor resartus</condition>
</patient>
<patient id= "CO" admitted= "2004-11-15" >
<name>christopher okigbo</name>
<address>
<street>7 Heaven ' s gate</street>
<city>Idoto</city>
<province>Anambra</province>
</address>
<condition>caeli Porta quaerit</condition>
</patient>
</patients>