Skip to main content

If you don't have an IBM ID and password, register here.

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

Tip: Remove sensitive content from your XML samples with XSLT

Keep the XML structure as you cleanse restricted data

Uche Ogbuji (uche@ogbuji.net), Principal Consultant, Fourthought Inc.
Photo of Uche Ogbuji
Uche Ogbuji is a consultant and co-founder of Fourthought Inc., a software vendor and consultancy specializing in XML solutions for enterprise knowledge management. Fourthought develops 4Suite, an open source platform for XML, RDF, and knowledge-management applications. Mr. Ogbuji is also a lead developer of the Versa RDF query language. He is a computer engineer and writer born in Nigeria, living and working in Boulder, Colorado, USA. You can find more about Mr. Ogbuji at his Weblog Copia or contact him at uche@ogbuji.net.

Summary:  Do you need to share samples of your XML code, but can't disclose the data? For example, you might need to post a sample of your XML code with a question to get some advice with a problem. In this tip, Uche Ogbuji shows how to use XSLT to remove sensitive content and retain the basic XML structure.

View more content in this series

Date:  11 Apr 2006
Level:  Introductory

Comments:  

You might have run into this scenario if you work with XML files that contain sensitive data: You run into a problem, perhaps a bug, with your favorite XML processing tool. You need to send the vendor an example of a file that triggers the bug. You can't just send random XML files because you presume it's the special makeup of your sample files that is causing the problem. You need a way to cleanse the files of sensitive data, while you retain the specific structural characteristics of the file so it still serves as a good example of the problem. You can do so with a few XSLT tricks, and this article shows how.

Eliminating content

Listing 1 (kill-content.xslt) is an XSLT script that eliminates all text nodes and attribute values, leaving essentially a skeleton of the node structure.


Listing 1 (kill-content.xslt). XSLT script to remove all 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 with many useful scripts this builds on the identity transform, as you can see from the first template. The second template copies all attributes to the output, except that it ignores the attribute value. The third template suppresses all text nodes. All other node types, including elements, are handled by the first template, which copies their basic structure to the output. Listing 2 is an example of a file to be processed.


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>


When you apply the XSLT script from Listing 1 to the XML sample in Listing 2, you get XML along the lines of the following (ignoring the XML declaration).

<patients><patient id="" admitted=""><name/><address><street/><city/>...


You can see how the basic element and attribute structure remains, but devoid of content.

Shooting blanks

If complete removal of the character data is too extreme and you want to preserve at least the length of each node, a variation on Listing 1 might be useful. The XSLT script in Listing 3 replaces all character data with a node of the same length comprising only spaces.


Listing 3 (blank-content.xslt). XSLT script to replace all character data with blank content
                <?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:x="http://ns.ogbuji.net/articles"
>

  <x:wsbuffer xml:space="preserve">                        </x:wsbuffer>
  <xsl:variable name="wsbuf" select="string(document('')/*/x:wsbuffer)"/>

  <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:value-of select="translate(., ., $wsbuf)"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="text()">
    <xsl:value-of select="translate(., ., $wsbuf)"/>
  </xsl:template>

</xsl:transform>


The key bit is the top-level extension element x:wsbuffer which provides a stock of blank character content to be used in the replacement operation. I kept the run of spaces short, for formatting reasons, but you might want to add many more blanks to the element because all character data nodes in the original data are replaced by an equivalent length of spaces up to a maximum which is the number of characters in x:wsbuffer. The xml:space attribute is to prevent XSLT processors from stripping the whitespace within x:wsbuffer. You can actually use any contents for x:wsbuffer and it will be used as the stock replacement text for character data. If you do use any non-whitespace characters for this purpose, you don't need the xml:space attribute.

The variable wsbuf is set up with the replacement text by introspection into the XSLT document (using the special meaning of document('')). The rest of the transform is similar to Listing 1, except that rather than suppress content, it replaces each character with the one of equivalent position from wsbuf. As it stands in Listing 1, the result is that all output content is blank. You can experiment with the contents of x:wsbuffer if you want to alter the output.

When you apply the XSLT script in Listing 3 to the XML in Listing 2, you get XML along the lines of the following.

<patients>   <patient id="  " admitted="          ">     <name>          </name>...



Wrap up

In this article, you learned how to modify XML documents so the basic structure stays the same, but you eliminate or modify the content so sensitive information is not leaked. The techniques presented do have some limitations, mostly due to the limitations in XSLT itself. Most notably, if part of the problem situation you try to communicate involves the nature of character data itself (for example, a bug handling character entities) you lose the key information if you use variations of the samples shown here. Remember that XSLT also loses some information in XML files, such as the top declarations, entity references and CDATA sections. If these are key to the problem in question you will not be able to use XSLT to cleanse the files. I have seen many cases where these scripts are quite sufficient, so these are techniques well worth learning. You can control character data even more closely if you're willing to use EXSLT extensions (See resources).


Resources

Learn

Get products and technologies

  • 4XSLT, part of 4Suite: Experiment with the code samples and the stylesheet processor (which the author co-develops) used to test the examples.

About the author

Photo of Uche Ogbuji

Uche Ogbuji is a consultant and co-founder of Fourthought Inc., a software vendor and consultancy specializing in XML solutions for enterprise knowledge management. Fourthought develops 4Suite, an open source platform for XML, RDF, and knowledge-management applications. Mr. Ogbuji is also a lead developer of the Versa RDF query language. He is a computer engineer and writer born in Nigeria, living and working in Boulder, Colorado, USA. You can find more about Mr. Ogbuji at his Weblog Copia or contact him at uche@ogbuji.net.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in

If you don't have an IBM ID and password, register here.


Forgot your IBM ID?


Forgot your password?
Change your password


By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)


By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=107530
ArticleTitle=Tip: Remove sensitive content from your XML samples with XSLT
publish-date=04112006
author1-email=uche@ogbuji.net
author1-email-cc=dwxed@us.ibm.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).