[KLUG Members] XML/DSML/XSL Transform Help

Jamie McCarthy jamie at mccarthy.vg
Fri Nov 12 12:49:18 EST 2004


I don't know jack about XSL, but here's how I'd do it in perl.  :)



jamie at daisy:~$ ./adam-xml.pl 
<resultSet>
  <resultEntry>
    <birthDate>02/17/1848</birthDate>
    <cn>YoYo Mama</cn>
    <employeeNumber>YOY004</employeeNumber>
    <morrisonserialid>1015</morrisonserialid>
  </resultEntry>
</resultSet>
jamie at daisy:~$ cat adam-xml.pl 
#!/usr/bin/perl

use XML::Simple;

# Read in the data following the __DATA__ delimiter
undef $/;
my $data = <DATA>;
my $ref = XMLin($data);

# Pull out the data from the only part we care about, into
# the $transformed hashref.
my $transformed = { };
my @keys = qw( morrisonserialid birthDate cn employeeNumber );
for my $key (@keys) {
        $transformed->{$key} = [ $ref->{searchResponse}{searchResultEntry}{$key}{value} ];
}

# Emit $transformed as the desired XML.  "KeepRoot" omits the
# <opt>...</opt> tags that otherwise surround the output.
my $resultSet = { resultSet => [ { resultEntry => [ $transformed ] } ] };
print XMLout($resultSet, KeepRoot => 1);

__DATA__
<?xml version="1.0" encoding="UTF-8"?>
<batchResponse xmlns="urn:oasis:names:tc:DSML:2:0:core"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<searchResponse requestID="8">
<searchResultEntry dn="cn=YoYo MaMa,ou=People,o=Morrison
Industries,c=US" requestID="8">
<attr name="morrisonserialid">
<value>1015</value>
</attr>
<attr name="birthDate">
<value>02/17/1848</value>
</attr>
<attr name="cn">
<value>YoYo Mama</value>
</attr>
<attr name="employeeNumber">
<value>YOY004</value>
</attr>
</searchResultEntry>
<searchResultDone requestID="8">
<resultCode code="0" descr="Success"/>
</searchResultDone>
</searchResponse>
</batchResponse>



OK, that was a fun way to spend 15 minutes, learning a little about
XML::Simple.  Back to work...
-- 
  Jamie McCarthy
 http://mccarthy.vg/
  jamie at mccarthy.vg



More information about the Members mailing list