Project

General

Profile

Howto: Use DCMTK's XML tools and an XSLT processor to change the patient's name

Here is an example that shows how to use DCMTK's XML tools and an XSLT processor to change the patient's name.

Of course, this is only for demonstration purposes how XML conversion works with DCMTK. Changing the patient's name can be much easier using the dcmodify tool, or using very few lines of code, using functions from dcmdata .

Command line

This command line should work on your Unix system (provided that xsltproc is installed):

dcm2xml +Wb input.dcm | xsltproc ch_pname.xsl - | xml2dcm - output.dcm

XSLT file

The following script (ch_pname.xsl) only changes the Patient's Name (0010,0010) but, of course, you are free to extend it to your needs:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- GENERAL -->

<xsl:output method="xml" indent="yes"/>

<xsl:param name="patients-name" select="'Doe^John'"/>

<!-- MAIN -->

<xsl:template match="file-format|data-set">
  <!-- special handling for surrounding XML elements -->
  <xsl:element name="{name()}">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

<!-- TEMPLATES -->

<xsl:template match="element[@tag='0010,0010']">
  <xsl:element name="{name()}">
    <xsl:copy-of select="@*"/>
    <!-- replace patient's name -->
    <xsl:value-of select="$patients-name"/>
  </xsl:element>
</xsl:template>

<xsl:template match="meta-header|element|sequence|item|pixel-item">
  <!-- copy all other XML elements -->
  <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>