To delete sensitive content in an XML sample file with XSLT

Source: Internet
Author: User
Tags xsl xslt

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.